Compilation Principle LR analysis C ++ implementation

Source: Internet
Author: User

# Include <iostream>

# Include <cstdlib>
# Include <string>
# Include <cstring>
Using namespace STD;
Const int size = 100;
Class Stack {
Int zt_data [size]; char fh_data [size];
Int top;
Public:
Stack ();
Void push_stack (int zt, char FH );
Void showstack ();
Void pop_stack ();
Char get_fh () {return fh_data [Top];}
Int get_zt () {return zt_data [Top];}
};
STACK: Stack () {zt_data [0] = 0; fh_data [0] = '#';
Top = 0;
}
Void Stack: push_stack (int zt, char FH ){
Top ++;
Zt_data [Top] = ZT; fh_data [Top] = FH;
Cout <"push_stack:" <ZT <'\ t' <FH <Endl;
}
Void Stack: pop_stack (){
Cout <"pop_stack:" <zt_data [Top] <'\ t' <fh_data [Top] <Endl;
Zt_data [Top] = 0; fh_data [Top] = NULL;
Top --;
}
Int main ()
{
String input;

While (1 ){
Cout <"Please input the string end with '#'" <Endl;
Cin> input;
If (input [input. Size ()-1] = '#') break;
Cout <"you put worry string ";
}
Stack LR;
String tab [12] [9];
Tab [0] [0] = "S5"; tab [0] [3] = "S4"; tab [0] [6] = "1 "; tab [0] [7] = "2"; tab [0] [8] = "3 ";
Tab [1] [1] = "S6"; tab [1] [5] = "ACC ";
Tab [2] [1] = "R2"; tab [2] [2] = "S7"; tab [2] [4] = "R2 "; tab [2] [5] = "R2 ";
Tab [3] [1] = "r4"; tab [3] [2] = "r4"; tab [3] [4] = "r4 "; tab [3] [5] = "r4 ";
Tab [4] [0] = "S5"; tab [4] [3] = "S4"; tab [4] [6] = "8 "; tab [4] [7] = "2"; tab [4] [8] = "3 ";
Tab [5] [1] = "R6"; tab [5] [2] = "R6"; tab [5] [4] = "R6 "; tab [5] [5] = "R6 ";
Tab [6] [0] = "S5"; tab [6] [3] = "S4"; tab [6] [7] = "9 "; tab [6] [8] = "3 ";
Tab [7] [0] = "S5"; tab [7] [3] = "S4"; tab [7] [8] = "10 ";
Tab [8] [1] = "S6"; tab [8] [4] = "S11 ";
Tab [9] [1] = "R1"; tab [9] [2] = "S7"; tab [9] [4] = "R1 "; tab [9] [5] = "R1 ";
Tab [10] [1] = "R3"; tab [10] [2] = "R3"; tab [10] [4] = "R3 "; tab [10] [5] = "R3 ";
Tab [11] [1] = "R5"; tab [11] [2] = "R5"; tab [11] [4] = "R5 "; tab [11] [5] = "R5 ";
Cout <"showthetab:" <Endl;
For (INT I = 0; I <12; I ++)
{
For (Int J = 0; j <9; j ++)
Cout <tab [I] [J] <"\ t ";
Cout <Endl;
}
String unend_char [6] = {"e + T", "T", "T * F", "F", "(E)", "I "};
Char unend_char_l [6] = {'E', 'E', 't', 't', 'F', 'F '};
Char * P = & input [0];
While (! ('E' = LR. get_fh () & (* P = '#')))
{
Int N;
Switch (* P ){
Case 'I': n = 0; break;
Case '+': n = 1; break;
Case '*': N = 2; break;
Case '(': n = 3; break;
Case ')': N = 4; break;
Case '#': n = 5; break;
Case 'E': n = 6; break;
Case 'T': n = 7; break;
Case 'F': n = 8; break;
}
Int M = LR. get_zt ();
String temp = tab [m] [N];
If (temp [0] ='s '){
Int ZT;
If (temp = "S0 ")
Zt = 0;
If (temp = "S1 ")
Zt = 1;
Else if (temp = "S2 ")
Zt = 2;
Else if (temp = "S3 ")
Zt = 3;
Else if (temp = "S4 ")
Zt = 4;
Else if (temp = "S5 ")
Zt = 5;
Else if (temp = "S6 ")
Zt = 6;
Else if (temp = "S7 ")
Zt = 7;
Else if (temp = "S8 ")
Zt = 8;
Else if (temp = "S9 ")
Zt = 9;
Else if (temp = "S10 ")
Zt = 10;
Else if (temp = "S11 ")
Zt = 11;
LR. push_stack (ZT, * P );
P ++;
}
Else if (temp [0] = 'R '){
Int r_temp;
If (temp = "R1 ")
R_temp = 1;
Else if (temp = "R2 ")
R_temp = 2;
Else if (temp = "R3 ")
R_temp = 3;
Else if (temp = "r4 ")
R_temp = 4;
Else if (temp = "R5 ")
R_temp = 5;
Else if (temp = "R6 ")
R_temp = 6;
String find_string = unend_char [r_temp-1];
For (INT I = 0; I <(INT) find_string.size (); I ++)
LR. pop_stack ();
Char fh_temp = unend_char_l [r_temp-1];
Int zt_temp = LR. get_zt ();

Int J;
Switch (fh_temp ){
Case 'I': J = 0; break;
Case '+': j = 1; break;
Case '*': J = 2; break;
Case '(': J = 3; break;
Case ')': J = 4; break;
Case '#': J = 5; break;
Case 'E': J = 6; break;
Case 'T': J = 7; break;
Case 'F': J = 8; break;
}
String zt_temp2 = tab [zt_temp] [J];
// Cout <"hahahahaha" <zt_temp2 <Endl;
Int zt_temp4 = atoi (zt_temp2.c_str ());
/* Char * zt_temp3 = const_cast <char *> (zt_temp2.c_str ());
Int zt_temp4 = atoi (zt_temp3 );*/
// Char zt_temp4 = char (zt_temp4 );
// Cout <zt_temp4 <Endl;
LR. push_stack (zt_temp4, fh_temp );

}

}
}

// For the first time, my code broke through 150 lines.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.