如果是我的学弟学妹大抵都会看到csteaching里的那封pdf所以我就不多说了
然后第一步是把那一堆符号变成你熟悉的模样,如下所示(有的符号间空格本不应存在但为了方便写程序时照着看我就加了空格,可以对照老师给的pdf删除空格,千万对照一下,我差点把我自己坑了,然后如果老师说输入是实验一的输入我只能说,用实验一的函数不用实验一的输入咱成吗,真的麻烦耶老师)
p {dl sl}
dl dl ds |*
ds int id;
sl sl s |*
s is |whs |fs |rs |wrs |cs |es
is if (e) s [else s] #方框为可有可无
whs while(e) s
fs for(e ;e ;e )s
wrs write e
rs read ID
cs {sl }
es e; |;
e ID=be |be
be ae |ae (> |< |>= |<= |== |!= )ae
ae t{(+ |- )t}
t f{(* |/ )f}
f. (e)|ID|NUM
拆解成这样之后,从下往上写,底层f()到顶层p(),这个逻辑也好理解。
#include<iostream>
#include<string>
#include<stack>
#include</Users/apple/Desktop/实验汇总/编译原理实验/实验三/IDnum.cpp>
#define maxnum 100
using namespace std;
char Scanin[maxnum]="{int i;int n;int j;j=1;read n;for(i=1;i<=n;i=i+1)j=j*i;write j;}";
int len=strlen(Scanin);
stack<char>scanin_left;//输入栈
void showstack(){
stack<char>tempscan;
while(!scanin_left.empty()){
tempscan.push(scanin_left.top());
cout<<scanin_left.top();
scanin_left.pop();
}
cout<<endl;
while(!tempscan.empty()){
scanin_left.push(tempscan.top());
tempscan.pop();
}
}
bool sl();
bool s();
bool e();
bool f(){//f->(e)|ID|NUM
if (scanin_left.top()=='('){
scanin_left.pop();
if(e()){
if(scanin_left.top()==')'){
scanin_left.pop();
return true;
}
else{
cout<<"缺少左括号"<<endl;
return false;
}
}
return false;
}
else {
if (ifid() or ifnum()){
return true;
}
return false;
}
}
bool t(){//t->f{(* |/ )f}
if(f()){
if(scanin_left.top()=='*' or scanin_left.top()=='/'){
scanin_left.pop();
if(f()){
return true;
}
cout<<"符号后语句不完整"<<endl;
return false;
}
return true;
}
return false;
}
bool ae(){//Ae->t{(+ |- )t}
if(t()){
if(scanin_left.top()=='+' or scanin_left.top()=='-'){
scanin_left.pop();
if (t()){
return true;
}
cout<<"符号后语句不完整"<<endl;
return false;
}
return true;
}
return false;
}
bool be(){//Be->ae |ae (> |< |>= |<= |== |!= )ae
if(ae()){
if(scanin_left.top()=='>' or scanin_left.top()=='<' ){
scanin_left.pop();
if(scanin_left.top()=='='){
scanin_left.pop();
}
if(ae()){
return true;
}
cout<<"符号后语句不完整"<<endl;
return false;
}
else if (scanin_left.top()=='=' or scanin_left.top()=='!'){
scanin_left.pop();
if(scanin_left.top()=='='){
scanin_left.pop();
if(ae()){
return true;
}
cout<<"符号后语句不完整"<<endl;
return false;
}
cout<<"符号不完整"<<endl;
return false;
}
return true;
}
return false;
}
bool e(){//e->ID=be |be
if(ifid()){
if(scanin_left.top()=='='){
scanin_left.pop();
if(scanin_left.top()=='='){
if(ae()){
if(scanin_left.top()==';'){
return true;
}
}
return false;
}
if(be()){
if(scanin_left.top()==';' or scanin_left.top()==')'){
return true;
}
}
return false;
}
else if(scanin_left.top()=='>' or scanin_left.top()=='<' ){
scanin_left.pop();
if(scanin_left.top()=='='){
scanin_left.pop();
}
if(ae()){
if(scanin_left.top()==';'){
return true;
}
return false;
}
cout<<"符号后语句不完整"<<endl;
return false;
}
if(scanin_left.top()==';'){
return true;
}
return false;
}
else if(be()){
if(scanin_left.top()==';'){
return true;
}
}
return false;
}
bool es(){//Es->e; |;
if(scanin_left.top()==';'){
scanin_left.pop();
return true;
}
else if (e()){
if(scanin_left.top()==';'){
scanin_left.pop();
return true;
}
cout<<"缺少;"<<endl;
return false;
}
return false;
}
bool cs(){//Cs->{sl}
if (scanin_left.top()=='{'){
scanin_left.pop();
if(sl()){
if(scanin_left.top()=='}'){
return true;
}
cout<<"缺少左括号"<<endl;
return false;
}
return false;
}
cout<<"缺少右括号"<<endl;
return false;
}
bool rs(){
scanin_left.pop();
if(es()){
return true;
}
cout<<"请跟有效变量"<<endl;
return false;
}
bool wrs(){
scanin_left.pop();
if(e()){
if(scanin_left.top()==';'){
scanin_left.pop();
return true;
}
return false;
}
cout<<"请跟有效变量"<<endl;
return false;
}
bool fs(){//Fs->for(e;e;e)s
if(scanin_left.top()=='('){
scanin_left.pop();
if(e()){
if (scanin_left.top()==';'){
scanin_left.pop();
if(e()){
if (scanin_left.top()==';'){
scanin_left.pop();
if(e()){
if(scanin_left.top()==')'){
scanin_left.pop();
if (s()){
return true;
}
return false;
}
cout<<"缺少右括号"<<endl;
return false;
}
return false;
}
cout<<"for函数格式不全"<<endl;
return false;
}
return false;
}
cout<<"for函数格式不全"<<endl;
return false;
}
return false;
}
return false;
}
bool whs(){//Whs ->while(e) s
if(!scanin_left.empty()){
scanin_left.pop();
if(e()){
if(!scanin_left.empty()){
if (scanin_left.top()==')'){
if (s()){
return true;
}
return false;
}
cout<<"缺少右括号"<<endl;
return false;
}
return false;
}
return false;
}
return false;
}
bool ifs(){//Is->if(e)s [else s]
if(scanin_left.top()=='('){
scanin_left.pop();
if(e()){
if(scanin_left.top()==')'){
scanin_left.pop();
if(s()){
if (scanin_left.top()==' '){
char temp[maxnum];//存放现有的数组
int temp_num=0;//现有数组长度为0
while(!scanin_left.empty() and isalpha(scanin_left.top())){
temp[temp_num]=scanin_left.top();
scanin_left.pop();
}
if(strcmp("else",temp)==0){
if(scanin_left.top()==' '){
scanin_left.pop();
if(s()){
return true;
}
return false;
}
cout<<"语句不完整"<<endl;
return false;
}
return true;
}
return true;
}
}
}
}
return false;
}
bool s(){//s—>is |whs |fs |rs |wrs |cs |es,判断开头的字符串是if,while,for,read,write,{,否则就是es
char temp[maxnum];//存放现有的数组
int temp_num=0;//现有数组长度为0
if (scanin_left.top()=='{'){
if(cs()){
return true;
}
return false;
}
while(!scanin_left.empty()){
if(!isalpha(scanin_left.top())){
if (strncmp("read",temp,temp_num)==0){
if(rs()){
return true;
}
return false;
}
else if (strncmp("write",temp,temp_num)==0){
if(wrs()){
return true;
}
return false;
}
else if (strcmp("while",temp)==0){
if(whs()){
return true;
}
return false;
}
else if (strcmp("for",temp)==0){
if(fs()){
return true;
}
return false;
}
else if (strcmp("if",temp)==0){
if(ifs()){
return true;
}
return false;
}
else{
for (int i=temp_num-1;i>=0;i--){//跳转结束,将这些都放回去
scanin_left.push(temp[i]);
}
if(es()){
return true;
}
return false;
}
}
else{
temp[temp_num]=scanin_left.top();
temp_num++;
scanin_left.pop();
}
}
return false;
}
bool sl(){//Sl->sl s |*
while(scanin_left.top()!='}'){
if(!s()){
return false;
}
}
return true;
}
bool ds(){//Ds->int id
if(scanin_left.top()==' '){
scanin_left.pop();
if(ifid()){
if(scanin_left.top()==';'){
scanin_left.pop();
return true;
}
cout<<"缺少;"<<endl;
return false;
}
cout<<"变量名不规范"<<endl;
return false;
}
return false;
}
bool dl(){//Dl dl ds |*
char temp[maxnum];
int temp_num=0;
while(!scanin_left.empty()){
if(!isalpha(scanin_left.top())){
break;
}
else{
temp[temp_num]=scanin_left.top();
temp_num++;
scanin_left.pop();
}
}
if(strncmp(temp,"int",temp_num)==0){
if (ds()){
return dl();
}
return false;
}
for(int i=temp_num-1;i>=0;i--){
scanin_left.push(temp[i]);
}
return true;
}
bool p(){//E {dl sl}
if(scanin_left.top()=='{'){
scanin_left.pop();
if(dl()){
if(sl()){
if(scanin_left.top()=='}'){
return true;
}
}
}
}
return false;
}
int main(){
for (int i=len-1;i>=0;i--){
scanin_left.push(Scanin[i]);
}
cout<<p()<<endl;
}
以上就是全部代码(但是不包括实验一的函数,实验一函数你们都会写的吧。。。我觉得实验一老师还是很良心的,所有思路都齐了,就差手把手教你写代码了)