表达式语法分析——递归子程序法

表达式语法分析——递归子程序法

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
 

递归子程序法是一种确定的自顶向下语法分析方法,要求文法是LL(1)文法。它的实现思想是对应文法中每个非终结符编写一个递归过程,每个过程的功能是识别由该非终结符推出的串,当某非终结符的产生式有多个候选式时能够按LL(1)形式唯一地确定选择某个候选式进行推导。请根据下面的表达式LL(1)文法,构造递归子程序,完成对表达式的语法分析。

表达式文法如下:

    ETG

    G+TG | ε

    TFS

    S→*FS | ε

    F(E) | i


对于给定的输入串(长度不超过50个符号),请输出分析过程中用到的所有产生式,并指明该输入串是否为该文法能生成的表达式,输出共11行,前10行每行两个数据用空格隔开,表示推导时所用产生式顺序号(从0开始),最后一行是accept,表示i+i*i是文法能生成的合法表达式。注:其中&符号代表文法中的ε符号。
例如:

i+i*i是文法能生成的一个表达式,输出格式如下:

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不是文法能生成的表达式,输出共5行,前5行每行两个数据用空格隔开,表示推导时所用产生式序号(从0开始),最后一行是error,表示i@i不是文法能生成的表达式。@不是合法的文法符号,输出格式举例:

0 E-->TG

1 T-->FS

2 F-->i

3 S-->&

4 G-->&

error

 

(i+i*i不是文法能生成的表达式,存在括号不匹配的语法错误,输出格式举例:

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
 

输入数据只有一行,代表待分析的符号串,以#号结束

Output
 

输出推导过程中所有的产生式,按照使用顺序给出。输出详细说明见题目描述中的例子。

Example Input
i+i*i#
Example Output
0 E-->TG1 T-->FS2 F-->i3 S-->&4 G-->+TG5 T-->FS6 F-->i7 S-->*FS8 F-->i9 S-->&10 G-->&accept
Hint
 
Author


#include<bits/stdc++.h>
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 !='*')
    {
        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;
    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;
}



#include <iostream>
#include <string>

using namespace std;

int main()
{
    string a,b;
    b="E";
    getline(cin,a);
    a.erase(a.length()-1,1);
    int j=0;
    int t=-1;
    for (int i=0;i<b.length();i++)
    {
        if (b[i]=='E')
        {
            t++;
            b.replace(i,1,"TG");
            i--;
            cout<<t<<" E-->TG"<<endl;
        }
        else if(b[i]=='T')
        {
            t++;
            b.replace(i,1,"FS");
            i--;
            cout<<t<<" T-->FS"<<endl;
        }
        else if (b[i]=='F')
        {
            if (a[j]=='i')
            {
                t++;
                b.replace(i,1,"i");
                j++;
                cout<<t<<" F-->i"<<endl;
            }
            else if (a[j]=='(')
            {
                t++;
                b.replace(i,1,"(E)");
                j++;
                cout<<t<<" F-->(E)"<<endl;
            }
            else
                break;
        }
        else if(b[i]=='S')
        {
            if (a[j]=='*')
            {
                t++;
                b.replace(i,1,"*FS");
                j++;
                cout<<t<<" S-->*FS"<<endl;
            }
            else
            {
                t++;
                b.erase(i,1);
                i--;
                cout<<t<<" S-->&"<<endl;
            }
        }
        else if(b[i]=='G')
        {
            if (a[j]=='+')
            {
                t++;
                b.replace(i,1,"+TG");
                j++;
                cout<<t<<" G-->+TG"<<endl;
            }
            else
            {
                t++;
                b.erase(i,1);
                i--;
                cout<<t<<" G-->&"<<endl;
            }
        }
        else if(b[i]==')')
        {
            if (a[j]!=')')
                break;
            else
                j++;
        }

    }
    if (a==b)
        cout<<"accept"<<endl;
    else
        cout<<"error"<<endl;
    return 0;
}


此代码非本人所写,本人不承担任何责任,侵删
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值