一、实验目的
实验目的和要求 通过编写词法分析程序,熟悉其识别单词的基本思想及构造方法。
二、实验要求
编制一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。并依次输出各个单词的内部编码、单词 符号自身值、字符类型。(遇到错误时可显示“Error”,并输出该字符,然后跳过该字符继续识别)。
(1) 标识符(变量)
ID=letter(letter|digit)*
(2) 关键字
void,main,short,long,int,double,float,while,if,else,for,break,return,char
(1) 实数
整数或小数
(2) 运算符
加减乘除 以及 < <= == > >=
(3) 分隔符
, ; ( ) { }
部分状态转换图
代码实现
#include <iostream>
#include <fstream>
#include <string>
#include<map>
#include <cstdio>
#include <cstdlib>
#define SIZE(a)(sizeof(a)/sizeof(a[0])) //求元素个数
using namespace std;
char op[] = {'+','-','*','/'};
void set_mp(map<int,string>&mp) {
mp[1] = "标识符";
mp[2] = "关键字";
mp[3] = "运算符";
mp[4] = "分隔符";
mp[5] = "整数";
mp[6] = "小数";
}
int find_k(string s){//查找s是否在保留字表keyword中
string keyword[] = {"void","main","short","long","int","double","float","while","if","else","for","break","return","char" };//keyword表
for (int i = 0; i < SIZE(keyword); i++) {
if (s.compare(keyword[i]) == 0)return 1;//是保留字,返回1
}
return 0;//不是保留字,返回0
}
int isLimit(char c) {
if (c == ',' || c == ';' || c == '(' || c == ')' || c == '{' || c == '}') {//识别分界符 4
return 1;
}
return 0;
}
int isOp(char c) {
int p = -1;
for(int i = 0; i < 4; i++) {
if(c == op[i]) p = i;
}
return p;
}
int main()
{
ifstream in("input.txt");
ofstream out("output.txt");
string s;
char word;
int op_pos = -1;
map<int,string>mp;
set_mp(mp);
word = in.get();
while (!in.eof()) {
if (isLimit(word)) {//识别分界符
out << "<" << word << ",4,"<<mp[4]<<">\n";
word = in.get();
}else if (isdigit(word)) {//识别整数/实数
s = "";
while (isdigit(word)) {
s += word;
word = in.get();
if (word == '.') {//如果出现小数点,有可能是常实数
s += word;
word = in.get();
if (isdigit(word)) {
while (isdigit(word)) {
s += word;
word = in.get();
}
out << "<" << s << ",6,"<<mp[6]<<">\n";//常实数
break;
}
else out << s + " is error!\n";//如果小数点后没有数字,则报错
}
if (!isdigit(word)) {
out << "<" << s << ",5,"<<mp[5]<<">\n";//常整数
break;
}
}
}else if (isalpha(word) || word == '_') {//识别标识符/保留字
s = "";
while (isalpha(word) || isdigit(word) || word =='_') {
s += word;
word = in.get();
}
if (find_k(s)) {//在keyword表中查找s是否是保留字
out << "<" << s << ",2,"<<mp[2]<<">\n";//保留字
}
else out << "<" << s << ",1,"<<mp[1]<<">\n";//标识符
}else if (word == '>') {//后面都是运算符
s = "";
s += word;
word = in.get();
if (word == '=') {//">="
s += word;
word = in.get();
}
out << "<" << s << ",3,"<<mp[3]<<">\n";
}
else if (word == '<') {
s = "";
s += word;
word = in.get();
if (word == '=') {//"<="
s += word;
word = in.get();
}
out << "<" << s << ",3,"<<mp[3]<<">\n";
}else if (word == '=') {
s = "";
s += word;
word = in.get();
if (word == '=') {//"=="
s += word;
word = in.get();
}
out << "<" << s << ",3,"<<mp[3]<<">\n";
}else if((op_pos = isOp(word)) != -1) {
out << "<" << word << ",3,"<<mp[3]<<">\n";
word = in.get();
}else {//跳过当前单词
word = in.get();
}
}
return 0;
}
测试数据
int main()
{
int i,ans;
char c;
scan c;
double b=1.5;
float f;
for(i=0;i<5;i=i+1)
{
ans=ans+1;
}
print ans;
return 0;
}
输出结果
<int,2,关键字>
<main,2,关键字>
<(,4,分隔符>
<),4,分隔符>
<{,4,分隔符>
<int,2,关键字>
<i,1,标识符>
<,,4,分隔符>
<ans,1,标识符>
<;,4,分隔符>
<char,2,关键字>
<c,1,标识符>
<;,4,分隔符>
<scan,1,标识符>
<c,1,标识符>
<;,4,分隔符>
<double,2,关键字>
<b,1,标识符>
<=,3,运算符>
<1.5,6,小数>
<;,4,分隔符>
<float,2,关键字>
<f,1,标识符>
<;,4,分隔符>
<for,2,关键字>
<(,4,分隔符>
<i,1,标识符>
<=,3,运算符>
<0,5,整数>
<;,4,分隔符>
<i,1,标识符>
<<,3,运算符>
<5,5,整数>
<;,4,分隔符>
<i,1,标识符>
<=,3,运算符>
<i,1,标识符>
<1,5,整数>
<),4,分隔符>
<{,4,分隔符>
<ans,1,标识符>
<=,3,运算符>
<ans,1,标识符>
<1,5,整数>
<;,4,分隔符>
<},4,分隔符>
<print,1,标识符>
<ans,1,标识符>
<;,4,分隔符>
<return,2,关键字>
<0,5,整数>
<;,4,分隔符>
<},4,分隔符>