⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 语法分析.cpp

📁 用c++实现的编译步骤中的语法分析器
💻 CPP
字号:
#include <iostream>
#include <iomanip>  //只用于setw()函数
#include <fstream>
#include <cmath>
#include <string>
int p[1000],w=0,v=1,s=1;
using namespace std;
void push(int b){p[w]=b;w++;};
int pop(){return p[w++];};
void pusht(int b){w--;p[w]=b;};
int main();
void program();
void proghead();
void block();
void consexpl();
void consdefi();
void varexl();
void conssuff();
void vandefi();
void varsuff();
void procdefi();
void typeil();
void procedh();
void procsuff();
void assipro();
void sentence();
void suffix();
void ifsent();
void read();
void whilsent();
void idsuff();
void Write();
void compsent();
void Exprsuff();
void sentsuff();
void Conditio();
void termsuff();
void Express();
void term();
void Factsuff();
void argument();
void Factor();
void addoper();
void Muloper();
void respoper();
void program()
{
     w=0;
     proghead();
     block();
     if((pop()!=27)||(w!=s))v=0;
}
void proghead()
{
     if((pop()==1)&&(pop()==34)&&(pop()==28))return;
     else v=0,main();
}
void block()
{
     consexpl();
     varexl();
     procdefi();
     compsent();
}
void consexpl()
{
     int a=pop();
     if(a==2){consdefi();conssuff();if(pop()!=28)v=0,main();}
}
void consdefi()
{
     if((pop()==34)&&(pop()==20)&&(pop()==33))return;
     else v=0,main();
}
void conssuff()
{
     int a=pop();
     if(a==26){consdefi();conssuff();}
     else pusht(a);
}
void varexl()
{
     int a=pop();
     if(a==3){vandefi();varsuff();}
     else pusht(a);
}
void vandefi()
{
     int a=pop();
     if(a==34)
     {idsuff();
      if(pop()==29)
      {typeil();
       if(pop()==28)return;
      }
     }
     v=0,main();
}
void idsuff()
{
     int a=pop();
     if(a==26)
     {if(pop()==34)idsuff();
      else v=0,main();
     }
     else pusht(a);
}
void typeil()
{
     int a=pop();
     if((a==4)||(a==5))return;
     else v=0,main();
}
void varsuff()
{
     int a=pop();
     if(a==34)idsuff();
     else pusht(a);
}
void procdefi()
{
     procedh();
     if(v==2){v=1;return;}
     block();
     int a=pop();
     if(a==28)procsuff();
     else pusht(a);
}
void procedh()
{
     int a=pop();
     if(a==7)
     {if(pop()==34)
      {argument();
       if(pop()==28)return;
      }
     }
     v=2;
     pusht(a);
}
void argument()
{
     int a=pop();
     if(a==31)
     {if((pop()==34)&&(pop()==29))
      {typeil();
       if(pop()==32)return;
      }
      v=0,main();
     }
     else pusht(a);
}
void procsuff()
{
     if(v==2){v=1;return;}
     procedh();
     if(v==2){v=1;return;}
     block();
     if(v==2){v=1;return;}
     int a=pop();
     if(a==28)procsuff();
     else pusht(a);
}
void compsent()
{
     int a=pop();
     if(a==13)
     {sentence();
      sentsuff();
      if(pop()==14)return;
     }
     else v=0,main();
}
void sentence()
{
     v=1;
     assipro();
     if(v!=3)ifsent();
     if(v!=3)whilsent();
     if(v!=3)read();
     if(v!=3)Write();
     v=1;
}
void ifsent()
{
     int a=pop();
     if(a==7)
     {Conditio();
      int b=pop();
      if((v==10)&&(b==8))sentence(),v=3;
      else v=0,main();
     }
     else pusht(a);
}
void Conditio()
{
    v=1;
    int a=pop();
    if(a==15)
    {Express();
     if(v==4){v=10;return;}
     else pusht(a);
    }
    else pusht(a);
    Express();
    if(v!=4){pusht(a);return;}
    respoper();
    if(v!=5){pusht(a);return;}
    Express();
    if(v!=4){pusht(a);return;}
    v=10;
}
void Express()
{
     int a=pop();
     if(a==16)
     {term();
      if(v==6)termsuff();
      if(v==7){v=4;return;}
     }
     if(a==17)
     {term();
      if(v==6)termsuff();
      if(v==7){v=4;return;}
     }
     else pusht(a);
     term();
      if(v==6)termsuff();
      if(v==7){v=4;return;}
     pusht(a);
}
void term()
{
     Factor();
     Factsuff();
     v=6;
}
void Factor()
{
     int a=pop();
     if(a==34)return;
     if(a==33)return;
     if(a==31)
     {Express();
      if(v!=4)v=0,main();
      if(pop()!=32)v=0,main();
     }
}
void Factsuff()
{
     Muloper();
     if(v==8)
     {Factor();
      Factsuff();
     }
}
void Muloper()
{
     int a=pop();
     if(a==19){v=8;return;}
     if(a==20){v=8;return;}
     pusht(a);
}
void termsuff()
{
     addoper();
     if(v==9)
     {term();
      if(v==6)termsuff();
      if(v==7)return;
     }
     else v=7;
}
void addoper()
{
     int a=pop();
     if(a==16){v=9;return;}
     if(a==17){v=9;return;}
     v=1;
     pusht(a);
}
void Exprsuff()
{
     int a=pop();
     if(a==26)     
     {Express();
      if(v==4)Exprsuff();
     }
     else pusht(a);
}
void assipro()
{
     int a=pop();
     if(a==34)suffix(),v=3;
     else v=1,pusht(a);
}
void suffix()  
{
     int a=pop();
     if(a==30)
     {Express();
      if(v==4)return;
     }
     if(a==31)
     {Express();
      int b=pop();
      if((v==4)&&(b==32))return;
     }
     pusht(a);
}
void whilsent()
{
     int a=pop();
     if(a==9)
     {Conditio();
      int b=pop();
      if((v==10)&&(b==10))sentence(),v=3;
      else v=0,main();
     }
     else pusht(a);
}
void read()
{
     int a=pop(),b=pop(),c=pop();
     if((a==11)&&(b==31)&&(c==34))
     {idsuff();
      int d=pop();
      if(d==32)v=3;
      else pusht(d),pusht(c),pusht(b),pusht(a);
     }   
     else pusht(c),pusht(b),pusht(a);
}  
void Write()
{
     int a=pop(),b=pop();    
     if((a==12)&&(b==31))
     {Express();
      if(v==4)
      {Exprsuff();
       int c=pop();
       if(c==32){v=3;return;}
       else push(c);
      }
     }
     pusht(b),pusht(a);  
}   
void sentsuff()
{
     int a=pop();
     if(a==28)           
     {sentence();
      sentsuff();
     }
     else pusht(a);
}
void respoper()
{
     int a=pop();
     if((a>=20)&&(a<=25))v=5;
     else pusht(a);
}









