实验二 利用自动机理论实现词法分析器
1.实验目的: 熟悉词法分析阶段的要求,掌握利用自动机理论实现词法分析器的方法。
2.实验设备: 硬件:PC 机一台; 软件:Windows系统;高级语言集成开发环境。
3.实验内容: 根据词法要求采用自动机理论实现词法分析器。实现语言不可用脚本类语言,推荐使用C语言;不允许使用任何语言的正规式控件实现实验要求。
4.实验要求及步骤
- 理解以下词法表述和状态图表示。
状态图:
状态2识别关键字和标识符,状态4识别常数状态,5~12识别操作符和括号状态,13为出错状态
- 将以上状态图转换为DFA的状态转换矩阵,写出该矩阵。
{ 0, 1, 3, 5, 6, 7, 10, 11, 12, 13}
{2, 1, 1, 2, 2, 2, 2, 2, 2, 2}
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
{4, 4, 3, 4, 4, 4, 4, 4, 4, 4}
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
{8, 8, 8, 8, 8, 9, 8, 8, 8, 8}
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
- 根据DFA的状态转换矩阵实现词法分析器。自设10对如下所示输入输出对,展示词法分析器运行结果。
- 从代码量、时间复杂度、空间复杂度三方面,分析对比实验一与实验二两种词法分析器。
代码量远小于实验一,实验二时间复杂度大于实验一,空间复杂度都为O(1)。
代码如下:
#include <iostream>
using namespace std;
#include<cstring>
int Array[14][10] = {
{0, 1, 3, 5, 6, 7, 10, 11, 12, 13},
{2, 1, 1, 2, 2, 2, 2, 2, 2, 2},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{4, 4, 3, 4, 4, 4, 4, 4, 4, 4},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{8, 8, 8, 8, 8, 9, 8, 8, 8, 8},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
};
int Analyze(char ch) {
if (ch == ' ') {
return 0;
} else if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') {
return 1;
} else if (ch >= '0' && ch <= '9') {
return 2;
} else if (ch == '=') {
return 3;
} else if (ch == '+') {
return 4;
} else if (ch == '*') {
return 5;
} else if (ch == ',') {
return 6;
} else if (ch == '(') {
return 7;
} else if (ch == ')') {
return 8;
} else
return 9;
}
int main() {
string test = "8 a bbba # char 888 int dsa8d ***a = , + - ";
string str = "";
char testChar;
int now = 0;
int next;
cout << "您的输入为:" << test << endl<<"输出如下结果:" << endl;
for (int i = 0; i < test.length(); i++) {
testChar = test[i];
next = Analyze(testChar);
while (1) {
int flag = Array[now][next];
if (flag == 0) {
now = flag;
testChar = test[++i];
next = Analyze(testChar);
} else if (flag == 1 | flag == 3 | flag == 7) {
str += testChar;
now = flag;
testChar = test[++i];
next = Analyze(testChar);
} else if (flag == 2 | flag == 4 | flag == 8) {
if (flag == 2) {
cout << '<' << str << ',' << " 标识符" << '>' << endl;
} else if (flag == 4) {
cout << '<' << str << ',' << " 常数" << '>' << endl;
} else if (flag == 8) {
cout << '<' << str << ',' << " *号终结符" << '>' << endl;
}
str = "";
now = 0;
break;
}
else {
str += testChar;
if (flag == 5) {
cout << '<' << str << ',' << " =号终结符" << '>' << endl;
} else if (flag == 6) {
cout << '<' << str << ',' << " +号终结符" << '>' << endl;
} else if (flag == 9) {
cout << '<' << str << ',' << " **号终结符" << '>' << endl;
} else if (flag == 10) {
cout << '<' << str << ',' << " 逗号终结符" << '>' << endl;
} else if (flag == 11) {
cout << '<' << str << ',' << " (号终结符" << '>' << endl;
} else if (flag == 12) {
cout << '<' << str << ',' << " )号终结符" << '>' << endl;
} else {
cout << '<' << str << ',' << " 其他终结符" << '>' << endl;
}
str = "";
now = 0;
break;
}
}
}
}