编译原理 LR

论坛 期权论坛 脚本     
匿名技术用户   2020-12-27 02:29   11   0
#include<bits/stdc++.h> 
using namespace std;

typedef pair<string,string> P;
P p[12];

struct AA{
 char c;
 string s;
 int id;
};

vector<AA> v[40];

int main(){
 ifstream in("Grammer.txt");
 int tot=0;
 {
  string s,t;
  while(in>>s>>t)
   p[tot++]=make_pair(s,t);
 } 
 //cout<<tot<<endl;
 ifstream in2("LR_table.txt");
 //freopen("LR_table.txt","r",stdin);
 int n;
 in2>>n;
 {
  int cnt,id;
  char c;
  string s;
  while(in2>>cnt>>c>>s>>id){
   v[cnt].push_back(AA{c,s,id});
  // cout<<cnt<<" "<<c<<" "<<s<<" "<<id<<endl; 
  }
 }
 //freopen("Test1.in","r",stdin);
 freopen("Test1.in","r",stdin);
 string buf;
 {
  char c[100];
  while(gets(c)!=NULL){
   buf+=string(c)+'#'; 
  }
 }
 //cout<<buf<<endl;
 stringstream sin(buf);
 char c;
 char cuf[100];
 int csz=0;
 
 {
  string temp;
  int pre=0;
  sin>>noskipws;
  while(sin>>c){
   if(c==' '){
    if(temp=="")continue;
    else{
     cuf[csz++]='i';
     //cuf+='i';
     pre=0;
     temp="";
    }
   }else if(c=='='||c==';'||c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='#'){
    if(temp=="");
    else{
     cuf[csz++]='i';
     //cuf+='i';
     pre=0;
     temp="";
    }
    cuf[csz++]=c;
    //cuf+=c;
   }else if(isdigit(c)){
    if(pre==0)pre=1;
    temp+=c;
   }else if(isalpha(c)||c=='_'){
    if(pre==0){
     pre=2;
    }
    else if(pre==1){
     cuf[csz++]='i';
     //cuf+='i';
     temp=""; 
     pre=2;
    }
    temp+=c;
   }
 //  cout<<"c: "<<c<<endl;
 //  cout<<"cuf: "<<cuf<<endl; 
  }
 }
 cuf[csz]='\0';
// cout<<buf<<endl;
// cout<<cuf<<endl;
 puts(cuf);
 
 int st[50],stsz=0;
 st[stsz++]=0; //状态栈 
 int cnt=2;
 char wuf[100]; //规约栈 
 int wsz=0;
 wuf[wsz++]='#';
 for(int z=0;z<csz;z++){
  c=cuf

编译原理 LR

#include&lt;bits/stdc++.h&gt;
using namespace std;
typedef pair&lt;string,string&gt; P;
P p[12];
struct AA{
char c;
string s;
int id;
};
vector&lt;AA&gt; v[40];
int main(){
ifstream in("Grammer.txt");
int tot=0;
{
string s,t;
while(in&gt;&gt;s&gt;&gt;t)
p[tot++]= ...查看全文
匿名技术用户 发表于 2020-12-27 02:29 
; // if(z==csz-1){ // stsz=1; // puts("success"); // } if(stsz==0){ puts("gg!!!wrong"); break; } int cur=st[stsz-1]; int flag=0,id=-1; for(int i=0;i<v[cur].size();i++){ AA tp=v[cur][i]; if(tp.c==c){ if(tp.s[0]=='P'){ wuf[wsz++]=c; //wuf+=c; st[stsz++]=tp.id; flag=1; break; }else if(tp.s[0]=='S'){ flag=2; id=tp.id; int sz=p[id].second.size(); if(sz>stsz||sz>wsz){ flag=-1;break; } stsz-=sz; wsz-=sz; wuf[wsz++]=p[id].first[0]; cur=st[stsz-1]; c=wuf[wsz-1]; // cout<<cur<<endl; // cout<<c<<endl; bool tflag=1; for(int j=0;j<v[cur].size();j++){ AA tp=v[cur][j]; if(tp.c==c){ if(tp.s[0]=='G'){ st[stsz++]=tp.id; tflag=0; } } } if(tflag==1){ puts("error2"); break; } }else if(tp.s[0]=='A'){ stsz-=1; wsz-=1; flag=3; } } } if(flag==3){ if(z==csz-1){ puts("success"); break; } } if(flag==0){ puts("error");break; } if(flag==-1){ puts("wrong");break; } printf("%d:\n",cnt); printf("statestack:\n"); for(int i=0;i<stsz;i++) printf("%d ",st[i]); puts(""); if(flag==2)z--; printf("input_string: "); for(int i=z+1;i<csz;i++) putchar(cuf[i]); puts(""); printf("sumupstack:\n"); for(int i=0;i<wsz;i++) putchar(wuf[i]); puts(""); if(flag==2){ puts("production: "); cout<<p[id].first<<"->"<<p[id].second<<endl; } cnt++; puts(""); } return 0; }

grammer

S X
X i=E;
E E+T
E E-T
E T
T T*F
T T/F
T F
F (E)
F i

LR_table

137
0 X Goto 1
0 i Push 2
1 # Accept 0
2 = Push 3
3 E Goto 4
3 T Goto 5
3 F Goto 6
3 ( Push 7
3 i Push 8
4 ; Push 9
4 + Push 10
4 - Push 11
5 * Push 12
5 / Push 13
5 ; Sumup 4
5 + Sumup 4
5 - Sumup 4
6 ; Sumup 7
6 + Sumup 7
6 - Sumup 7
6 * Sumup 7
6 / Sumup 7
7 E Goto 14
7 T Goto 15
7 F Goto 16
7 ( Push 17
7 i Push 18
8 ; Sumup 9
8 + Sumup 9
8 - Sumup 9
8 * Sumup 9
8 / Sumup 9
9 # Sumup 1
10 T Goto 19
10 F Goto 6
10 ( Push 7
10 i Push 8
11 T Goto 20
11 F Goto 6
11 ( Push 7
11 i Push 8
12 F Goto 21
12 ( Push 7
12 i Push 8
13 F Goto 22
13 ( Push 7
13 i Push 8
14 ) Push 23
14 + Push 24
14 - Push 25
15 * Push 26
15 / Push 27
15 ) Sumup 4
15 + Sumup 4
15 - Sumup 4
16 ) Sumup 7
16 + Sumup 7
16 - Sumup 7
16 * Sumup 7
16 / Sumup 7
17 E Goto 28
17 T Goto 15
17 F Goto 16
17 ( Push 17
17 i Push 18
18 ) Sumup 9
18 + Sumup 9
18 - Sumup 9
18 * Sumup 9
18 / Sumup 9
19 * Push 12
19 / Push 13
19 ; Sumup 2
19 + Sumup 2
19 - Sumup 2
20 * Push 12
20 / Push 13
20 ; Sumup 3
20 + Sumup 3
20 - Sumup 3
21 ; Sumup 5
21 + Sumup 5
21 - Sumup 5
21 * Sumup 5
21 / Sumup 5
22 ; Sumup 6
22 + Sumup 6
22 - Sumup 6
22 * Sumup 6
22 / Sumup 6
23 ; Sumup 8
23 + Sumup 8
23 - Sumup 8
23 * Sumup 8
23 / Sumup 8
24 T Goto 29
24 F Goto 16
24 ( Push 17
24 i Push 18
25 T Goto 30
25 F Goto 16
25 ( Push 17
25 i Push 18
26 F Goto 31
26 ( Push 17
26 i Push 18
27 F Goto 32
27 ( Push 17
27 i Push 18
28 ) Push 33
28 + Push 24
28 - Push 25
29 * Push 26
29 / Push 27
29 ) Sumup 2
29 + Sumup 2
29 - Sumup 2
30 * Push 26
30 / Push 27
30 ) Sumup 3
30 + Sumup 3
30 - Sumup 3
31 ) Sumup 5
31 + Sumup 5
31 - Sumup 5
31 * Sumup 5
31 / Sumup 5
32 ) Sumup 6
32 + Sumup 6
32 - Sumup 6
32 * Sumup 6
32 / Sumup 6
33 ) Sumup 8
33 + Sumup 8
33 - Sumup 8
33 * Sumup 8
33 / Sumup 8

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP