Expression parsing - recursive subroutine method

Expression syntax analysis - recursive subroutine method
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description

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:
E→TG
G→+TG | ε
T→FS
S→*FS | ε
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 sequence number of the production used in the derivation (starting from 0), 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.
For example:
i+i*i is an expression that the grammar can generate, and 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

i@i is not an expression that can be generated by the grammar. There are 5 lines in the output. The first 5 lines are separated by spaces, indicating the serial number of the production (starting from 0) used in the derivation. The last line is error, indicating i@ i is not an expression that the grammar can generate. @ is not a legal grammatical symbol, example output format:
0 E–>TG
1 T–>FS
2 F–>i
3 S–>&
4 G–>&
error

(i+i*i is not an expression that can be generated by the grammar, there is a syntax error that the parentheses do not match, the output format example:
0 E–>TG
1 T–>FS
2 F–>(E)
3 E–>TG
4 T –>FS
5 F–>i
6 S–>&
7 G–>+TG
8 T–>FS
9 F–>i
10 S–>*FS
11 F–>i
12 S–>&
13 G–>&
error
Input

The input data has only one line, which represents the symbol string to be analyzed, and ends with a # sign
Output

Output all productions during derivation, given in the order of use. See the example in the title description for a detailed description of the output.
Sample Input

i+i*i#
Sample Output

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<algorithm>
#include<stack>
using namespace std;
int num;
stack<char> A;
stack<char> B;
int f(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 !='*')//保持不变,E题不同
    {
       cout<<num<<" "<<"S-->&"<<endl;
        A.pop();
        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 == 'F' && y == '(')
    {
         cout<<num<<" "<<"F-->(E)"<<endl;
        A.pop();
        A.push(')');
        A.push('E');
        A.push('(');
        num++;
        return 1;
    }
    else
    {
         if(x == y)
        {

             A.pop();
             B.pop();
            return 1;
        }
        else
        {

            return 0;
        }
    }
}
void reset()
{
    num = 0;
    while(!A.empty())
    {
      A.pop();
    }
    A.push('#');
    A.push('E');
    while(!B.empty())
    {
        B.pop();
    }

}
int main()
{
    string  s;
    while(cin>>s)
    {
        reset();
        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 = f(A.top(),B.top());
                if(key == 0)
                {
                    cout<<"error"<<endl;
                    break;
                }
            }
        }

    }
    return 0;
}

Guess you like

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