词法分析器、语法分析器试验报告一.程序思路1.词法分析器对输入的程序进行分析,将关键字,保留字与系统标识符分开,并对其属性进行说明。
建立数组,将单词读入,对单词的ASCII码进行判断,将在ASCII码值在一定区间进行区分(使用if语句来判断),分开来后,对其所在ASCII值区间的不同,给予以不同的属性(比如:系统保留字…),词法分析器完成后要能识别+,-,=等字符。
2.语法分析器对输入的程序语句进行分析。
对每个非终结点定义单个函数,定义了它的后继节点。
然后用一个构造来的函数,使定义的后继节点符合语法,否则报错。
二.源程序cifafenxi:#include"string.h"#include"iostream.h"#include"stdio.h"#include"conio.h"#define OK 1#define……#define……class word{ int ch;char code[50][50];public://word();int scan();int letter(int ch);int digit(int ch);int flag(int ch);int keyword(char * ch);int alpha(char * ch);int judge(char * ch);};int word::letter(int ch) //字符判断int word::alpha(char * ch) //字符属性设定int word::digit(int ch) //数字判断int word::flag(int ch){if(((ch>='0')&&(ch<='9'))||((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z'))||(ch=='+')||(ch=='-')||(ch=='*')||(ch=='/')||(ch=='=')||(ch=='==')||(ch=='%')||(ch=='<')||(ch=='>')||(ch=='(')||(ch==')'))return(1);else return(0);}int word::keyword(char * ch) //关键字判断{if((ch[0]=='i')&&(ch[1]=='n')&&(ch[2]=='t')&&(ch[3]==0)){ cout<<"\n"<<"The [int] is a keyword !"<<"\n"; return(INT); }if((ch[0]=='c')&&(ch[1]=='h')&&(ch[2]=='a')&&(ch[3]=='r')&&(ch[4]==0)){ cout<<"\n"<<"The [char] is a keyword !"<<"\n"; return(CHAR); }……………………………………………………{ cout<<"\n"<<"It is a keyword [if] !"<<"\n"; return(SWITCH); }return(0);}int word::judge(char * ch) //数字进位制判断if((ch[0]=='0')&&((ch[1]>='0')&&(ch[1]<='7'))){ cout<<"\n"<<"This is a OTC number, OTC value is"<<"["<<ch<<"] !"<<"\n";return(OTC); }……………………………………………………return(1);}int word::scan(){ int i=0,j=0,x=1;cout<<"Please input !"<<"\n"; ch=getc(stdin);while((ch==' ')||(ch==10))ch=getc(stdin);if((flag(ch)==NO)){ cout<<"\n"<<"ERROR"<<"\n"; return(ERROR); }ungetc(ch,stdin);while(x) { ch=getc(stdin); if((letter(ch)==YES)){ i=0; while (letter(ch)||digit(ch)) {code[j][i]=ch;ch=getc(stdin);i++; }code[j][i]=0;if(keyword(code[j])==0) {if(alpha(code[j]))cout<<"The identifier's ID is ["<<j<<"] !"<<"\n";} j++;if(ch==10)x=0; }if((digit(ch)==YES)){ i=0;while(digit(ch)||(ch=='x')||((ch>='a')&&(ch<='f'))){code[j][i]=ch;ch=getc(stdin);i++;}code[j][i]=0;judge(code[j]);cout<<"The number's ID is ["<<j<<"] !"<<"\n";j++;if(ch==10)x=0; }if(ch==':'){ i=0; code[j][i]=ch; ch=getc(stdin); if(ch=='='){ code[j][i++]=ch;cout<<"The ["<<"code[j]"<<" is (ASSIGN-OP, -) ."; j++; ch=getc(stdin); if(ch==10)x=0; }} switch(ch){case'+':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (PLUS-OP, AND) ."<<"\n"; } //ch=getc(stdin); if(ch==59)ungetc(ch,stdin); }break;case'-':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (MINUS-OP, SUB) ."<<"\n"; } //ch=getc(stdin);if(ch==59)ungetc(ch,stdin); }break;……………………………………………………break;case')':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, RP) ."<<"\n";}// ch=getc(stdin); if(ch==59)ungetc(ch,stdin); }break;}if(ch==10)x=0;}return(OK);}int main(){char ch1;word word;while(ch1!='q'){word.scan();ch1=getch();}return(OK);}yufafenxi:#include"string.h"#include"iostream.h"#include"stdio.h"#include"conio.h"class word{ int ch;char code[MAX][MAX];public:// word ();int scan();int letter(int ch);int digit(int ch);int right(int ch);int keyword(char * ch);int alpha(char * ch);int judge(char * ch);int yufa(int ch);int match(int ch);int word _e();int word _t();int word _f();int word _s();int word _c();};int word::letter(int ch)……int word::alpha(char * ch)……int word::digit(int ch)……int word::right(int ch)……int word::match(int ch){ int i=0;int j=0;int x=1;ch=getc(stdin);while((ch==' ')||(ch==10))ch=getc(stdin);if((isright(ch)==NO)){ cout<<"\n"<<"ERROR"<<"\n"; return(ERROR); }ungetc(ch,stdin);while(x){ ch=getc(stdin); if((isletter(ch)==YES)) { i=0; while (isletter(ch)||isdigit(ch)){ code[j][i]=ch; ch=getc(stdin);i++; }code[j][i]=0;ungetc(ch,stdin);if(ch==10)x=0;j++;return(iskeyword(code[j-1]));}if((isdigit(ch)==YES)){ i=0; while(isdigit(ch)||(ch=='x')||((ch>='a')&&(ch<='f'))){code[j][i]=ch;ch=getc(stdin);i++; }code[j][i]=0;ungetc(ch,stdin); j++;if(ch==10)x=0;return(judge(code[j-1])); } if(ch==':'){ i=0; code[j][i]=ch; ch=getc(stdin); if(ch=='='){ code[j][i++]=ch; code[j][i++]=0; j++; return(ASSIGN);} }switch(ch){ case'+':{ i=0; code[j][i]=ch; code[j][i++]=0; return(AND); }case'-':{ i=0; code[j][i]=ch; code[j][i++]=0; return(SUB); } ……………………………………………………} i f(ch==10)x=0;}return(OK);}int word:: word _t()……int word:: word _f()……int word::word_s()……int main(){char ch1;word word;while(ch1!='q'){clrscr();cout<<"Please Input !"<<"\n";lookhead=word.scan();word.fun_s();ch1=getch();}return(OK);}三.小节——心得体会这次小程序的编写让我对编译程序的工作有了个大概的了解,让我的编译程序的原理及设计有了长足的进步,使我明白和更好的体会到了编译程序的作用。