create a c++ code for all possible states for bottom parsing for the following grammar expression:
E
E->E+T
E->T
T->T*F
T->F
F->(E)
F->i
# include <iostream.h>
# include <string.h>
# include <conio.h>
staticchar Stack[50][10]={NULL};
staticint top=-1;
staticint cit=0;
// Input Grammarstaticint productions[6]={5,1,7,7,2,10};
constchar Grammar[5][11][10]={
{"S","E"},
{"E","E+T","E-T","E*T","E/T","E%T","E^T","T"},
{"T","T+F","T-F","T*F","T/F","T%F","T^F","F"},
{"F","(E)","D"},
{"D","0","1","2","3","4","5","6","7","8","9"}
};
// Input Statementconstint input_length=8;
char Input[input_length][5]={"2","*","(","3","+","4",")","$"};
/*************************************************************************///------------------------------
Push( )
------------------------------///*************************************************************************/void
Push(constchar* Token)
{
top++;
strcpy(Stack[top],Token);
}
/*************************************************************************///-------------------------------
Pop( )
------------------------------///*************************************************************************/void
Pop( )
{
strset(Stack[top],NULL);
top--;
}
/*************************************************************************///-----------------------------
Reduce( )
-----------------------------///*************************************************************************/void
Reduce(constint items,constint index)
{
for(int i=0;i<items;i++)
Pop( );
Push(Grammar[index][0]);
}
/*************************************************************************///------------------------------
Shift( )
-----------------------------///*************************************************************************/void
Shift( )
{
Push(Input[cit]);
cit++;
}
/*************************************************************************///-----------------------
CheckReduceCondition( )
---------------------///*************************************************************************/constint
CheckReduceCondition( )
{
int items;
int index;
char TopItems[100]={NULL};
for(int i=0;i<=top;i++)
{
strset(TopItems,NULL);
for(int j=i;j<=top;j++)
strcat(TopItems,Stack[j]);
for(j=0;j<productions[0];j++)
{
for(int k=1;k<=productions[(j+1)];k++)
{
if(strcmp(TopItems,Grammar[j][k])==0)
{
items=(top-i+1);
index=j;
goto NextCheck;
}
}
}
}
return 0;
NextCheck:
char CitInput[20]={NULL};
strcpy(CitInput,Stack[top]);
strcat(CitInput,Input[cit]);
for(i=0;i<productions[0];i++)
{
for(int j=1;j<=productions[(i+1)];j++)
{
if(strstr(Grammar[i][j],CitInput)!=NULL)
return 0;
}
}
Reduce(items,index);
return 1;
}
/*************************************************************************//*************************************************************************//*******************************
main( )
*******************************//*************************************************************************//*************************************************************************/int
main( )
{
clrscr( );
int flag=0;
cout<<" /////*****+++++-----..... Bottom-Up Parsing .....-----+++++*****/////";
gotoxy(5,3);
cout<<"Stack";
gotoxy(35,3);
cout<<"Input";
gotoxy(65,3);
cout<<"Next Action";
gotoxy(5,5);
for(int i=0;i<=top;i++)
cout<<Stack[i];
gotoxy(35,5);
for(int j=cit;j<input_length;j++)
cout<<Input[j];
gotoxy(65,5);
cout<<"Shift";
do
{
if(!CheckReduceCondition( ))
{
Shift( );
gotoxy(65,(wherey( )+1));
cout<<"Shift";
}
else
{
gotoxy(65,(wherey( )+1));
cout<<"Reduce";
}
gotoxy(5,wherey( ));
for(int i=0;i<=top;i++)
cout<<Stack[i];
gotoxy(35,wherey( ));
for(int j=cit;j<input_length;j++)
cout<<Input[j];
if(top==0 && strcmp(Stack[top],Grammar[0][0])==0
&&
strcmp(Input[cit],"$")==0)
{
flag=1;
break;
}
elseif(strcmp(Stack[top],"$")==0)
{
flag=0;
break;
}
}
while(1);
if(flag)
cout<<"\n\n Input is Correct...";
else
cout<<"\n\n Input is Incorrect...";
getch( );
return 0;
}
create a c++ code for all possible states for bottom parsing for the following grammar expression:...
Q6) Consider the following grammar for arithmetic expressions. F ? (E) l i Using top-down parsing, find a leftmost derivation in this grammar for the expression i/i + . Show your work. 10 Points
Give the predictive parsing table for the following grammar: E -> T E’ (Do not forget to consider $) E’ -> + T E’ | e ( e stand for empty string) T -> F T’ T’ -> * F T’ | e F -> ( E ) | id | num
4.- [11 points] Give the predictive parsing table for the following grammar: (Do not forget to consider $) (ε stand for empty string) E → TE E' →+TE' E TFT T →*FT'TE F → (E) | id num
Build a LR parsing table for the following grammar: F → f
7- Show a complete LR(0) and SLR(1) parsers, including the canonical collection of LR(0) and parsing table, using the following grammar E-→ E + T / T T-, T F / F l a l b Is this grammar LR(0) or SLR(1)? Why? 7- Show a complete LR(0) and SLR(1) parsers, including the canonical collection of LR(0) and parsing table, using the following grammar E-→ E + T / T T-, T F / F l a l b Is...
NEED THIS SOON. Recursive Descent Parsing Consider the following BNF grammar: A -> I = E E -> P O P | P O -> + | - | * | / | ** P -> I | L | UI | UL | (E) U -> + | - | ! I -> C | CI C -> a | b | ... | y | z L -> D | DL D -> 0 | 1 | ......
Recursive Descent Parsing Consider the following BNF grammar: A -> I = E E -> P O P | P O -> + | - | * | / | ** P -> I | L | UI | UL | (E) U -> + | - | ! I -> C | CI C -> a | b | ... | y | z L -> D | DL D -> 0 | 1 | ... | 8 |...
(20 pts) Create an LR(O) parse table for the following grammar. Show all steps (creating closures, the DFA, the transition table, and finally the parse table): E->E+T E*T T T->(E) | id Show a complete bottom-up parse, including the parse stack contents, input string, and action for the string below using the parse table you created (id + id) * id Show a rightmost derivation for the string above, and show how the bottom-up parse you completed correctly finds all...
(10] Eliminate left recursion from the grammar A Ba |Aa c B Bb | Ab 1 d A Ad IB A BA ASJAE Consider the following grammar G: S'S S (S)S|e fa) (10] Construct the collection of the sets of LR(0) items (b) [5] When constructing the action table of SLR parser of G what are the rules to determine the parsing actions? That is, what is the rule for a shift action at state /? What is the rule...
Let G be the following grammar: 1. S T 2. T O 3. T T 4. O V = E i [ E ] 5. V i 6. V i 7. E ( E) 8. E Construct the LR(0) DFA for this grammar a) b) Construct the LR(0) parsing table. Is it LR(o)? Why and why not? Let G be the following grammar: 1. S T 2. T O 3. T T 4. O V = E i [ E...