int kk()
{
    string a;
    int k=0,l=0;
    char b[100];
    cout<<"请输入文件路径(勿包含中文名!)"<<endl;
    cin>>b;
    fstream jj;
    jj.open(b); 
    while(!jj.eof()) 

  { 
    k++;
    int i=0,j;
    getline(jj,a);
    j=a.length();
    if(j>l)l=j;
    while(i!=j)
    {
        int g=0,h=0;
        char d[100]={};
        while(a[i]==' ')i++;
        while(((a[i]>='a')&&(a[i]<='z'))||((a[i]>='A')&&(a[i]<='Z')))
          {
            d[g]=a[i];g++;i++;
          }
    if(g)
    {
    string s[100]={"PROGRAM","CONST","VAR","INTEGER","LONG","PROCEDURE","IF","THEN","WHILE","DO","READ","WRITE","BEGIN","END","ODD",
    "program","const","var","Integer","long","procedure","if","then","while","do","read","write","begin","end","odd"};
    if((d==s[0])||(d==s[15]))push(1);
    else if((d==s[1])||(d==s[16]))push(2);
    else if((d==s[2])||(d==s[17]))push(3);
    else if((d==s[3])||(d==s[18]))push(4);
    else if((d==s[4])||(d==s[19]))push(5);
    else if((d==s[5])||(d==s[20]))push(6);
    else if((d==s[6])||(d==s[21]))push(7);
    else if((d==s[7])||(d==s[22]))push(8);
    else if((d==s[8])||(d==s[23]))push(9);
    else if((d==s[9])||(d==s[24]))push(10);
    else if((d==s[10])||(d==s[25]))push(11);
    else if((d==s[11])||(d==s[26]))push(12);
    else if((d==s[12])||(d==s[27]))push(13);
    else if((d==s[13])||(d==s[28]))push(14);
    else if((d==s[14])||(d==s[29]))push(15);
    else push(34);
    g=0;
    }
    while((a[i]>47)&&(a[i]<58))
      {
        h=1;
        i++;
      }
    if(h)push(33);
    if(a[i])
      {
        switch(a[i])
          {
            case '+':push(16);break;
            case '-':push(17);break;
            case '*':push(18);break;
            case '/':push(19);break;
            case '=':push(20);break;
            case ',':push(26);break;
            case '.':push(27);break;
            case ';':push(28);break;
            case '(':push(31);break;
            case ')':push(32);break;
            case '#':push(35);break;
            case '<':
            {
            if(a[i+1]=='>')push(21),i++;
            else if(a[i+1]=='=')push(23),i++;
            else push(22);
            break;
            }
            case '>':
            {
            if(a[i+1]=='=')push(25),i++;
            else push(24);break;
            }
            case ':':
            {
            if(a[i+1]=='=')push(30),i++;
            else push(29);break;
            }
            default:if(a[i]!=' ')cout<<"第"<<k<<" 行"<<" 第"<<i<<" 列未知符号"<<endl,push(0); 
            }
            i++;
          }
        }
  } 
  
  jj.close(); 
  s=w;
  program();  
  return 0;
}
int main()
{
    if(v==1)kk();
    if(v==0)cout<<"不是程序!"<<endl;
    else cout<<"是程序!"<<endl;
    system("pause");
    exit(0);
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -