資源簡介
編譯原理作業,用C++實現了詞法分析、語法分析以及四元式的生成

代碼片段和文件信息
#include“cifa.h“
#define?N?100
string?Reservedword[]?=?{“program““begin““end““var““integer““real““while““if““then““else““do“};
string?Identifier[N];
void?change(char*?str)?????????????//將line中的大寫字母轉換成小寫便于分析
{
for(int?i=0;i if(str[i]>=‘A‘&&str[i]<=‘Z‘)
str[i]+=32;
}
int?isReservedword(string?str)???//判斷保留字,并返回編號,不是保留字則返回-1
{
int?i=0;
for(i=0;i<11;i++)
{
if(!str.compare(Reservedword[i]))
return?i+1;
}
return?-1;
}
int?isIdentifier(string?strint?count)??????//判斷已知標識符,并返回編號,不是保留字則返回-1
{
int?i=0;
for(i=0;i {
if(!str.compare(Identifier[i]))
return?i+1;
}
return?-1;
}
void?addIdentifier(string?strint?count)????//添加標識符
{
Identifier[count]=str;
}
void?LexicAlanalysis(fstream?&?infilefstream?&?outfile)
{
char?line[180];
char?str[180];
int?count?=?0flag?=?1;??????????//count記錄標識符的個數??flag判斷是不是注釋
while(infile>>line)??????????????//讀取一個字符串
{
int?ijk;
change(line);
for(i=0;i {
if(flag==0&&line[i]!=‘*‘)?{i++;continue;}
if(line[i]>=‘a‘&&line[i]<=‘z‘)
{
j=i;k=0;
while((line[j]>=‘0‘&&line[j]<=‘9‘)||(line[j]>=‘a‘&&line[j]<=‘z‘))
str[k++]=line[j++];
str[k]=NULL;
if(k>0)
{
if(isReservedword(str)>=0)??????//保留字
{
outfile<<“(3“< i+=strlen(str);
continue;
}
if(isIdentifier(strcount)>=0)????//標識符
{
outfile<<“(4“< i+=strlen(str);
continue;
}
else
{
addIdentifier(strcount);
count++;
outfile<<“(4“< i+=strlen(str);
continue;
}
}
}
j=i;?k=0;?????????????????????????????????//數字
while((line[j]>=‘0‘&&line[j]<=‘9‘)||line[j]==‘.‘)
str[k++]=line[j++];
str[k]=NULL;
if(str[0]==‘.‘&&str[1]==NULL)
{outfile<<“(8.)“< else?if(k>0)
{
outfile<<“(5“< i+=strlen(str);
continue;
}
if(line[i]==‘;‘)
{outfile<<“(6;)“< if(line[i]==‘‘)
{outfile<<“(7)“< if(line[i]==‘:‘)
{
if(line[i+1]!=NULL&&line[i+1]==‘=‘)
{outfile<<“(10:=)“< else
{outfile<<“(9:)“< }
if(line[i]==‘+‘)
{outfile<<“(11+)“< if(line[i]==‘-‘)
{outfile<<“(12-)“< if(line[i]==‘*‘)
{
if(line[i+1]!=NULL&&line[i+1]==‘/‘&&flag==0)??//處理‘*/‘
{flag=1;i+=2;continue;}
else?if(flag==1)
{outfile<<“(13*)“< else
{i++;continue;}
}
if(line[i]==‘/‘)
{
if(line[i+1]!=NULL&&line[i+1]==‘/‘)
{infile.getline(line180);break;}
else?if(line[i+1]!=NULL&&line[i+1]==‘*‘)
{flag=0;i+=2;continue;}
else?
{outfile<<“(14/)“<
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????228864??2015-04-02?09:11??四元式生成.doc
?????文件?????????252??2014-05-08?11:30??BinaryType.txt
?????文件?????????504??2014-03-28?20:34??cifa.h
?????文件?????????307??2014-05-06?20:21??code.txt
?????文件?????????102??2014-05-08?11:26??code1.txt
?????文件?????????101??2014-05-08?11:27??code2.txt
?????文件?????????140??2014-05-08?11:30??Type.txt
?????文件????????2017??2014-04-08?20:37??yufa.h
?????文件????????9728??2014-03-13?11:49??符號表.xls
?????文件????????4133??2014-03-28?20:34??詞法函數.cpp
?????文件???????14016??2014-06-05?10:13??語法函數.cpp
?????文件?????????820??2014-04-08?20:29??語法分析.cpp
- 上一篇:SNL遞歸下降語法分析 C++實現
- 下一篇:c語言版網吧計費管理系統修改版
評論
共有 條評論