JMU软件工程编译原理实验二

实验二 利用自动机理论实现词法分析器

1.实验目的: 熟悉词法分析阶段的要求,掌握利用自动机理论实现词法分析器的方法。

2.实验设备: 硬件:PC 机一台; 软件:Windows系统;高级语言集成开发环境。

3.实验内容: 根据词法要求采用自动机理论实现词法分析器。实现语言不可用脚本类语言,推荐使用C语言;不允许使用任何语言的正规式控件实现实验要求。

4.实验要求及步骤

  1. 理解以下词法表述和状态图表示。

 

状态图:

状态2识别关键字和标识符,状态4识别常数状态,5~12识别操作符和括号状态,13为出错状态

  1. 将以上状态图转换为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}

  1. 根据DFA的状态转换矩阵实现词法分析器。自设10对如下所示输入输出对,展示词法分析器运行结果。

  1. 从代码量、时间复杂度、空间复杂度三方面,分析对比实验一与实验二两种词法分析器

代码量远小于实验一,实验二时间复杂度大于实验一,空间复杂度都为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;
			}
		}
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hiOoo.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值