精品词法分析器设计文档格式.docx
《精品词法分析器设计文档格式.docx》由会员分享,可在线阅读,更多相关《精品词法分析器设计文档格式.docx(13页珍藏版)》请在冰豆网上搜索。
圆括号(左、右)用于表达式中,用于改变运算的优先级,以及标识函数的参数;
分号用于标识一个语句的结束。
^[\f\t\n();
]$
6、初等函数运算语言支持的常量还包括:
PI,E.
^PI|E$
2DFA
2.3状态转换表
_
字符
数字
运算符
小数点
1
2、4
2
3
4
2、2.4记号表
3、
实验程序清单
function.cpp
mains。
cpp
name.h
wordID.txt
4、调试过程和运行结果
5、"
x=0。
5*PI;
y=E;
?
1/3*(ln(y)+5*sin(x))+(7+z)^2;
"
程序的主要部分及其功能说明
函数声明
--—-——-——-----———---—-----—---—----——-—-name.h—-—-——-————--—---—-—--————-——--—---------———————--———-
#include<
iostream〉
#include〈string>
usingnamespacestd;
constintMax=1000;
conststringFunctionName[]={”sin"
”cos”,"
tg”,"
ctg"
”log"
”lg”,"
ln”,”PI”,”E”};
//关键字
classOperate
{
private:
stringstr;
//输入的字符串
stringword[Max];
//输出的结果
intnum[Max];
//输出的类型
public:
intstartPostion;
//字符开始位置
intnowPostion;
//字符当前位置
intlength;
//结果个数
Operate(strings);
voidgetWordAndNum();
boolisNumber(charc);
boolisChar(charc);
//boolisChar(charc);
intisOperaters(charc);
boolisblank(charc);
voidaddWords(strings,intn);
};
intisKey(stringstr);
voidwork(stringstr);
--——-——--——-————--——--—-—-———--—--—------function.cpp-——-————-——----———-————--————--————--—————--—--—
函数定义
-—---——--——-——---——--—----———-————————-—-function.cpp————---———-—-——---———---—————--—-—-----—----————
#include<
iomanip〉
#include〈string〉
#include”name。
h"
//Operate类定义
Operate:
:
Operate(strings)
{
str=s;
startPostion=0;
nowPostion=0;
length=0;
}
voidOperate:
:
getWordAndNum()
{
for(inti=0;
i<
=(length-1);
i++)
cout<
<
setw(10)〈<
word[i]〈<
”<
〈setw(3)<
num[i]<
〈"
>
\n”;
}
boolOperate:
isNumber(charc)
if(c〉=’0'
&
c〈=’9’)
returntrue;
returnfalse;
}
isChar(charc)
if((c〉=’a’&&
c〈='
z'
)||(c>
='
A’&&c〈='
Z'
)||c=='
_'
)
returntrue;
returnfalse;
intOperate:
isOperaters(charc)
switch(c)
case'
?
'
return10;
case'
;
’:
return11;
('
return12;
)'
return13;
+'
return14;
case’—'
return15;
case’*'
return16;
/'
return17;
case’=’:
return18;
case’^'
return19;
default:
return0;
};
isblank(charc)
\t’:
case’\n'
voidOperate:
addWords(strings,intn)
word[length]=s;
num[length]=n;
length+=1;
//其他函数
//是否是关键字,是返回序号,否返回0
intisKey(stringstr)
9;
if(str==FunctionName[i])
returni+1;
return0;
}
voidwork(stringstr)
{
intlongs=str。
length();
stringaword;
charc;
//当前字符
intstate=0;
//状态
intnumber=0;
//类型
Operateop(str);
while(true)
{
//读取字符
if(op.nowPostion>
=longs)
break;
else
c=str[op。
nowPostion];
switch(state)
{
case0:
//初始状态
if(c==’$'
break;
//如果是运算符
//保存当前运算符,startPostion=nowPostion=+1
number=op。
isOperaters(c);
if(number〉0)
{
op。
addWords(str.substr(op.startPostion,1),number);
op.nowPostion++;
op.startPostion=op。
nowPostion;
}
if(op.isblank(c))
nowPostion++;
break;
//如果是字符
if(op。
isChar(c))
state=1;
if(c〉=’1'
&&
9’)
state=4;
op.nowPostion++;
}
if(c='
0'
{
state=2;
break;
case1:
//识别字符串
if(op.isChar(c)||op。
isNumber(c))
{
//否则的话,完成当前标识符的识别
number=20;
aword=str。
substr(op.startPostion,op.nowPostion—op。
startPostion);
if(isKey(aword)〉0)
number=isKey(aword);
op.addWords(aword,number);
op。
startPostion=op。
nowPostion;
state=0;
case2:
//识别0,若下一个字符不是小数点’。
则显示输入错误,跳过0,读取下一个字符
if(c==’。
state=3;
isOperaters(c)〉0)
state=0;
op.addWords("
0开头,输入错误!
跳过”,0);
state=0;
case3:
//识别0—9
if(op.isNumber(c))
nowPostion++;
op.addWords(str.substr(op.startPostion,op.nowPostion-op。
startPostion),21);
startPostion=op.nowPostion;
case4:
//识别1-9
isNumber(c))//识别整数
if(c=='
。
)//识别小数
n