【编译原理12】预测分析法分析文法符号串

Problem Description

文法G[S]:
S→Ap
S→Bq
A→ a
A→cA
B→ b
B→dB
试编写一个预测分析程序, 判断文法G所能接受的串。
Input 输入多行由终止符构成的符号串,输入EOF结束。
Output 每行输入的符号串在语法结构上如果合法,输出"syntax correct";否则输出"syntax error"。

Sample Input
ccap#
cccccccccccap#
Sample Output
syntax correct syntax
correct

C++代码实现

//
// Created by Visunf Chen on 2020-10-25.
//
#include "iostream"

using namespace std;

string str;
int flag=1;

void S(string s);
void A(string s);
void B(string s);

void S(string string1){
    str = string1;
    A(str);
    if (str[0]=='p')str = str.substr(1,str.length());
    B(str);
    if (str[0]=='q')str = str.substr(1,str.length());
}

void A(string string1){
    str = string1;
    if (str[0]=='a')str = str.substr(1,str.length());
    else if(str[0]=='c'){
        str = str.substr(1,str.length());
        A(str);
    }
}

void B(string string1){
    str = string1;
    if (str[0]=='b')str = str.substr(1,str.length());
    else if(str[0]=='d'){
        str = str.substr(1,str.length());
        B(str);
    } 
}

int main(){
    while (cin>>str&&str!="EOF"){
        S(str);
        if (str[0]=='#'&&flag==1)cout<<"syntax correct"<<endl;
        else cout<<"syntax error"<<endl;
    }
    return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值