词法分析程序
目的:
设计、编制和调试一个具体的词法分析程序,加深对词法分析的理解。
要求:
(1)通过对 PL/0 词法分析程序(GETSYS)的分析,编制一个具有以下功能的词法
分析程序:
a.输入为字符串(或待进行词法分析的源程序),输出为单词串,即由(单
词,类别)所组成的二元组序列;
b. 有一定的错误检查能力,例如能发现 2a 这类不能作为单词的字符串。
(2)提交设计报告,报告内容包括:
实验目的、要求,算法描述,程序结构,主要变量说明,程序清单,调试
情况,设计技巧,心得体会。
算法描述:
词法分析的思路比较简单,先判断是字母还是数字,如果是字母,那么开始判断是关键
字还是标识符。如果是其它的分隔符之类的,进行处理即可
程序清单:
#include
#include
#include
#include//用?来ぁ?判 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 wbuffer = 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 buffer)
//字?符?处鋦理え?过 y 程ì
{
int i = -1;
char alphatp[20];
while ((isalpha(buffer)) || (isdigit(buffer)))//这 a 两?个?函ˉ数簓分?别纄是?判 D 字?
符?和í判 D 数簓字?函ˉ数簓位?于 瞔 type.h 中 D
{
}
alphatp[++i] = buffer;
hengbao.get(buffer);
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(buffer);
}
char erroralphaprocess(char before,char buffer)
//非?法ぁ?标括?识?符?字?
符?处鋦理え?过 y 程ì
{
char alphatp[20];
alphatp[0]=before;
int i = 0;
while ((isalpha(buffer)) || (isdigit(buffer)))//这 a 两?个?函ˉ数簓分?别纄是?判 D 字?
符?和í判 D 数簓字?函ˉ数簓位?于 瞔 type.h 中 D
{
}
alphatp[++i] = buffer;
hengbao.get(buffer);
alphatp[i + 1] = '\0';//在ú末?尾 2 添 ?加ó字?符?串?结á束?标括?志?
cout << "linenum:
" << linenum << "
String= " << alphatp << "\t\t\t" << "非?法ぁ?
标括?识?符?" << endl;
return buffer;
}
char digitprocess(char buffer)
//数簓字?处鋦理え?过 y 程ì
{
int i = -1;
char digittp[20];
bool flag=true;
char before;
before=buffer;
while ((isdigit(buffer)))
{
}
digittp[++i] = buffer;
hengbao.get(buffer);
if(isalpha(buffer))//说μ明÷数簓字?后ó面?跟ú的?不?是?一?个?数簓字?
{
}
buffer=erroralphaprocess(before,buffer);
return buffer;
digittp[i + 1] = '\0';
cout << "linenum: " << linenum << "
String= " << digittp << "\t\t\t" << "数簓据
Y" << endl;
return(buffer);
}
char otherprocess(char buffer)
//分?界?符?、¢运?算?符?、¢逻?辑-运?算?符?、¢等台?
{
char othertp[20];
bool otype, otypetp;
othertp[0] = buffer;
othertp[1] = '\0';
if (otype = search(othertp, 3))
{
hengbao.get(buffer);
othertp[1] = buffer;
othertp[2] = '\0';
if (otypetp = search(othertp, 3)) //判 D 断?该?运?算?符?是?否?是?由 ?连?续?的?
两?个?字?符?组哩?成é的?
{
cout << "linenum:
" << linenum << "
String=
" << othertp << "\t\t\t" <<
"运?算?符?" << endl;
hengbao.get(buffer);
}
else
{
othertp[1] = '\0';
//单蹋?字?符?逻?辑-运?算?符?
cout << "linenum:
" << linenum << "
String=
" << othertp << "\t\t\t" <<
"逻?辑-运?算?符?" << endl;
}
}
if (otype = search(othertp, 4)) //关?系μ运?算?符?
{
hengbao.get(buffer);
othertp[1] = buffer;
othertp[2] = '\0';
if (otypetp = search(othertp, 4))
//判 D 断?该?关?系μ运?算?符?是?否?是?由 ?连?
续?的?两?个?字?符?组哩?成é的?
{
cout << "linenum:
" << linenum << "
String=
" << othertp << "\t\t\t" <<
"关?系μ运?算?符?" << endl;
hengbao.get(buffer);
}
else
{
//单蹋?字?符?逻?辑-运?算?符?
othertp[1] = '\0';
cout << "linenum:
" << linenum << "
String=
" << othertp << "\t\t\t" <<
"逻?辑-运?算?" << endl;
}
}
if (otype = search(othertp, 2))
//分?界?符?
{
cout << "linenum: " << linenum << "
String= " << othertp << "\t\t\t" << "分?
界?符?" << endl;
hengbao.get(buffer);
}
return(buffer);
}int main()
{
for (int i = 0; i <= 50; i++)
{
}
lableconst[i] = "
";//用?于 ?保馈?存?标括?识?符?
if (!hengbao)
cout << "文?件 t 打洙?开 a 错洙?误ó" << endl;
{
}
else
{
hengbao.get(wbuffer);
while (!hengbao.eof())
{
if (wbuffer == '\n')
{
}
linenum++;
hengbao.get(wbuffer);
else if (isalpha(wbuffer))// 如?果?是?字?母?,?那?么′当獭?然?可é能ü是?标括?
识?符?也?可é能ü是?关?键ü字?,?进?行 D 检ì查é
{
}
wbuffer = alphaprocess(wbuffer);
else if (isdigit(wbuffer))
{
wbuffer = digitprocess(wbuffer);