Compiler Design Lab
Compiler Design Lab
Compiler Design Lab
1
Shivam Bindal (BT19CSE008)
INDEX
2
Shivam Bindal (BT19CSE008)
Q- Write a program by taking input as C File. (i) Identify the total number of
tokens as output. (ii) Identify the total number of unique tokens as output.
Code:
////////////////////////////////////////////////////
// //
// Coded by Shivam Bindal //
// //
////////////////////////////////////////////////////
#include <bits/stdc++.h>
#define int long long
using namespace std;
bool iskeyword(string s){
string keywords[32] =
{"auto","break","case","char","const","continue","default","do",
"double","else","enum","extern","float",
"for","goto","if","int","long","register","return","short","
signed","sizeof","static","struct","switch","typedef","union","u
nsigned",
"void","volatile","while"};
for(int i = 0; i < 32; ++i){
if(s == keywords[i]){
return true;
}
}
return false;
}
bool isOperator(char ch){
if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch
== '>' || ch == '<' || ch == '=')
return true;
return false;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
char ch;
string s="";
ifstream fin("ddd.txt");
int i,j=0;
if(!fin.is_open()){
cout<<"error while opening the file\n";
exit(0);
3
Shivam Bindal (BT19CSE008)
}
int tt=0;
int x=0 ,y=0 , z=0 , p=0;
while(!fin.eof()){
ch = fin.get();
if(isOperator(ch)){
// cout<<ch<<" is operator\n";
x++;
tt++;
}
if(isalnum(ch)){
s+=ch;
}
else if((ch == ' ' || ch == '\n')){
// cout<<s<<endl;
if(iskeyword(s) == 1){
// cout<<s<<" is keyword\n";
z++;
tt++;
}
else{
//cout<<s<<" is indentifier\n";
p++;
tt++;
}
s="";
}
}
cout<<"Total Number of Tokens = "<<tt<<endl;
cout<<"Total Number of keyword = "<<z<<endl;
cout<<"Total Number of identifiers = "<<p<<endl;
cout<<"Total Number of operators = "<<x<<endl;
fin.close();
return 0;
}
Output:
4
Shivam Bindal (BT19CSE008)
Q- Remove Left Recursion in given grammar and take input from file and give
output in a file.
Code:
#include <bits/stdc++.h>
#define SIZE 10
using namespace std;
int main(){
int n , index=3;
char p[10][SIZE] ,a , b , c;
cout<<"Enter number of p : "<<endl;
cin>>n;
cout<<"Enter the grammar as E->E-A :"<<endl;
for(int i=0;i<n;i++) cin>>p[i];
for(int i=0;i<n;i++){
cout<<"\nGRAMMAR : : :"<<p[i]<<endl;
a=p[i][0];
if(a==p[i][index]) {
c=p[i][index+1];
cout<<" is left recursive.\n";
while(p[i][index]!=0 && p[i][index]!='|') index++;
if(p[i][index]!=0) { b=p[i][index+1];
cout<<"Grammar without left recursion:\n";
cout<<a<<"->"<<b<<a<<endl;
cout<<a<<"->"<<c<<a<<endl;
}
else cout<<" can't be reduced\n";
}
else cout<<" is not left recursive.\n";
index=3;
}
return 0;
}
Output:
5
Shivam Bindal (BT19CSE008)
Code:
////////////////////////////////////////////////////
// //
// Coded by Shivam Bindal //
// //
////////////////////////////////////////////////////
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void followfirst(char , int , int);
void findfirst(char , int , int);
void follow(char c);
int count,n=0;
char calc_first[10][100];
char calc_follow[10][100];
int m=0;
char production[10][10], first[10];
char f[10];
int k;
char ck;
int e;
int main(int argc,char **argv)
{
int jm=0;
int km=0;
int i,choice;
char c,ch;
printf("How many productions ? :");
scanf("%d",&count);
printf("\nEnter %d productions in form A=B where A and B are
grammar symbols :\n\n",count);
for(i=0;i<count;i++)
{
scanf("%s%c",production[i],&ch);
}
int kay;
char done[count];
int ptr = -1;
for(k=0;k<count;k++){
for(kay=0;kay<100;kay++){
calc_first[k][kay] = '!';
}
6
Shivam Bindal (BT19CSE008)
}
int point1 = 0,point2,xxx;
for(k=0;k<count;k++)
{
c=production[k][0];
point2 = 0;
xxx = 0;
for(kay = 0; kay <= ptr; kay++)
if(c == done[kay])
xxx = 1;
if (xxx == 1)
continue;
findfirst(c,0,0);
ptr+=1;
done[ptr] = c;
printf("\n First(%c)= { ",c);
calc_first[point1][point2++] = c;
for(i=0+jm;i<n;i++){
int lark = 0,chk = 0;
for(lark=0;lark<point2;lark++){
if (first[i] == calc_first[point1][lark]){
chk = 1;
break;
}
}
if(chk == 0){
printf("%c, ",first[i]);
calc_first[point1][point2++] = first[i];
}
}
printf("}\n");
jm=n;
point1++;
}
printf("\n");
printf("-----------------------------------------------
\n\n");
char donee[count];
ptr = -1;
for(k=0;k<count;k++){
for(kay=0;kay<100;kay++){
calc_follow[k][kay] = '!';
}
}
point1 = 0;
7
Shivam Bindal (BT19CSE008)
int land = 0;
for(e=0;e<count;e++)
{
ck=production[e][0];
point2 = 0;
xxx = 0;
for(kay = 0; kay <= ptr; kay++)
if(ck == donee[kay])
xxx = 1;
if (xxx == 1)
continue;
land += 1;
follow(ck);
ptr+=1;
donee[ptr] = ck;
printf(" Follow(%c) = { ",ck);
calc_follow[point1][point2++] = ck;
for(i=0+km;i<m;i++){
int lark = 0,chk = 0;
for(lark=0;lark<point2;lark++){
if (f[i] == calc_follow[point1][lark]){
chk = 1;
break;
}
}
if(chk == 0){
printf("%c, ",f[i]);
calc_follow[point1][point2++] = f[i];
}
}
printf(" }\n\n");
km=m;
point1++;
}
char ter[10];
for(k=0;k<10;k++){
ter[k] = '!';
}
int ap,vp,sid = 0;
for(k=0;k<count;k++){
for(kay=0;kay<count;kay++){
if(!isupper(production[k][kay]) &&
production[k][kay]!= '#' && production[k][kay] != '=' &&
production[k][kay] != '\0'){
vp = 0;
8
Shivam Bindal (BT19CSE008)
9
Shivam Bindal (BT19CSE008)
int tuna = 0;
for(zap=0;zap<count;zap++){
if(calc_first[zap][0] == production[ap][k]){
for(tuna=1;tuna<100;tuna++){
if(calc_first[zap][tuna] != '!'){
tem[ct++] =
calc_first[zap][tuna];
}
else
break;
}
break;
}
}
tem[ct++] = '_';
}
k++;
}
int zap = 0,tuna;
for(tuna = 0;tuna<ct;tuna++){
if(tem[tuna] == '#'){
zap = 1;
}
else if(tem[tuna] == '_'){
if(zap == 1){
zap = 0;
}
else
break;
}
else{
first_prod[ap][destiny++] = tem[tuna];
}
}
}
char table[land][sid+1];
ptr = -1;
for(ap = 0; ap < land ; ap++){
for(kay = 0; kay < (sid + 1) ; kay++){
table[ap][kay] = '!';
}
}
for(ap = 0; ap < count ; ap++){
ck = production[ap][0];
xxx = 0;
10
Shivam Bindal (BT19CSE008)
11
Shivam Bindal (BT19CSE008)
cz = cz + 1;
}
int vz=0;
while(ter[vz] != calc_follow[k][fz]){
vz = vz + 1;
}
table[k][vz+1] = '#';
fz++;
}
break;
}
}
}
for(ap = 0; ap < land ; ap++){
printf("\t\t\t %c\t|\t",table[ap][0]);
for(kay = 1; kay < (sid + 1) ; kay++){
if(table[ap][kay] == '!')
printf("\t\t");
else if(table[ap][kay] == '#')
printf("%c=#\t\t",table[ap][0]);
else{
int mum = (int)(table[ap][kay]);
mum -= 65;
printf("%s\t\t",production[mum]);
}
}
printf("\n");
printf("\t\t\t------------------------------------------
----------------------------------------------------------------
-----------");
printf("\n");
}
int j;
printf("\n\nPlease enter the desired INPUT STRING = ");
char input[100];
scanf("%s%c",input,&ch);
printf("\n\t\t\t\t\t========================================
===================================\n");
printf("\t\t\t\t\t\tStack\t\t\tInput\t\t\tAction");
printf("\n\t\t\t\t\t========================================
===================================\n");
int i_ptr = 0,s_ptr = 1;
char stack[100];
stack[0] = '$';
stack[1] = table[0][0];
12
Shivam Bindal (BT19CSE008)
while(s_ptr != -1){
printf("\t\t\t\t\t\t");
int vamp = 0;
for(vamp=0;vamp<=s_ptr;vamp++){
printf("%c",stack[vamp]);
}
printf("\t\t\t");
vamp = i_ptr;
while(input[vamp] != '\0'){
printf("%c",input[vamp]);
vamp++;
}
printf("\t\t\t");
char her = input[i_ptr];
char him = stack[s_ptr];
s_ptr--;
if(!isupper(him)){
if(her == him){
i_ptr++;
printf("POP ACTION\n");
}
else{
printf("\nString Not Accepted by LL(1) Parser
!!\n");
exit(0);
}
}
else{
for(i=0;i<sid;i++){
if(ter[i] == her)
break;
}
char produ[100];
for(j=0;j<land;j++){
if(him == table[j][0]){
if (table[j][i+1] == '#'){
printf("%c=#\n",table[j][0]);
produ[0] = '#';
produ[1] = '\0';
}
else if(table[j][i+1] != '!'){
int mum = (int)(table[j][i+1]);
mum -= 65;
strcpy(produ,production[mum]);
printf("%s\n",produ);
13
Shivam Bindal (BT19CSE008)
}
else{
printf("\nString Not Accepted by LL(1)
Parser !!\n");
exit(0);
}
}
}
int le = strlen(produ);
le = le - 1;
if(le == 0){
continue;
}
for(j=le;j>=2;j--){
s_ptr++;
stack[s_ptr] = produ[j];
}
}
}
printf("\n\t\t\t============================================
================================================================
===========\n");
if (input[i_ptr] == '\0'){
printf("\t\t\t\t\t\t\t\tYOUR STRING HAS BEEN ACCEPTED
!!\n");
}
else
printf("\n\t\t\t\t\t\t\t\tYOUR STRING HAS BEEN REJECTED
!!\n");
printf("\t\t\t==============================================
================================================================
=========\n");
}
void follow(char c)
{
int i ,j;
if(production[0][0]==c){
f[m++]='$';
}
for(i=0;i<10;i++)
{
for(j=2;j<10;j++)
{
if(production[i][j]==c)
14
Shivam Bindal (BT19CSE008)
{
if(production[i][j+1]!='\0'){
followfirst(production[i][j+1],i,(j+2));
}
if(production[i][j+1]=='\0'&&c!=production[i][0]
){
follow(production[i][0]);
}
}
}
}
}
15
Shivam Bindal (BT19CSE008)
{
int k;
if(!(isupper(c)))
f[m++]=c;
else{
int i=0,j=1;
for(i=0;i<count;i++)
{
if(calc_first[i][0] == c)
break;
}
while(calc_first[i][j] != '!')
{
if(calc_first[i][j] != '#'){
f[m++] = calc_first[i][j];
}
else{
if(production[c1][c2] == '\0'){
follow(production[c1][0]);
}
else{
followfirst(production[c1][c2],c1,c2+1);
}
}
j++;
}
}
}
Output:
16
Shivam Bindal (BT19CSE008)
Code:
////////////////////////////////////////////////////
// //
// Coded by Shivam Bindal //
// //
////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <stack>
using namespace std;
void find_first(vector< pair<char, string> > gram,
map< char, set<char> > &firsts,
char non_term);
void find_follow(vector< pair<char, string> > gram,
map< char, set<char> > &follows,
map< char, set<char> > firsts,
char non_term);
int main(int argc, char const *argv[])
{
if(argc != 3) {
cout<<"Arguments should be <grammar file> <input
string>\n";
return 1;
}
// Arguments check
// cout<<argv[1]<<argv[2];
17
Shivam Bindal (BT19CSE008)
while(!grammar_file.eof()) {
char buffer[20];
grammar_file.getline(buffer, 19);
18
Shivam Bindal (BT19CSE008)
int parse_table[non_terms.size()][terms.size()];
19
Shivam Bindal (BT19CSE008)
fill(&parse_table[0][0], &parse_table[0][0] +
sizeof(parse_table)/sizeof(parse_table[0][0]), -1);
for(auto prod = gram.begin(); prod != gram.end(); ++prod) {
string rhs = prod->second;
set<char> next_list;
bool finished = false;
for(auto ch = rhs.begin(); ch != rhs.end(); ++ch) {
if(!isupper(*ch)) {
if(*ch != 'e') {
next_list.insert(*ch);
finished = true;
break;
}
continue;
}
set<char> firsts_copy(firsts[*ch].begin(),
firsts[*ch].end());
if(firsts_copy.find('e') == firsts_copy.end()) {
next_list.insert(firsts_copy.begin(),
firsts_copy.end());
finished = true;
break;
}
firsts_copy.erase('e');
next_list.insert(firsts_copy.begin(),
firsts_copy.end());
}
// If the whole rhs can be skipped through epsilon or
reaching the end
// Add follow to next list
if(!finished) {
next_list.insert(follows[prod->first].begin(),
follows[prod->first].end());
}
20
Shivam Bindal (BT19CSE008)
}
// Print parse table
cout<<"Parsing Table: \n";
cout<<" ";
for(auto i = terms.begin(); i != terms.end(); ++i) {
cout<<*i<<" ";
}
cout<<"\n";
for(auto row = non_terms.begin(); row != non_terms.end();
++row) {
cout<<*row<<" ";
for(int col = 0; col < terms.size(); ++col) {
int row_num = distance(non_terms.begin(), row);
if(parse_table[row_num][col] == -1) {
cout<<"- ";
continue;
}
cout<<parse_table[row_num][col]<<" ";
}
cout<<"\n";
}
cout<<"\n";
string input_string(argv[2]);
input_string.push_back('$');
stack<char> st;
st.push('$');
st.push('S');
21
Shivam Bindal (BT19CSE008)
if(input_string[0] == st.top()) {
st.pop();
input_string.erase(0, 1);
}
else if(!isupper(st.top())) {
cout<<"Unmatched terminal found\n";
accepted = false;
break;
}
else {
char stack_top = st.top();
int row = distance(non_terms.begin(),
non_terms.find(stack_top));
int col = distance(terms.begin(),
terms.find(input_string[0]));
int prod_num = parse_table[row][col];
if(prod_num == -1) {
cout<<"No production found in parse table\n";
accepted = false;
break;
}
st.pop();
string rhs = gram[prod_num].second;
if(rhs[0] == 'e') {
continue;
}
for(auto ch = rhs.rbegin(); ch != rhs.rend(); ++ch)
{
st.push(*ch);
}
}
}
if(accepted) {
cout<<"Input string is accepted\n";
}
else {
cout<<"Input string is rejected\n";
22
Shivam Bindal (BT19CSE008)
return 0;
}
if(!isupper(*ch)) {
firsts[non_term].insert(*ch);
break;
}
else {
if(firsts[*ch].empty()) {
find_first(gram, firsts, *ch);
}
// If variable doesn't have epsilon, stop loop
if(firsts[*ch].find('e') == firsts[*ch].end()) {
firsts[non_term].insert(firsts[*ch].begin(),
firsts[*ch].end());
break;
}
set<char> firsts_copy(firsts[*ch].begin(),
firsts[*ch].end());
23
Shivam Bindal (BT19CSE008)
}
}
}
}
set<char> firsts_copy(firsts[*ch]);
// If char's firsts doesnt have epsilon follow
search is over
if(firsts_copy.find('e') == firsts_copy.end()) {
follows[non_term].insert(firsts_copy.begin(),
firsts_copy.end());
finished = true;
break;
24
Shivam Bindal (BT19CSE008)
}
// Else next char has to be checked after appending
firsts to follow
firsts_copy.erase('e');
follows[non_term].insert(firsts_copy.begin(),
firsts_copy.end());
Output:
25
Shivam Bindal (BT19CSE008)
Code:
"""
////////////////////////////////////////////////////
// //
// Coded by Shivam Bindal //
// //
////////////////////////////////////////////////////
"""
# 1 ----------------- To Find Closure ----------------
def closure(I,nonT):
J = I
for item in J :
#print(item)
index = item[1].index('.')
if(index<(len(item[1])-1) and item[1][index+1] in nonT):
#print('item : ',item[1][index+1])
for production in nonT[item[1][index+1]]:
if( [item[1][index+1],str('.')+str(production)]
not in J):
J.append([item[1][index+1],str('.')+str(prod
uction)])
#print([item[1][index+1],str('.')+str(produc
tion)])
return J
state = []
I = []
def setOfItems(start,nonTer,ter):
I.append(closure([['start','.'+start+'$']],nonTer))
#print(I)
ter += list(nonTer.keys())
#print("list of inputs : " , ter)
for conI in I:
for grammar in ter:
if(grammar == '$'):
continue
#print("grammar : ",grammar)
goto = False
goto1 = False
shift = False
26
Shivam Bindal (BT19CSE008)
shift1 = False
reduce = False
close = []
for item in conI:
#print("item : ",item)
if(item[1].index('.')<(len(item[1])-1) and
item[1][item[1].index('.')+1] is grammar):
close.append([item[0],item[1][:item[1].index
('.')]+grammar+'.'+item[1][item[1].index('.')+2:]])
#else:
# print(item)
#print("close : ",close)
l = closure(close,nonTer)
if(len(l) == 0):
continue
#print("closure : ", l)
if(grammar in nonTer.keys()):
goto1 = True
else:
shift1 = True
if(l not in I):
if(goto1):
state.append(['g',I.index(conI)+1,len(I)+1,g
rammar])
goto = True
elif(shift1):
shift = True
state.append(['s',I.index(conI)+1,len(I)+1,g
rammar])
I.append(l)
else:
if(goto1):
goto = True
state.append(['g',I.index(conI)+1,I.index(l)
+1,grammar])
elif(shift1):
shift = True
state.append(['s',I.index(conI)+1,I.index(l)+
1,grammar])
# --------------------------------------------------------------
---
# 3. -----------------Create a Parse Table ---------------------
---
reduce = []
27
Shivam Bindal (BT19CSE008)
accept = -1
def toReduce(rule,accept,start):
s = ['start',start+'.$']
for parState in I:
#print(s,parState)
if(s in parState):
#print("here;")
accept = I.index(parState)
for item in parState:
if( item in rule):
reduce[I.index(parState)].append(rule.index(item
))
return accept
# --------------------------------------------------------------
----
# 4. --------------------- To Parse ----------------------------
----
symbolMap = dict()
parseTable = []
def createParseTable(ter):
for i in state:
parseTable[i[1]-1][symbolMap[i[3]]] = i[0]+str(i[2]-1)
parseTable[accept][symbolMap['$']] = 'a'
for i in reduce:
if(len(i)>0):
for j in ter:
parseTable[reduce.index(i)][symbolMap[j]] =
'r'+str(i[0])
def isEmpty(self):
return len(self.__storage) == 0
def push(self,p):
self.__storage.append(p)
def pop(self):
28
Shivam Bindal (BT19CSE008)
return self.__storage.pop()
def top(self):
return self.__storage[len(self.__storage) - 1]
def length(self):
return len(self.__storage)
def __str__(self):
"""
Because of using list as parent class for stack, our
last element will
be first for stack, according to FIFO principle. So, if
we will use
parent's implementation of str(), we will get reversed
order of
elements.
"""
#: You can reverse elements and use supper `__str__`
method, or
#: implement it's behavior by yourself.
#: I choose to add 'stack' in the begging in order to
differ list and
#: stack instances.
return 'stack [{}]'.format(', '.join([ str(i) for i in
reversed(self.__storage) ]))
29
Shivam Bindal (BT19CSE008)
for i in range(n):
ch = input("NonTerminals : ").strip()
rules = input("Productions (|) : ").split("|")
nonTerminals[ch] = rules
# --- Old Rules-------
S = input("Start Symbol : ")
terminals+=['$']
print("Productions : ")
for i in nonTerminals.keys():
print(i,"-->",end=' ')
for j in nonTerminals[i]:
print(j,end= ' | ')
print()
setOfItems(S,nonTerminals,terminals)
print("canonicals Items : ")
for count , i in enumerate(I):
print(count+1 , i)
30
Shivam Bindal (BT19CSE008)
rule = []
accept = -1
for i in nonTerminals.keys():
for j in nonTerminals[i]:
rule.append([i,j+str('.')])
print('rule :')
for i in rule:
print(i)
# ------- To find the reduction rules - -- - -- ---
reduce = [ [] for i in range(len(I)) ]
accept = toReduce(rule,accept,S)
print("reduce")
for count,i in enumerate(reduce):
print(count+1,i)
print("accept : ",accept+1)
# --- - - - parse Table - -- -- - -- -- - -- - - -- -
symbols = []
symbols += terminals
for count , i in enumerate(symbols):
symbolMap[i] = count
print(symbols)
for i in nonTerminals.keys():
terminals.remove(i)
createParseTable(terminals)
# ---Parse Table-----
print('Parse Table')
print(" \t\t",end='')
for i in symbols:
print(i,end= '\t')
print()
for count,j in enumerate(parseTable):
print(count,end='\t\t')
for i in j:
print(i,end='\t')
print()
31
Shivam Bindal (BT19CSE008)
if(parseString(rule,string)):
print("Accepted")
else:
print("Not accepted")
Output:
32
Shivam Bindal (BT19CSE008)
Code:
////////////////////////////////////////////////////
// //
// Coded by Shivam Bindal //
// //
////////////////////////////////////////////////////
#include<bits/stdc++.h>
#include <algorithm>
#include <set>
#include <math.h>
#include <vector>
#define MOD 1000000007
#define mode 998244353
#define mo 10000005
using namespace std;
set<char> ss;
map<char,vector<vector<char>>> mp;
bool dfs(char i, char org, char last,
map<char,vector<vector<char>>> &mp){
bool rtake = false;
for(auto r : mp[i]){
bool take = true;
for(auto s : r){
if(s == i) break;
if(!take) break;
if(!(s>='A'&&s<='Z')&&s!='e'){
ss.insert(s);
break;
}
else if(s == 'e'){
if(org == i||i == last)
ss.insert(s);
rtake = true;
break;
}
else{
take = dfs(s,org,r[r.size()-1],mp);
rtake |= take;
}
}
}
return rtake;
33
Shivam Bindal (BT19CSE008)
map<int,map<char,set<pair<deque<char>,deque<char>>>>> f;
map<int,vector<pair<int,char>>> g;
34
Shivam Bindal (BT19CSE008)
if(last != -1)
g[last].push_back({num,way});
}
else{
f[rep] = mp2;
}
int cc = num;
for(auto q : mp2){
for(auto r : q.second){
if(!r.second.empty()){
r.first.push_back(r.second.front());
r.second.pop_front();
dfs2(q.first,r.first.back(),cc,r);
}
}
}
}
int main(){
int i,j;
ifstream fin("input.txt");
string num;
vector<int> fs;
vector<vector<int>> a;
char start;
bool flag = 0;
cout<<"Grammar: "<<'\n';
while(getline(fin,num)){
if(flag == 0) start = num[0],flag = 1;
cout<<num<<'\n';
vector<char> temp;
char s = num[0];
for(i=3;i<num.size();i++){
if(num[i] == '|'){
mp[s].push_back(temp);
temp.clear();
}
else temp.push_back(num[i]);
}
mp[s].push_back(temp);
}
map<char,set<char>> fmp;
for(auto q : mp){
ss.clear();
35
Shivam Bindal (BT19CSE008)
dfs(q.first,q.first,q.first,mp);
for(auto g : ss) fmp[q.first].insert(g);
}
cout<<'\n';
cout<<"FIRST: "<<'\n';
for(auto q : fmp){
string ans = "";
ans += q.first;
ans += " = {";
for(char r : q.second){
ans += r;
ans += ',';
}
ans.pop_back();
ans+="}";
cout<<ans<<'\n';
}
map<char,set<char>> gmp;
gmp[start].insert('$');
int count = 10;
while(count--){
for(auto q : mp){
for(auto r : q.second){
for(i=0;i<r.size()-1;i++){
if(r[i]>='A'&&r[i]<='Z'){
if(!(r[i+1]>='A'&&r[i+1]<='Z'))
gmp[r[i]].insert(r[i+1]);
else {
char temp = r[i+1];
int j = i+1;
while(temp>='A'&&temp<='Z'){
if(*fmp[temp].begin()=='e'){
for(auto g : fmp[temp]){
if(g=='e') continue;
gmp[r[i]].insert(g);
}
j++;
if(j<r.size()){
temp = r[j];
if(!(temp>='A'&&temp<='Z
')){
gmp[r[i]].insert(tem
p);
36
Shivam Bindal (BT19CSE008)
break;
}
}
else{
for(auto g :
gmp[q.first]) gmp[r[i]].insert(g);
break;
}
}
else{
for(auto g : fmp[temp]){
gmp[r[i]].insert(g);
}
break;
}
}
}
}
}
if(r[r.size()-1]>='A'&&r[r.size()-1]<='Z'){
for(auto g : gmp[q.first])
gmp[r[i]].insert(g);
}
}
}
}
cout<<'\n';
cout<<"FOLLOW: "<<'\n';
for(auto q : gmp){
string ans = "";
ans += q.first;
ans += " = {";
for(char r : q.second){
ans += r;
ans += ',';
}
ans.pop_back();
ans+="}";
cout<<ans<<'\n';
}
string temp = "";
temp+='.';
temp+=start;
37
Shivam Bindal (BT19CSE008)
deque<char> emp;
deque<char> st;
st.push_back(start);
dfs2('!','k',-1,{emp,st});
cout<<"\nProductions: "<<'\n';
int cc = 1;
set<char> action,go;
map<pair<char,deque<char>>,int> pos;
for(auto q : mp){
go.insert(q.first);
for(auto r : q.second){
cout<<"r"<<cc<<": ";
string ans = "";
ans += q.first;
ans+="->";
deque<char> temp;
for(auto s : r) ans += s,temp.push_back(s);
pos[{q.first,temp}] = cc;
for(auto s : r){
if(s>='A'&&s<='Z') go.insert(s);
else action.insert(s);
}
cout<<ans<<'\n';
cc++;
}
}
cout<<"\nGraph: "<<'\n';
for(auto mp2 : f){
cout<<'\n';
cout<<"I";
cout<<mp2.first<<": \n";
for(auto q : mp2.second){
string ans = "";
ans += q.first;
ans += "->";
for(auto r : q.second){
for(auto t : r.first) ans+=t;
ans+='.';
for(auto t : r.second) ans+=t;
ans+='|';
}
ans.pop_back();
for(auto tt : ans){
38
Shivam Bindal (BT19CSE008)
39
Shivam Bindal (BT19CSE008)
}
if(!flag){
for(auto r : f[i]){
char ccc = r.first;
deque<char> chk =
(*r.second.begin()).first;
int cou = 1;
for(auto r : gmp[ccc]){
if(q == r){
cout<<"r"<<pos[{ccc,chk}]<<"\t";
}
cou++;
}
}
}
}
}
for(auto q : go){
if(g.count(i)){
int flag = 0;
for(auto r : g[i]){
if(r.second == q){
flag = 1;
cout<<r.first<<"\t";
break;
}
}
if(!flag) cout<<"-"<<'\t';
}
else{
cout<<"-"<<'\t';
}
}
cout<<'\n';
}
return 0;
}
40
Shivam Bindal (BT19CSE008)
Output:
41
Shivam Bindal (BT19CSE008)
43
Shivam Bindal (BT19CSE008)
lastr=list(firstfollow.compute_first(i[i.index('
.')+2])-set(chr(1013)))
else:
lastr=i.lookahead
for prod in production_list:
head, body=prod.split('->')
if head!=Y: continue
newitem=Item(Y+'->.'+body, lastr)
if not exists(newitem, items):
items.append(newitem)
flag=1
if flag==0: break
return items
def goto(items, symbol):
dot.node(symbol,str(items))
global production_list
initial=[]
for i in items:
if i.index('.')==len(i)-1: continue
head, body=i.split('->')
seen, unseen=body.split('.')
if unseen[0]==symbol and len(unseen) >= 1:
initial.append(Item(head+'-
>'+seen+unseen[0]+'.'+unseen[1:], i.lookahead))
return closure(initial)
def calc_states():
for s in states:
if len(s) != len(t): continue
if sorted(s)==sorted(t):
for i in range(len(s)):
if s[i].lookahead!=t[i].lookahead: break
else: return True
return False
head, body=production_list[0].split('->')
states=[closure([Item(head+'->.'+body, ['$'])])]
45
Shivam Bindal (BT19CSE008)
while True:
flag=0
for s in states:
for e in nt_list+t_list:
t=goto(s, e)
states.append(t)
flag=1
return states
def make_table(states):
def getstateno(t):
for s in states:
if len(s.closure) != len(t): continue
if sorted(s.closure)==sorted(t):
for i in range(len(s.closure)):
if
s.closure[i].lookahead!=t[i].lookahead: break
else: return s.no
return -1
def getprodno(closure):
closure=''.join(closure).replace('.', '')
return production_list.index(closure)
SLR_Table=OrderedDict()
for i in range(len(states)):
states[i]=State(states[i])
46
Shivam Bindal (BT19CSE008)
for s in states:
SLR_Table[s.no]=OrderedDict()
nextsym=body.split('.')[1]
if nextsym=='':
if getprodno(item)==0:
SLR_Table[s.no]['$']='accept'
else:
for term in item.lookahead:
if term not in SLR_Table[s.no].keys():
SLR_Table[s.no][term]={'r'+str(getpr
odno(item))}
else: SLR_Table[s.no][term] |=
{'r'+str(getprodno(item))}
continue
nextsym=nextsym[0]
t=goto(s.closure, nextsym)
if t != []:
if nextsym in t_list:
if nextsym not in SLR_Table[s.no].keys():
SLR_Table[s.no][nextsym]={'s'+str(getsta
teno(t))}
else: SLR_Table[s.no][nextsym] |=
{'s'+str(getstateno(t))}
else: SLR_Table[s.no][nextsym] =
str(getstateno(t))
return SLR_Table
def augment_grammar():
47
Shivam Bindal (BT19CSE008)
pl,prod_list = firstfollow.main()
pro = prod_list.copy()
for nt in ntl:
firstfollow.compute_first(nt)
firstfollow.compute_follow(nt)
print(nt)
print("\tFirst:\t", firstfollow.get_first(nt))
print("\tFollow:\t", firstfollow.get_follow(nt), "\n")
augment_grammar()
nt_list=list(ntl.keys())
t_list=list(tl.keys()) + ['$']
cs=calc_states()
items = []
ctr=0
m = [ ]
for s in cs:
items.append(str(ctr))
ctr+=1
check = []
count = 0
ind = []
for i in cs:
if i not in check:
check.append(i)
else:
ind.append(count)
count += 1
merge_ind = []
48
Shivam Bindal (BT19CSE008)
combine = []
for i in ind:
if cs[i] in check:
merge_ind.append(cs.index(cs[i]))
combine.append(str(cs.index(cs[i]))+str(i))
for i in range(len(combine)):
combine.append("s"+combine[i])
#for i in cs:
#for j in i:
#print(j,j.lookahead)
table=make_table(cs)
sym_list = nt_list + t_list
for i in ind:
val = ind.index(i)
for j in table[i]:
if j not in table[int(merge_ind[val])]:
table[int(merge_ind[val])][j] = table[i][j]
table.pop(i)
for i in range(len(ind)):
s_list = []
s = "s" + str(ind[i])
s_list.append(s)
ind.append(set(s_list))
for i in range(len(merge_ind)):
s_list = []
s = "s" + str(merge_ind[i])
s_list.append(s)
merge_ind.append(set(s_list))
for i in range(0,int(len(merge_ind)/2)):
merge_ind[i] = str(merge_ind[i])
ind[i] = str(ind[i])
for i in table:
for j in table[i]:
if (table[i][j] in ind):
ind1 = ind.index(table[i][j])
49
Shivam Bindal (BT19CSE008)
table[i][j] = combine[ind1]
elif (table[i][j] in merge_ind):
ind1 = merge_ind.index(table[i][j])
table[i][j] = combine[ind1]
for i in items:
if i in merge_ind:
indexof = merge_ind.index(i)
c = combine[indexof]
j = ind[indexof]
j_ind = items.index(j)
items.pop(j_ind)
item_index = items.index(i)
items.pop(item_index)
items.insert(item_index,c)
print()
print("*******----STRING-----**********")
print()
lookahead = []
ctr = 0
for s in check:
string = []
st=[]
if items[ctr] in combine:
com_ind = combine.index(items[ctr])
for j in cs[int(ind[com_ind])].closure:
st.append(j.lookahead)
for i in range(len(s)):
string_i=[]
for k in s[i].lookahead:
string_i.append(k)
string_i.append(st[i][0])
string.append(string_i)
lookahead.append(string)
else:
for i in range(len(s)):
string_i=[]
for k in s[i].lookahead:
string_i.append(k)
string.append(string_i)
lookahead.append(string)
ctr+=1
50
Shivam Bindal (BT19CSE008)
ctr = 0
for s in check:
print("Item {}:".format(items[ctr]))
string = ""
for i in range(len(s)):
string += s[i]
string += " "
string += str(lookahead[ctr][i])
string += "\n"
print(string)
if len(items[ctr]) == 2:
for j in table[int(items[ctr][0])]:
if isinstance(table[int(items[ctr][0])][j],set):
pass
elif table[int(items[ctr][0])][j][0] == "s":
print(j,"->",table[int(items[ctr][0])][j][1:])
else:
print(j,"->",table[int(items[ctr][0])][j])
else:
for j in table[int(items[ctr])]:
if isinstance(table[int(items[ctr])][j],set):
pass
elif table[int(items[ctr][0])][j][0] == "s":
print(j,"->",table[int(items[ctr])][j][1:])
else:
print(j,"->",table[int(items[ctr])][j])
print()
ctr+=1
dis_arr = []
print("*******----PARSING TABLE-----**********")
print('_________________________________________________________
____________')
print("LALR(1) TABLE")
sym_list = nt_list + t_list
sr, rr=0, 0
print("\t GOTO \t\t ACTION")
print('_________________________________________________________
___________')
print('\t| ','\t| '.join(sym_list),'\t\t|')
print('_________________________________________________________
____________')
for i, j in table.items():
inti = str(i)
if inti in merge_ind:
51
Shivam Bindal (BT19CSE008)
inti = combine[merge_ind.index(inti)]
dfa={}
counter = 0
for i,j in table.items():
od={}
for k,l in j.items():
if isinstance(l,set):
od[k]=''.join(l)
elif l.isdigit():
od[k]=int(l)
else:
od[k]=l
dfa[dis_arr[counter]]=od
counter+=1
print("*******----STRING PARSING-----**********")
print()
string=input('Enter string to parse: ')
string+='$'
stack=['0']
pointer=0
try:
while True:
lookahead=string[pointer]
if dfa[stack[-1]][lookahead][0] =='s':
act = dfa[stack[-1]][lookahead][1:]
stack.append(lookahead)
52
Shivam Bindal (BT19CSE008)
stack.append(act)
print(stack)
pointer+=1
elif dfa[stack[-1]][lookahead][0] =='r':
r_no=int(dfa[stack[-1]][lookahead][1])
to_pop=pro[r_no-1][3:]
for i in range(2*len(to_pop)):
stack.pop()
stack.append(pro[r_no-1][0])
stack.append(str(dfa[stack[-2]][pro[r_no-1][0]]))
print(stack)
Output:
53
Shivam Bindal (BT19CSE008)
54
Shivam Bindal (BT19CSE008)
lastr=list(firstfollow.compute_first(i[i.index('
.')+2])-set(chr(1013)))
else:
lastr=i.lookahead
for prod in production_list:
head, body=prod.split('->')
if head!=Y: continue
newitem=Item(Y+'->.'+body, lastr)
if not exists(newitem, items):
items.append(newitem)
flag=1
if flag==0: break
return items
def goto(items, symbol):
global production_list
initial=[]
for i in items:
if i.index('.')==len(i)-1: continue
head, body=i.split('->')
seen, unseen=body.split('.')
if unseen[0]==symbol and len(unseen) >= 1:
initial.append(Item(head+'-
>'+seen+unseen[0]+'.'+unseen[1:], i.lookahead))
return closure(initial)
def calc_states():
def contains(states, t):
for s in states:
if len(s) != len(t): continue
if sorted(s)==sorted(t):
for i in range(len(s)):
if s[i].lookahead!=t[i].lookahead: break
else: return True
return False
global production_list, nt_list, t_list
head, body=production_list[0].split('->')
states=[closure([Item(head+'->.'+body, ['$'])])]
while True:
flag=0
for s in states:
for e in nt_list+t_list:
t=goto(s, e)
if t == [] or contains(states, t): continue
states.append(t)
flag=1
if not flag: break
56
Shivam Bindal (BT19CSE008)
return states
def make_table(states):
global nt_list, t_list
def getstateno(t):
for s in states:
if len(s.closure) != len(t): continue
if sorted(s.closure)==sorted(t):
for i in range(len(s.closure)):
if
s.closure[i].lookahead!=t[i].lookahead: break
else: return s.no
return -1
def getprodno(closure):
closure=''.join(closure).replace('.', '')
return production_list.index(closure)
SLR_Table=OrderedDict()
for i in range(len(states)):
states[i]=State(states[i])
for s in states:
SLR_Table[s.no]=OrderedDict()
for item in s.closure:
head, body=item.split('->')
if body=='.':
for term in item.lookahead:
if term not in SLR_Table[s.no].keys():
SLR_Table[s.no][term]={'r'+str(getprodno
(item))}
else: SLR_Table[s.no][term] |=
{'r'+str(getprodno(item))}
continue
nextsym=body.split('.')[1]
if nextsym=='':
if getprodno(item)==0:
SLR_Table[s.no]['$']='accept'
else:
for term in item.lookahead:
if term not in SLR_Table[s.no].keys():
SLR_Table[s.no][term]={'r'+str(getpr
odno(item))}
else: SLR_Table[s.no][term] |=
{'r'+str(getprodno(item))}
continue
nextsym=nextsym[0]
t=goto(s.closure, nextsym)
if t != []:
57
Shivam Bindal (BT19CSE008)
if nextsym in t_list:
if nextsym not in SLR_Table[s.no].keys():
SLR_Table[s.no][nextsym]={'s'+str(getsta
teno(t))}
else: SLR_Table[s.no][nextsym] |=
{'s'+str(getstateno(t))}
else: SLR_Table[s.no][nextsym] =
str(getstateno(t))
return SLR_Table
def augment_grammar():
for i in range(ord('Z'), ord('A')-1, -1):
if chr(i) not in nt_list:
start_prod=production_list[0]
production_list.insert(0, chr(i)+'-
>'+start_prod.split('->')[0])
return
def main():
global production_list, ntl, nt_list, tl, t_list
firstfollow.main()
print("\tFIRST AND FOLLOW OF NON-TERMINALS")
for nt in ntl:
firstfollow.compute_first(nt)
firstfollow.compute_follow(nt)
print(nt)
print("\tFirst:\t", firstfollow.get_first(nt))
print("\tFollow:\t", firstfollow.get_follow(nt), "\n")
augment_grammar()
nt_list=list(ntl.keys())
t_list=list(tl.keys()) + ['$']
print(nt_list)
print(t_list)
j=calc_states()
ctr=0
for s in j:
print("Item{}:".format(ctr))
for i in s:
print("\t", i)
ctr+=1
table=make_table(j)
print('_____________________________________________________
________________')
print("\n\tCLR(1) TABLE\n")
sym_list = nt_list + t_list
sr, rr=0, 0
58
Shivam Bindal (BT19CSE008)
print('_____________________________________________________
________________')
print('\t| ','\t| '.join(sym_list),'\t\t|')
print('_____________________________________________________
________________')
for i, j in table.items():
print(i, "\t| ", '\t| '.join(list(j.get(sym,' ') if
type(j.get(sym))in (str , None) else next(iter(j.get(sym,'
'))) for sym in sym_list)),'\t\t|')
s, r=0, 0
for p in j.values():
if p!='accept' and len(p)>1:
p=list(p)
if('r' in p[0]): r+=1
else: s+=1
if('r' in p[1]): r+=1
else: s+=1
if r>0 and s>0: sr+=1
elif r>0: rr+=1
print('_____________________________________________________
________________')
print("\n", sr, "s/r conflicts |", rr, "r/r conflicts")
print('_____________________________________________________
________________')
print("Enter the string to be parsed")
Input=input()+'$'
try:
stack=['0']
a=list(table.items())
'''print(a[int(stack[-1])][1][Input[0]])
b=list(a[int(stack[-1])][1][Input[0]])
print(b[0][0])
print(a[0][1]["S"])'''
print("productions\t:",production_list)
print('stack',"\t \t\t \t",'Input')
print(*stack,"\t \t\t \t",*Input,sep="")
while(len(Input)!=0):
b=list(a[int(stack[-1])][1][Input[0]])
if(b[0][0]=="s" ):
#s=Input[0]+b[0][1:]
stack.append(Input[0])
stack.append(b[0][1:])
Input=Input[1:]
print(*stack,"\t \t\t \t",*Input,sep="")
elif(b[0][0]=="r" ):
59
Shivam Bindal (BT19CSE008)
s=int(b[0][1:])
#print(len(production_list),s)
l=len(production_list[s])-3
#print(l)
prod=production_list[s]
l*=2
l=len(stack)-l
stack=stack[:l]
s=a[int(stack[-1])][1][prod[0]]
#print(s,b)
stack+=list(prod[0])
stack.append(s)
print(*stack,"\t \t\t \t",*Input,sep="")
elif(b[0][0]=="a"):
print("\n\tString Accepted\n")
break
except:
print('\n\tString INCORRECT for given Grammar!\n')
return
if __name__=="__main__":
main()
Output :
60
Shivam Bindal (BT19CSE008)
61