printToken();/*输出Token到文件Token.txt中*/
//正在删除注释
voidScanner:
:
deleteComments()
TokenTypeScanner:
:
returnTokenType(strings)//返回Token的类型
DFAStateScanner:
:
charType(charc)//返回字符的类型
typedefenum
{ENDFILE,ERROR,
IF,ELSE,INT,RETURN,VOID,WHILE,//关键字
ID,NUM,
ASSIGN,PLUS,MINUS,TIMES,OVER,EQ,UEQ,LT,LPAREN,RPAREN,SEMI,BT,LQ,BQ,
DOU,LZGH,RZGH,LDGH,RDGH,//特殊字符:
=+-*/==!
=<等
}TokenType;
2.1.4测试程序说明
根据附录A后面的例子,程序输入两个整数,计算并打印出它们的最大公因子,保存为a.txt。
/*AprogramtoperformEucild's
Algorithmtocomputegcd.*/
intgcd(intu,intv)
{
if(v==0)
returnu;
elsereturn
gcd(v,u-u/v*v);/*u-u/v*v==umodv*/
}
voidmain(void)
{
intx;
inty;
x=input();
y=input();
output(gcd(x,y));
}
2.2、语法分析parse部分
2.2.1系统设计思想
设计思想:
parser用递归下降分析方法实现,通过调用词法分析函数getToken实现语法分析。
根据C-语言的规则,得出BNF语法如下:
1.program->declaration-list
2.declaration-list->declaration-listdeclaration|declaration
3.declaration->var-declaration|fun-declaration
4.var-declaration->type-specifierID;|type-specfierID[NUM]
5.type-specifier->int|void
6.fun-specifierID(parans)compound-stmt
7.params->params-list|void
8.param-list->param-list,param|param
9.param->type-specifierID|type-specifierID[]
pound-stmt->{local-declarationsstatement-list}
11.local-declarations->local-declarationsvar-declaration|empty
12.statement-list->statement-liststatement|empty
13.statement->expression-stmt|compound-stmt|selection-stmt|iteration-stmt|return-stmt
14.expression-stmt->expression;|;
15.selection-stmt->if(expression)statement|if(expression)statementelsestatement
16.iteration-stmt->while(expression)statement
17.return-stmt->return;|returnexpression;
18.expression->var=expression|simple-expression
19.var->ID|ID[expression]
20.simple-expression->additive-expressionrelopadditive-expression|additive-expression
21.relop-><=|<|>|>=|==|!
=
22.additive-expression->additive-expressionaddopterm|term
23.addop->+|-
24.term->termmulopfactor|factor
25.mulop->*|/
26.factor->(expression)|var|call|NUM
27.call->ID(args)
28.args->arg-list|empty
29.arg-list->arg-list,expression|expression
2.1.2语法分析程序流程图
2.1.3各文件或函数的设计说明
语法分析程序包括:
parser.cpp,parser.h
Øparser.cpp:
Parser:
:
Parser()//界面设计
TokenParser:
:
getToken()//获取scanner中保存在TokenList数组中的Token,并且每次获取完之后数组下标指向下一个
voidParser:
:
syntaxError(strings)//出错处理
voidParser:
:
match(TokenTypeex)//匹配出错
TreeNode*Parser:
:
declaration(void)//类型匹配错误
TreeNode*Parser:
:
param_list(TreeNode*k)//k可能是已经被取出来的VoidK,但又不是(void)类型的参数列表,所以一直传到param中去,作为其一个子节点
Øparse.h:
对parse.c的函数声明
//19种节点类型,分别表示int、id、void、数值、变量声明、数组声明、函数声明、函数声明参数列表、函数声明参数、复合语句体、if、while、return、赋值、运算、数组元素、函数调用、函数调用参数列表、未知节点
typedefenum{IntK,IdK,VoidK,ConstK,Var_DeclK,Arry_DeclK,FunK,ParamsK,ParamK,CompK,Selection_StmtK,Iteration_StmtK,Return_StmtK,AssignK,OpK,Arry_ElemK,CallK,ArgsK,UnkownK}Nodekind;
typedefenum{Void,Integer}ExpType;
ofstreamfout_Tree("tokenTree.txt");//输出语法树到文件
//treeNode定义包括子节点、兄弟节点、所处行号、节点类型、属性、表达式返回类型
typedefstructtreeNode
TreeNode*newNode(Nodekindk);//根据节点类型新建节点
TreeNode*declaration_list(void);
TreeNode*declaration(void);
TreeNode*params(void);
TreeNode*param_list(TreeNode*k);
TreeNode*param(TreeNode*k);
TreeNode*compound_stmt(void);
TreeNode*local_declaration(void);
TreeNode*statement_list(void);
TreeNode*statement(void);
TreeNode*expression_stmt(void);
TreeNode*selection_stmt(void);
TreeNode*iteration_stmt(void);
TreeNode*return_stmt(void);
TreeNode*expression(void);
TreeNode*var(void);
TreeNode*simple_expression(TreeNode*k);
TreeNode*additive_expression(TreeNode*k);
TreeNode*term(TreeNode*k);
TreeNode*factor(TreeNode*k);
TreeNode*call(TreeNode*k);
TreeNode*args(void);
2.1.4测试程序说明
根据附录A后面的例子,程序输入两个整数,计算并打印出它们的最大公因子,保存为a.txt。
/*AprogramtoperformEucild's
Algorithmtocomputegcd.*/
intgcd(intu,intv)
{
if(v==0)
returnu;
elsereturn
gcd(v,u-u/v*v);/*u-u/v*v==umodv*/
}
voidmain(void)
{
intx;
inty;
x=input();
y=input();
output(gcd(x,y));
}
3.程序代码实现
按文件列出主要程序代码,添加必要的注释.
Scanner.cpp:
#include
#include
#include
#include
#include"scanner.h"
#include
usingnamespacestd;
/*
Name:
词法分析器
Copyright:
Author:
XXX
Date:
19-05-1412:
00
Description:
提取出token
*/
Scanner:
:
Scanner()
{
scanSuccess=true;
charIndex=0;
str="";
commentFlag=true;
sourseString="";
lineCount=0;
}
voidScanner:
:
scan()
{
cout<<"开始词法分析..."<booldoubleSym=false;
getSourseStringFromFile("sourseFile.txt");
intstate=START;
lineCount=0;
charch;
while(state<6)
{
ch=getNextChar();
if('\0'==ch)
{
Tokent;
t.lineNo=lineCount;
t.tokenString="";
t.tokenType=ENDFILE;
tokenList.push_back(t);
break;
}
if(START==state)//初始状态和空格
{
state=charType(ch);
if(state!
=START)
str+=ch;
}
elseif(INNUM==state)//digit
{
state=charType(ch);
if(state!
=INNUM)
state=DONE;
else
str+=ch;
}
elseif(INID==state)//letter
{
state=charType(ch);
if(state!
=INID)
state=DONE;
else
str+=ch;
}
elseif(INDBSYM==state)//除了<>=!
之外的各种符号
{
if('='==ch)
{
str+=ch;
doubleSym=true;
}
else
doubleSym=false;
state=DONE;
}
if(DONE==state)//接收状态
{
inttp=0;
if('\n'==ch)
tp=1;
Tokent;
t.lineNo=lineCount-tp;
t.tokenString=str;
t.tokenType=returnTokenType(str);
tokenList.push_back(t);
if(ERROR==t.tokenType)
scanSuccess=false;
intlastState=charType(str[str.length()-1]);
if(lastState==INNUM||lastState==INID||(lastState==INDBSYM&&doubleSym==false))
backToLastChar();
str="";
state=START;
if(doubleSym==true)
doubleSym=false;
}
}
if(scanSuccess==false)
cout<<"词法分析出错!
"<else
cout<<"词法分析成功了!
"<printToken();//输出Token到文件Token.txt中
}
TokenScanner:
:
getTokenAt(inttokenIndex)
{
Tokentoken;
token.lineNo=lineCount;
token.tokenString="";
token.tokenType=ENDFILE;
if(tokenIndex{
token=tokenList.at(tokenIndex++);
}
returntoken;
}
voidScanner:
:
getSourseStringFromFile(stringpath)
{
ifstreamfin(path.c_str());
stringtemp;
sourseString="";
while(getline(fin,temp))
{
sourseString+=temp;
sourseString+='\n';
}
fin.close();
charIndex=0;
}
voidScanner:
:
deleteComments()
{
cout<<"正在删除注释..."<ofstreamfout_Sourse("sourseFile.txt");
intstate=1;
charch;
while(state<6)
{
ch=getNextChar();
if('\0'==ch)//文件结束
break;
if(1==state)
{
if('/'==ch)
state=2;
else
{
state=1;
fout_Sourse<}
}
elseif(2==state)
{
if('*'==ch)
{
state=3;
commentFlag=false;
}
else
{
state=1;
fout_Sourse<<"/"<}
}
elseif(3==state)
{
if('*'==ch)
state=4;
else
{
state=3;
}
}
elseif(4==state)
{
if('*'==ch)
state=4;
elseif('/'==ch)
state=5;
else
{
state=3;
}
}
if(5==state)//结束状态,处理
{
commentFlag=true;
state=1;
}
}
if(!
commentFlag)
{
cout<<"注释错误,没有结束符!
"<scanSuccess=false;
}
else
cout<<"注释已经成功删除!
"<}
TokenTypeScanner:
:
returnTokenType(strings)//返回Token的类型
{
TokenTypet;
if(s=="else")
{
t=ELSE;
}
elseif(s=="if")
{
t=IF;
}
elseif(s=="int")
{
t=INT;
}
elseif(s=="return")
{
t=RETURN;
}
elseif(s==