编译原理实验(一)——词法分析
程序员文章站
2024-03-03 19:47:52
...
实验要求
输入为字符串(或待进行词法分析的源程序),输出为单词串,即由(单词,类别)所组成的二元组序列;
参考程序
#include <string>
#include <iostream>
#include <fstream>
#include <set>
using namespace std;
const int maxLineNum = 100;
set<string> key; //关键字
char ch; //当前读取字符
int col, maxcol; // col 读取当前行位置,当前行最大长度
ifstream readfile; //文件输入流
char line[maxLineNum]; //读取文件一行
char identifier[maxLineNum]; //保留读取标识符或者关键字
void init(); //进行初始化工作
int getsym(); //进行词法分析
int getch(); //读取一个字符
int main()
{
cout << "请输入要分析的文件名(请放在该程序同一目录): ";
string filename = "";
cin>>filename;
readfile.open(filename);
if (readfile.is_open() == false)
{
cout << "文件不存在";
return 0;
}
init();
col = maxcol = 0;
ch = ' ';
while (getsym())
;
return 0;
}
void init()
{
key.insert("const");
key.insert("do");
key.insert("while");
key.insert("if");
key.insert("else");
key.insert("for");
key.insert("int");
key.insert("char");
key.insert("return");
key.insert("void");
key.insert("read");
key.insert("write");
}
int getch()
{
if (col == maxcol)
{
if (readfile.peek() == EOF)
{
return -1;
}
maxcol = col = 0;
ch = ' ';
while (ch != '\n')
{
if (readfile.peek() == EOF)
{
line[maxcol] = 0;
break;
}
readfile.get(ch);
line[maxcol++] = ch;
}
}
ch = line[col++];
return 0;
}
int getsym()
{
int i, j, k, num;
while (ch == ' ' || ch == 10 || ch == 13 || ch == 9)
{
if (getch() == -1)
return 0;
}
if (isalpha(ch))
{
k = 0;
while (isalpha(ch) || isdigit(ch))
{
identifier[k] = ch;
k++;
if (getch() == -1)
return 0;
}
identifier[k] = 0;
if (key.find(string(identifier)) == key.end())
{
cout << "标识符 (3 , '" << identifier << "')\n";
}
else
{
cout << "关键字 (1 , '" << identifier << "')\n";
}
}
else
{
if (isdigit(ch))
{
k = num = 0;
while (isdigit(ch))
{
num = num * 10 + ch - '0';
k++;
if (getch() == -1)
return 0;
}
cout << "整数 (2 , '" << num << "')\n";
}
else
{
if (ch == '<')
{
if (getch() == -1)
return 0;
if (ch == '=')
{
cout << "运算符 (4 , '<=')\n";
if (getch() == -1)
return 0;
}
else
{
cout << "运算符 (4 , '<')\n";
}
}
else if (ch == '>')
{
if (getch() == -1)
return 0;
if (ch == '=')
{
cout << "运算符 (4 , '>=')\n";
if (getch() == -1)
return 0;
}
else
{
cout << "运算符 (4 , '>')\n";
}
}
else
{
if (ch == '+')
{
cout << "运算符 (4 , '+')\n";
}
else if (ch == '-')
{
cout << "运算符 (4 , '-')\n";
}
else if (ch == '*')
{
cout << "运算符 (4 , '*')\n";
}
else if (ch == '/')
{
cout << "运算符 (4 , '/')\n";
}
else if (ch == '=')
{
cout << "运算符 (4 , '=')\n";
}
else if (ch == '(')
{
cout << "运算符 (4 , '(')\n";
}
else if (ch == ')')
{
cout << "运算符 (4 , ')')\n";
}
else if (ch == '.')
{
cout << "运算符 (4 , '.')\n";
}
else if (ch == ',')
{
cout << "分界符 (5 , ',')\n";
}
else if (ch == ';')
{
cout << "分界符 (5 , ';')\n";
}
else if (ch == '{')
{
cout << "分界符 (5 , '{')\n";
}
else if (ch == '}')
{
cout << "分界符 (5 , '}')\n";
}
else
{
cout << ch << " error!请检查代码是否拼写正确!";
return 0;
}
if (getch() == -1)
return 0;
}
}
}
return 1;
}
实验截图
输入test.txt
int main(){
int a=10,b=20;
int c=a+b;
}