资源简介

包含编译原理实验中的语法分析源程序及实验报告,此语法分析器基于pascal语言,在实验报告中有详细的说明,该语法分析器只能说是小语言语法分析器,文法并不齐全,供大家参考

资源截图

代码片段和文件信息

#include
#include
#include
using namespace std;
int iderror=0;
char ch=0;
char c;
int s[100]={0};
int n[100]={0};//记录行 
int i=0j=0h=1;//h表示行 
string w=““;
string word=““;
ifstream infile(“c:\\users\\jda\\desktop\\a.txt“ios::in);
int judge(char d)
{int flag=0;
 if(d>=48&&d<=57)//数字 
 flag=1;
 else if((d>=65&&d<=90)||(d>=97&&d<=122))//字母 
 flag=2;
 else if(d==‘ ‘||d==‘\n‘)
 flag=3;
 else flag=4;
 return flag;
}
void digit()
{
 word=word+ch;
 c=infile.get();
 if(judge(c)==1) {ch=c;digit();}
 else if(judge(c)==2) cout<<“error!“<       else 
       {id=2;i++;s[i]=id;n[i]=h;
       word=““;
       w=w+c;
       void scan();scan();
       }
       }
 void letter()
 { 
 word=word+ch;
 c=infile.get();
 if(judge(c)==1||judge(c)==2)
 {ch=c;letter();}
 else {if(word==“if“ || word==“else“ || word==“then“ || word==“begin“ || word==“end“ || word==“while“||word==“do“||word==“program“||word==“var“||word==“integer“||word==“char“||word==“boolean“||word==“real“||word==“and“||word==“or“||word==“not“)
        {if(word==“if“)id=11;
         else if(word==“else“)id=12;
         else if(word==“then“)id=13;
         else if(word==“while“)id=14;
         else if(word==“do“)id=15;
         else if(word==“begin“)id=16;
         else if(word==“end“)id=17;
         else if(word==“program“)id=18;
         else if(word==“var“)id=19;
         else if(word==“integer“)id=20;
         else if(word==“char“)id=21;
         else if(word==“boolean“)id=22;
         else if(word==“real“)id=23;
         else if(word==“and“)id=43;
         else if(word==“or“)id=44;
         else if(word==“not“)id=45;
         word=““;i++;s[i]=id;n[i]=h;
         w=w+c;
         void scan();scan();   
       }
      else {id=1;word=““;i++;s[i]=id;n[i]=h;
      w=w+c;void scan();scan();
      }
      }

void yuns()
{
word=word+ch;
c=infile.get();
if(c==‘=‘)
{word=word+c;
if(word==“>=“)id=34;
else if(word==“:=“)id=32;
word=““;i++;s[i]=id;n[i]=h;void scan();scan();}
else{if(word==“>“)id=29;
     else if(word==“:“)id=41;
     word=““;i++;s[i]=id;n[i]=h;
     w=w+c;void scan();scan();
     }
}
void yuns1()
{word=word+ch;
c=infile.get();
if(c==‘=‘||c==‘>‘)
{word=word+c;
if(word==“<=“)id=33;
else if(word==“<>“)id=30;
word=““;i++;s[i]=id;n[i]=h;void scan();scan();}
else{id=28;word=““;i++;s[i]=id;n[i]=h;
     w=w+c;void scan();scan();}
}
void scan()
{if(w!=““)
 {ch=c;w=““;}
 else
 ch=infile.get();
  while(ch==‘ ‘||ch==‘\n‘) 
  {if(ch==‘\n‘)h++;
   ch=infile.get();}
           if(judge(ch)==1)//数字 
           digit();
           else if(judge(ch)==2) //字母 
                 letter();
                 else if(ch==‘+‘||ch==‘-‘||ch==‘*‘||ch==‘/‘||ch==‘=‘)
                { switch(ch)
                  {case ‘+‘:id=24;break;
                   case ‘-‘:id=25;break;
                   case ‘*‘:id=26;break;
                   case ‘=‘:id=31;break;
                   case ‘/‘

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       9750  2009-11-21 21:33  语法分析.cpp

     文件    1301504  2009-12-13 10:14  语法分析.doc

----------- ---------  ---------- -----  ----

              1311254                    2


评论

共有 条评论