Compilation principle (expression syntax analysis - recursive subroutine method)

The recursive subroutine method is a deterministic top-down parsing method that requires the grammar to be an LL(1) grammar. Its realization idea is to write a recursive process corresponding to each non-terminal symbol in the grammar. The function of each process is to identify the string deduced by the non-terminal symbol. When there are multiple candidates for the production of a non-terminal symbol, it can press The LL(1) form uniquely determines the selection of a candidate for derivation. Please construct a recursive subroutine according to the following expression LL(1) grammar to complete the syntax analysis of the expression.

The expression grammar is as follows:

    ETG

    G +TG | ε

    TFS

    S * FS | e

    F(E) | i


For a given input string (with a length of no more than 50 symbols), please output all the production expressions used in the analysis process, and indicate whether the input string is an expression that can be generated by the grammar. The
output is 11 lines in total, the first 10 lines The two data in each line are separated by spaces, indicating the production sequence number (starting from 0 ) used in derivation, and the last line is accept , indicating that i+i*i is a legal expression that can be generated by the grammar. Note: The & symbol represents the epsilon symbol in the grammar.
E.g:

i+i*i is an expression that the grammar can generate. The output format is as follows:

0 E -> TG

1 T-->FS

2 F-->i

3 S-->&

4 G-->+TG

5 T-->FS

6 F-->i

7 S-->*FS

8 F-->i

9 S-->&

10 G-->&

accept

#include<stdio.h>
#include<string>
#include<iostream>
#include<stack>
using namespace std;
stack<char> A;
stack<char> B;
int num;
void reset()
{
   num=0;
   while(!A.empty())
   {
     A.pop();
   }
   while(!B.empty())
   {
       B.pop();
   }
   A.push('#');
   A.push('E');
}
int panduan(char x,char y){
    if(x=='E')
    {
        cout<<num<<" "<<"E-->TG"<<endl;
        A.pop();
        A.push('G');
        A.push('T');
        num++;
        return 1;
    }
    else if(x=='G'&&y=='+')
    {
        cout<<num<<" "<<"G-->+TG"<<endl;
        A.pop();
        A.push('G');
        A.push('T');
        A.push('+');
        num++;
        return 1;
    }
    else if(x=='G'&&y!='+')
    {
        cout<<num<<" "<<"G-->&"<<endl;
        A.pop();
        num++;
        return 1;
    }
    else if(x=='T')
    {
         cout<<num<<" "<<"T-->FS"<<endl;
         A.pop();
         A.push('S');
         A.push('F');
         num++;
         return 1;
    }
    else if(x=='S'&&y=='*')
    {
        cout<<num<<" "<<"S-->*FS"<<endl;
        A.pop();
        A.push('S');
        A.push('F');
        A.push('*');
        num++;
        return 1;
    }
    else if(x=='S'&&y!='*')
    {
        cout<<num<<" "<<"S-->&"<<endl;
        A.pop();
        num++;
        return 1;
    }
    else if(x=='F'&&y=='(')
    {
        cout<<num<<" "<<"F-->(E)"<<endl;
        A.pop();
        A.push(')');
        A.push('E');
        A.push('(');
        num++;
        return 1;
    }
    else if(x=='F'&&y=='i')
    {
        cout<<num<<" "<<"F-->i"<<endl;
        A.pop();
        A.push('i');
        num++;
        return 1;
    }
    else
    {
        if(x==y)
        {
            A.pop();
            B.pop();
            return 1;
        }
        else
        {
            return 0;
        }
    }

}
intmain()
{
    string s;
    while(cin>>s)
    {

       reset();//If there was data in the stack last time it was emptied
       int len=s.length();
       for(int i=len-1;i>=0;i--)
       {
           B.push(s[i]);
       }
       while(1){
           if(A.top()=='#'&&B.top()=='#')
           {
               cout<<"accept"<<endl;
               break;
           }
          else
            {
                int key = panduan(A.top(),B.top());
                if(key == 0)
                {
                    cout<<"error"<<endl;
                    break;
                }
            }
       }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324730547&siteId=291194637