您的位置:首页 > 编程语言 > C语言/C++

C++:词法分析

2015-12-05 21:30 441 查看
词法分析的思路比较简单,先判断是字母还是数字,如果是字母,那么开始判断是关键字还是标识符。如果是其它的分隔符之类的,进行处理即可,我写的分析程序十分简单,大神可以略过哦。

#include<iostream>
#include<fstream>
#include<string>
#include<ctype.h>//用来判断变量类型
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;//缓冲器,用于一个一个地读source.txt文件里的字符
bool search(string searchchar, int wordtype)
{
switch (wordtype)
{
case 1://判断是否为关键字
{
for (int i = 0; i < 13; i++)
{
if (searchchar == key[i])
{
return true;
}
}
return false;
break;
}
case 2://判断是否为分解符
{
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)                //字符处理过程
{
int i = -1;
char alphatp[20];
while ((isalpha(buffer)) || (isdigit(buffer)))//这两个函数分别是判字符和判数字函数位于ctype.h中
{
alphatp[++i] = buffer;
hengbao.get(buffer);
}
alphatp[i + 1] = '\0';//在末尾添加字符串结束标志
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 digitprocess(char buffer)  //数字处理过程
{
int i = -1;
char digittp[20];
while ((isdigit(buffer)))
{
digittp[++i] = buffer;
hengbao.get(buffer);
}
digittp[i + 1] = '\0';
cout << "linenum:  " << linenum << "   String=  " << digittp << "\t\t\t" << "数据" << 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))  //判断该运算符是否是由连续的两个字符组成的
{
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))  //判断该关系运算符是否是由连续的两个字符组成的
{
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 << "文件打开错误" << endl;
}
else
{
hengbao.get(wbuffer);
while (!hengbao.eof())
{
if (wbuffer == '\n')
{
linenum++;
hengbao.get(wbuffer);
}
else if (isalpha(wbuffer))// 如果是字母,那么当然可能是标识符也可能是关键字,进行检查
{
wbuffer = alphaprocess(wbuffer);
}
else if (isdigit(wbuffer))
{
wbuffer = digitprocess(wbuffer);
}
else
{
wbuffer = otherprocess(wbuffer);
}
}
cout << "标识符个数是:" << lableconstnum << "分别是" << endl;
for (int i = 0; i < lableconstnum; i++)
{
cout << lableconst[i] << " ";
}
cout << endl;
cout << "词法分析完成";
hengbao.close();
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  词法分析