Home > Back-end >  SLR (1) grammar analysis experiment report the answer
SLR (1) grammar analysis experiment report the answer

Time:10-31

 
/*
SLR (1) grammar analysis
*/
//li hui, 2017/12/12

#include
#include
#include
using namespace std;

/*
The definition of stack
*/
Typedef struct StackNode {
The string data.
Struct StackNode * next;
} StackNode, * LinkStack;


/*
Function declaration
*/
Bool InitStack (LinkStack & amp; S);
Bool Push (LinkStack & amp; S, string e);
Bool Pop (LinkStack & amp; S, string & amp; e);
String GetTop (LinkStack S);
String out LinkStack (S);
Int GetNum (LinkStack S);
Bool fenxi ();
Int isT (string s);
Int isN (string s);
Int isZ (string s);
The string change (char a);
Int yu (string s);
String Chan (string s);


/*
Expressions, analysis table (ACTION list, and GOTO table)
*/
String BD [7]={" S - & gt; E ", "E - & gt; E + T ", "E - & gt; T ", "T - & gt; T * F ", "T - & gt; F ", "F - & gt; (E) ", "F - & gt; I "};
The string ACTION [12] [6].
String GOTO [12] [3].


Void main () {

//initialization of an analysis table
for (int i=0; i <12. I++) {
for (int j=0; J & lt; 6; J++) {
The ACTION [I] [j]="";
}
For (int m=0; M & lt; 3; M++) {
GOTO [I] [m]="";
}
}
//assignment
The ACTION [0] [0]="s5"; The ACTION [0] [3]="s4";
The ACTION [1] [1]="s6"; The ACTION [1] [5]="acc";
The ACTION [2] [1]="r2"; The ACTION [2] [2]="r7"; The ACTION [2] [4]="r2"; The ACTION [2] [5]="r2";
The ACTION [3] [1]="r4"; The ACTION [3] [2]="r4"; The ACTION [3] [4]="r4"; The ACTION [3] [5]="r4";
The ACTION [4] [0]="s5"; The ACTION [4] [3]="s4";
The ACTION [5] [1]="r6"; The ACTION [5] [2]="r6"; The ACTION [5] [4]="r6"; The ACTION [5] [5]="r6";
The ACTION [6] [0]="s5"; The ACTION [6] [3]="s4";
The ACTION [7] [0]="s5"; The ACTION [7] [3]="s4";
The ACTION [8] [1]="s6"; The ACTION [8] [4]="s4";
The ACTION [9] [1]="r1"; The ACTION [9] [2]="s7"; The ACTION [9] [4]="r1"; The ACTION [9] [5]="r1";
The ACTION [10] [1]="r3"; The ACTION [10] [2]="r3"; The ACTION [10] [4]="r3"; The ACTION [10] [5]="r3";
The ACTION [11] [1]="r5"; The ACTION [11] [2]="r5"; The ACTION [11] [4]="r5"; The ACTION [11] [5]="r5";


GOTO [0] [0]="1"; GOTO [0] [1]="2"; GOTO [0] [2]="3";
GOTO [3] [0]="8"; GOTO [3] [1]="2"; GOTO [3] [2]="3";
GOTO [6] [1]="9"; GOTO [6] [2]="3";
GOTO [7] [2]="10";

cout
//output
cout <"Input grammar (this method has been extended) :" & lt; cout <"S - & gt; E E - & gt; E + T \ n E - & gt; T T - & gt; T * F \ n T - & gt; F - F & gt; (E) \ n F - & gt; I "& lt; cout <"UT: +, *, (,), I" & lt; cout <"The UN: S, E, T, F" & lt;
cout <"With each non-terminal FOLLOW set:" & lt; cout <"FOLLOW (S)=# {};" cout <"FOLLOW (E)={#, +)};" cout <"FOLLOW (T)={#, +), *};" cout <"FOLLOW (F)={#, +), *};"


cout <"This method analysis table as shown in figure:" & lt; cout <"I + * () # E T F" & lt; for (int i=0; i <12. I++) {
cout for (int j=0; J & lt; 6; J++) {
cout
If (j==5) {
For (int m=0; M & lt; 3; M++) {
cout }
cout }
}

}

Bool flag=false;
Flag=fenxi ();

If (flag==true) {
cout }
The else {
cout }



}



Bool fenxi () {
Bool flag=false;
cout <"Input # at the end of the string:" & lt; String juzi="";
Cin & gt;> Juzi.
Int juzi_len=juzi. Length ();

//define two stacks, grammar symbols and the corresponding state stack
LinkStack F;//symbol stack
LinkStack Z;//state stack
InitStack (F);//initialize
InitStack (Z);//initialize

cout <"Output process is as follows:" & lt; cout <& lt;" Step status symbol stack input string ACTION GOTO "& lt;
Push (Z, "0");
Push (F, "#");
//start
int j=0;
int i=0;
While (i//output section
cout cout cout cout

String fir=change (juzi. At (I));
Int a=isZ (GetTop (Z));
Int b=isT (fir);
String move=the ACTION [a] [b];
If (move) at (0)=='s') {

//output section
cout
//move
String z=change (move) at (1));
Push (F, fir);
Push (Z, Z);
i++;
}
If (move) at (0)=='r') {
//reduce
String z=change (move) at (1));//state
String db=BD [(z.a t (0) - '0')];
Int x=yu (db);//production right several




//all the stack
For (int q=0; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related