没有合适的资源?快使用搜索试试~ 我知道了~
首页燕山大学软件工程编译原理实验报告.doc
资源详情
资源评论
资源推荐

词法分析程序
目的:
设计、编制和调试一个具体的词法分析程序,加深对词法分析的理解。
要求:
(1)通过对 PL/0 词法分析程序(GETSYS)的分析,编制一个具有以下功能的词
法分析程序:
a.输入为字符串(或待进行词法分析的源程序),输出为单词串,即由
(单词,类别)所组成的二元组序列;
b. 有一定的错误检查能力,例如能发现 2a 这类不能作为单词的字符串。
(2)提交设计报告,报告内容包括:
实验目的、要求,算法描述,程序结构,主要变量说明,程序清单,调试
情况,设计技巧,心得体会。
算法描述:
词法分析的思路比较简单,先判断是字母还是数字,如果是字母,那么开始判断是关
键字还是标识符。如果是其它的分隔符之类的,进行处理即可
程序清单:
#include<iostream>
#include<fstream>
#include<string>
#include<ctype.h>//用?来ぁ?判 D 断?变?量?类え?型 í
using namespace std;
ifstream hengbao("source.txt", ios::in);
string key[13] = { "if", "else", "for", "while", "do", "return", "break", "continue", "int",
"void", "main", "const", "cout" }; //关?键 ü 字?
string border[7] = { ",", ";", "{", "}", "(", ")", "//" }; //分?界?符?
string arithmetic[6] = { "+", "-", "*", "/", "++", "--" }; //运?算?符?

string relation[7] = { "<", "<=", "=", ">", ">=", "==", "!=" }; //关?系 μ 运?算?符?
string lableconst[80];//标括?识?符?
int constnum = 40;
int lableconstnum = 0; //统?计?常£数簓和 í 标括?识?符?数簓量?
int linenum = 1;
char wbu9er = NULL;//缓 o 冲?器÷,?用?于?一?个?一?个?地?读 ásource.txt 文?件 t 里?的?字?
符?
bool search(string searchchar, int wordtype)
{
switch (wordtype)
{
case 1://判 D 断?是?否?为 a 关?键 ü 字?
{
for (int i = 0; i < 13; i++)
{
if (searchchar == key[i])
{
return true;
}
}
return false;
break;
}
case 2://判 D 断?是?否?为 a 分?解 a 符?
{
for (int i = 0; i < 7; i++)
{
if (searchchar == border[i])
{
return true;
}
}
return false;
break;
}
case 3://运?算?符?
{
for (int i = 0; i < 6; i++)
{
if (searchchar == arithmetic[i])
{
return true;
}
}

return false;
break;
}
case 4://关?系 μ 运?算?符?
{
for (int i = 0; i < 7; i++)
{
if (searchchar == relation[i])
{
return true;
}
}
return false;
break;
}
case 5:
{
int i = 0;
for (;i < lableconstnum; i++)
{
if (searchchar == lableconst[i])
{
return true;
}
}
lableconst[i] = searchchar;
lableconstnum++;
return false;
break;
}
default:
cout << "error ";
break;
}
return false;
}
char alphaprocess(char bu9er) //字?符?处鋦理え?过 y 程 ì
{
int i = -1;
char alphatp[20];
while ((isalpha(bu9er)) || (isdigit(bu9er)))//这 a 两?个?函 ˉ 数簓分?别纄是?判 D 字?符?
和 í 判 D 数簓字?函 ˉ 数簓位?于 瞔 type.h 中 D
{

alphatp[++i] = bu9er;
hengbao.get(bu9er);
}
alphatp[i + 1] = '\0';//在 ú 末?尾 2 添?加 ó 字?符?串?结 á 束?标括?志?
if (search(alphatp, 1))
cout << "linenum: " << linenum << " String= " << alphatp << "\t\t\t" <<
"关?键 ü 字?" << endl;
else
{
search(alphatp, 5); //标括?识?符?
cout << "linenum: " << linenum << " String= " << alphatp << "\t\t\t" <<
"标括?识?符?" << endl;
}
return(bu9er);
}
char erroralphaprocess(char before,char bu9er) //非?法ぁ?标括?识?符?字?符?处
鋦理え?过 y 程 ì
{
char alphatp[20];
alphatp[0]=before;
int i = 0;
while ((isalpha(bu9er)) || (isdigit(bu9er)))//这 a 两?个?函 ˉ 数簓分?别纄是?判 D 字?符?
和 í 判 D 数簓字?函 ˉ 数簓位?于 瞔 type.h 中 D
{
alphatp[++i] = bu9er;
hengbao.get(bu9er);
}
alphatp[i + 1] = '\0';//在 ú 末?尾 2 添?加 ó 字?符?串?结 á 束?标括?志?
cout << "linenum: " << linenum << " String= " << alphatp << "\t\t\t" << "非?
法ぁ?标括?识?符?" << endl;
return bu9er;
}
char digitprocess(char bu9er) //数簓字?处鋦理え?过 y 程 ì
{
int i = -1;
char digittp[20];
bool Iag=true;
char before;
before=bu9er;
while ((isdigit(bu9er)))
{
digittp[++i] = bu9er;
hengbao.get(bu9er);

if(isalpha(bu9er))//说 μ 明÷数簓字?后 ó 面?跟 ú 的?不?是?一?个?数簓字?
{
bu9er=erroralphaprocess(before,bu9er);
return bu9er;
}
}
digittp[i + 1] = '\0';
cout << "linenum: " << linenum << " String= " << digittp << "\t\t\t" << "数簓
据 Y" << endl;
return(bu9er);
}
char otherprocess(char bu9er) //分?界?符?、¢运?算?符?、¢逻?辑-运?算?符?、¢等台?
{
char othertp[20];
bool otype, otypetp;
othertp[0] = bu9er;
othertp[1] = '\0';
if (otype = search(othertp, 3))
{
hengbao.get(bu9er);
othertp[1] = bu9er;
othertp[2] = '\0';
if (otypetp = search(othertp, 3)) //判 D 断?该?运?算?符?是?否?是?由?连?续?的?两?
个?字?符?组哩?成 é 的?
{
cout << "linenum: " << linenum << " String= " << othertp << "\t\t\t"
<< "运?算?符?" << endl;
hengbao.get(bu9er);
}
else //单蹋?字?符?逻?辑-运?算?符?
{
othertp[1] = '\0';
cout << "linenum: " << linenum << " String= " << othertp << "\t\t\t"
<< "逻?辑-运?算?符?" << endl;
}
}
if (otype = search(othertp, 4)) //关?系 μ 运?算?符?
{
hengbao.get(bu9er);
othertp[1] = bu9er;
othertp[2] = '\0';
if (otypetp = search(othertp, 4)) //判 D 断?该?关?系 μ 运?算?符?是?否?是?由?连?
续?的?两?个?字?符?组哩?成 é 的?
{
剩余36页未读,继续阅读




安全验证
文档复制为VIP权益,开通VIP直接复制

评论0