(代码在在最后面)
实验目的
熟悉词法分析阶段的要求,掌握利用状态图手工实现词法分析器的方法。
实验设备
硬件:PC 机一台
软件:Windows系统,高级语言集成开发环境
实验内容
根据词法要求手工实现词法分析器
实验要求及步骤
- 理解以下词法表述和状态图表示。


- 参考教科书3.2.4章节编写代码实现词法分析器。实现语言不限,推荐使用C语言,不可用脚本类语言;不允许使用任何语言的正规式控件实现实验要求。








- 提交词法分析器代码与实验报告。实验报告为word形式文档,内容是10对如下形式的输入输出。
if(7xx)=8xx
%%
910x*=200

(7x+8y)=60

__fxk/10

99910
$666+(10)x
jihuiting = dameinv
%%680x1**((55)+x)
top 66
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
string input;
vector<string>errs;
typedef struct node
{
int num;
string content;
string zhu;
node(int a, string b)
{
this->num = a;
if (a == 0)
return;
this->content = b;
vector<string>v = { "","$dim","$if" ,"$do" ,"$stop" ,"$end" ,"$id" ,"$int","$assign","$plus","$star","$power","$comma","$lpar" ,"$rpar" ,"$err" };
this->zhu = v[a];
input.erase(0, b.length());
if (this->num)
this->print();
}
void print()
{
cout.setf(std::ios::left);
cout << setw(20) << this->zhu;
if (this->num == 6 || this->num == 7 || this->num == 15)
cout << this->content;
cout << endl;
}
}node;
const vector<char>end_option = { '=','+','*',',','(',')' ,' ' };
node getnum(string a)
{
string num = "";
for (int i = 0; i < a.length() && isdigit(a[i]); i++)
{
num += a[i];
}
bool isnum = false;
for (auto x : end_option)
{
if (a[num.length()] == x)
{
isnum = true;
break;
}
}
if ((num == "" || !isnum) && a.length() != num.length())
return node(0, num);
else
return node(7, num);
}
node getword(string a)
{
string word = "";
for (int i = 0; i < a.length() && isalpha(a[i]); i++)
{
word += a[i];
}
vector<string> words = { "if","do","stop","end" };
vector<string> words2 = { "IF","DO","STOP","END" };
vector<int>num = { 2,3,4,5 };
for (int i = 0; i < words.size(); i++)
{
if (word == words[i] || word == words2[i])
return node(num[i], word);
}
return node(0,"");
}
node getoption(string a)
{
vector<string> word = { "**","=","+","*",",","(",")" };//**要放在*前面
vector<int>num = { 11,8,9,10,12,13,14 };
for (int i = 0; i < word.size(); i++)
{
if (a.substr(0, word[i].length()) == word[i])
return node(num[i], word[i]);
}
return node(0, "");
}
node getname(string a)
{
if (isdigit(a[0]))
return node(0, "");
string name = "";
for (int i = 0; i < a.length() && (isalnum(a[i])||a[i]=='_'||a[i]=='$'); i++)
{
name += a[i];
}
if (name == "")
return node(0, "");
return node(6, name);
}
node getother(string a)
{
string other = "";
for (int i = 0; i < a.length(); i++)
{
for (int j = 0; j < end_option.size(); j++)
{
if (a[i] == end_option[j])
{
errs.push_back(other);
return node(15, other);
}
}
other += a[i];
}
errs.push_back(other);
return node(15, other);
}
int main()
{
getline(cin, input);
//判断顺序:
//数字(7) getnum
//单词(2-5) getword
//符号(8-14)
//标识符(6)
//其他(15)
while (input!="")
{
if (input[0] == ' ')
{
input.erase(0, 1);
continue;
}
if (getnum(input).num)
continue;
if (getword(input).num)
continue;
if (getoption(input).num)
continue;
if (getname(input).num)
continue;
getother(input);
}
if (errs.size())
{
cout << "========错误分析报告========" << endl;
for (auto x : errs)
{
cout << x << endl;
}
}
}