词法语法.docx
《词法语法.docx》由会员分享,可在线阅读,更多相关《词法语法.docx(28页珍藏版)》请在冰豆网上搜索。
词法语法
2012060080028包馨宇
词法分析器
Val.h:
#include
#include
#include
#include
inttype=0;
charval[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
charerrval[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
voidto_outfile();
voidto_errfile();
Function.h
usingnamespacestd;
boolisletter(charc)//判别是否字母
{
if(c>64&&c<91||c>96&&c<123)
returntrue;
returnfalse;
}
boolisdigital(charc)//判别是否数字
{if(c>47&&c<58)
returntrue;
returnfalse;
}
intmatch_reserve(char*p)//匹配保留字
{
if(!
(strcmp(p,"begin")))
{
type=1;
strcpy(val,"begin");
to_outfile();
return1;
}
elseif(!
(strcmp(p,"end")))
{
type=2;
strcpy(val,"end");
to_outfile();
return1;
}
elseif(!
(strcmp(p,"integer")))
{
type=3;
strcpy(val,"integer");
to_outfile();
return1;
}
elseif(!
(strcmp(p,"if")))
{
type=4;
strcpy(val,"if");
to_outfile();
return1;
}
elseif(!
(strcmp(p,"then")))
{
type=5;
strcpy(val,"then");
to_outfile();
return1;
}
elseif(!
(strcmp(p,"else")))
{
type=6;
strcpy(val,"else");
to_outfile();
return1;
}
elseif(!
(strcmp(p,"function")))
{
type=7;
strcpy(val,"function");
to_outfile();
return1;
}
elseif(!
(strcmp(p,"read")))
{
type=8;
strcpy(val,"read");
to_outfile();
return1;
}
elseif(!
(strcmp(p,"write")))
{
type=9;
strcpy(val,"write");
to_outfile();
return1;
}
else
return0;
}//match
voidmatch_small(char*p,inttempc)//非保留字、非运算符匹配
{
chart[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
strcpy(t,p);
intk=0;
intstate=0;
for(k;k{
if(isletter(t[k]))
{
state=1;
break;
}
}
if(state==0)
{
type=11;
strcpy(val,p);
to_outfile();
}
else
{
type=10;
strcpy(val,p);
to_outfile();
}
}
///////////////////////////////////////////////匹配双运算符
voidmatch_double_operator(char*p)
{
if(!
(strcmp(p,":
=")))
{
type=20;
strcpy(val,":
=");
to_outfile();
}
elseif(!
(strcmp(p,"<>")))
{
type=13;
strcpy(val,"<>");
to_outfile();
}
elseif(!
(strcmp(p,"<=")))
{
type=14;
strcpy(val,"<=");
to_outfile();
}
elseif(!
(strcmp(p,">=")))
{
type=16;
strcpy(val,">=");
to_outfile();
}
}
//////////////////////////////////////////////////匹配单个运算符
voidmatch_single_operator(char*p)
{
if(!
(strcmp(p,"<")))
{
type=15;
strcpy(val,"<");
to_outfile();
}
elseif(!
(strcmp(p,">")))
{
type=17;
strcpy(val,">");
to_outfile();
}
elseif(!
(strcmp(p,"-")))
{
type=18;
strcpy(val,"-");
to_outfile();
}
elseif(!
(strcmp(p,"*")))
{
type=19;
strcpy(val,"*");
to_outfile();
}
elseif(!
(strcmp(p,"=")))
{
type=13;
strcpy(val,"=");
to_outfile();
}
elseif(!
(strcmp(p,"(")))
{
type=21;
strcpy(val,"(");
to_outfile();
}
elseif(!
(strcmp(p,")")))
{
type=22;
strcpy(val,")");
to_outfile();
}
elseif(!
(strcmp(p,";")))
{
type=23;
strcpy(val,";");
to_outfile();
}
else
{
type=0;
strcpy(val,p);
strcpy(errval,p);
to_outfile();
to_errfile();
}
}
Scanner.cpp
#include
#include"val.h"
#include
#include"function.h"
usingnamespacestd;
ifstreamsourcefile;/////源程序文件
ofstreamoutfile;//打开分析结果二元表
ofstreamerrfile;//打开错误记录表
charbuffer[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//读入缓冲区
chartemp[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
chartail[2]={0,0};
voidto_outfile()
{
outfile<outfile<<"";
outfile<for(inti=0;i<15;i++)
{
val[i]=0;
}
outfile<<"\n";
outfile<<"\n";
}
voidto_errfile()
{
errfile<errfile<<"";
errfile<for(inti=0;i<15;i++)
{
errval[i]=0;
}
errfile<<"\n";
outfile<<"\n";
}
intmain(intargc,char*argv[]){
staticcharSourceFileName[256];
staticcharOutputFileName[256];
staticcharErrorFileName[256];
intcount=0;//读字符计数器
cout<<"Pleaseinputthepathandthefilenameofyoursourcefile:
"<cin>>SourceFileName;
sourcefile.open(SourceFileName);
if(!
sourcefile.is_open()){
cout<<"Openfailed!
PleaseCheckThePath!
"<cin.get();
return0;
}
cout<<"Pleaseinputthefilenameofyouroutputfile:
"<cin>>OutputFileName;
cout<<"Pleaseinputthefilenameofyourerrorfile:
"<cin>>ErrorFileName;
outfile.open(OutputFileName);
errfile.open(ErrorFileName);
while(sourcefile>>buffer){//当还有数据可读(没读到EOF的时候)读入一个单词长度的字符(读到空格或回车)
if(buffer[0]==0){
break;
}
intnext=0;//指针指向缓存数组
intsmall=0;//非运算符(标识符)数组长度
while((next<15)&&(buffer[next]!
=0)){//遍历所有单词字符
if((isletter(buffer[next]))||(isdigital(buffer[next]))){//将字母和数字加入small数组
small++;
tail[0]=buffer[next];
strcat(temp,tail);
next++;
if(buffer[next]==0){///////////////如果非运算符处于单词末尾处理small数组
if(temp[0]!
=0){
intfm=match_reserve(temp);//匹配保留字
if(fm==1){
for(intj=0;j<15;j++){
temp[j]=0;
}
tail[0]=0;
tail[1]=0;
small=0;
}
if(fm==0){//匹配保留字不成功
match_small(temp,small);
for(intj=0;j<15;j++){
temp[j]=0;
}
tail[0]=0;
tail[1]=0;
small=0;
}
}
}
}
else{//遇到运算符
////////////////////////先对small数组即标识符处理
if(temp[0]!
=0){
intfm=match_reserve(temp);//匹配保留字
if(fm==1){
for(intj=0;j<15;j++){
temp[j]=0;
}
tail[0]=0;
tail[1]=0;
small=0;
}
if(fm==0){
match_small(temp,small);
for(intj=0;j<15;j++){
temp[j]=0;
}
tail[0]=0;
tail[1]=0;
small=0;
}
}
/////////////////////////////////////////处理双操作符,向后展望一个字符
if(((buffer[next]==':
')&&(buffer[next+1]=='='))||((buffer[next]=='<')&&(buffer[next+1]=='='))||((buffer[next]=='>')&&(buffer[next+1]=='='))||((buffer[next]=='<')&&(buffer[next+1]=='>'))){
tail[0]=buffer[next];
tail[1]=buffer[next+1];
match_double_operator(tail);
next+=2;
tail[0]=0;
tail[1]=0;
}
//////////////////////////////////////////处理单操作符
else{
tail[0]=buffer[next];
match_single_operator(tail);
next++;
tail[0]=0;
tail[1]=0;
}
}//else
}//while
for(intq=0;q<15;q++){
buffer[q]=0;
}
}//while
sourcefile.close();
outfile.close();
errfile.close();
return0;
语法分析器
#include
#include
#include
#include
usingnamespacestd;
structvariable
{
stringvname,vproc,vtype;
intvkind,vlev,vadr;
}var;
structprocedure
{
stringpname,ptype;
intplev,fadr,ladr;
}proc;
vectorv_table;
vectorp_table;
stringsym;
intval,cur_line,cur_level;
boolended=false;
voidclean_output()
{
freopen("example.dys","w",stdout);
fclose(stdout);
freopen("example.var","w",stdout);
fclose(stdout);
freopen("example.pro","w",stdout);
fclose(stdout);
freopen("SynAnalyze.err","w",stdout);
fclose(stdout);
}
voidadvance()
{
freopen("example.dys","a+",stdout);
cin>>sym>>val;
printf("%16s%2d\n",sym.c_str(),val);
if(sym=="EOF")ended=true;
while(sym=="EOLN")
{
cur_line++;
cin>>sym>>val;
printf("%16s%2d\n",sym.c_str(),val);
}
}
voiderror(inttype)
{
freopen("SynAnalyze.err","a+",stdout);
switch(type)
{
case0:
printf("LINE%d:
variable\"%s\"previously
declared.\n",cur_line,sym.c_str());break;
case1:
printf("LINE%d:
missingdeclarationhere.\n",
cur_line);break;
case2:
printf("LINE%d:
missingexecutivestatement
here.\n",cur_line);break;
case3:
printf("LINE%d:
expected\"end\"here.\n",
cur_line);break;
case4:
printf("LINE%d:
expected\"begin\"here.\n",
cur_line);break;
case5:
printf("LINE%d:
missing\';\'here.\n",
cur_line);break;
case6:
printf("LINE%d:
bracketsdoesn'tmatched.\n",
cur_line);break;
case7:
printf("LINE%d:
illegalvariablehere.\n",
cur_line);break;
case8:
printf("LINE%d:
missing\'(\'here.\n",
cur_line);break;
case9:
printf("LINE%d:
illegaldeclarationhere.\n",
cur_line);break;
case10:
printf("LINE%d:
illegalexecutivestatement
here.\n",cur_line);break;
case11:
printf("LINE%d:
variable\"%s\"
undeclared.\n",cur_line,sym.c_str());break;
case12:
printf("LINE%d:
missing\"else\"here.\n",
cur_line);break;
case13:
printf("LINE%d:
missing\"then\"here.\n",
cur_line);break;
case14:
printf("LINE%d:
illegaloperatorhere.\n",
cur_line);break;
default:
printf("LINE%d:
unknownerror\n",cur_line);
}
advance();
fclose(stdout);
}
voidinit()
{
clean_output();
val,cur_line=1;
cur_level=0;
v_table.clear();
p_table.clear();
proc.pname="";
proc.ptype="";
proc.plev=0;
proc.fadr=0;
proc.ladr=-1;
p_table.push_back(proc);
return;
}
voidv_add(intvkind)
{
for(inti=p_table[cur_level].fadr;i<=p_table[cur_level].ladr;i++)
{
if(v_table[i].vname==sym&&v_table[i].vlev==cur_level&&v_table[i].vkind==vkind)
{
error(0);
return;
}
}
var.vname=sym;
var.vproc=p_table[cur_level].pname;
var.vtype="integer";
var.vkind=vkind;
var.vlev=cur_level;
var.vadr=v_table.size();
v_table.push_back(var);
p_table[cur_level].ladr++;
}
boolv_check()
{
for(inti=p_table[cur_level].fadr;i<=p_table[cur_level].ladr;i++)
{
if(v_table[i].vname==sym&&v_table[i].vlev==cur_level)
{
returntrue;
}
}
returnfalse;
}
voidA();
voidB();
voidC();
voidD();
voidE();
voidF();
voidG();
voidH();
voidI();
voidJ();
voidA()
{
if(sym=="begin")
{
advance();
if(sym=="integer")
{
advance();
B();
}
elseerror
(1);
if(sym==";")
{
while(sym==";")
{
advance();
if(sym=="integer")
{
advance();
B();
}
else
{
break;
}
}
}
elseerror
(2);
C();
while(sym==";")
{
advance