《词法分析和语法分析设计编译器编译原理课程设计.doc》由会员分享,可在线阅读,更多相关《词法分析和语法分析设计编译器编译原理课程设计.doc(36页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流词法分析和语法分析设计编译器编译原理课程设计.精品文档.编译原理课程设计报告课题名称: C- Minus词法分析和语法分析设计 提交文档学生姓名: X X X 提交文档学生学号: XXXXXXXXXX 同组 成 员 名 单: X X X 指导 教 师 姓 名: X X 指导教师评阅成绩: 指导教师评阅意见: 提交报告时间:2015年6月10日1. 课程设计目标实验建立C-编译器。只含有扫描程序(scanner)和语法分析(parser)部分。2. 分析与设计C-编译器设计的整体框架,本实验实现扫描处理和语法分析程序(图中粗黑部分)。 2.1
2、、扫描程序scanner部分2.1.1系统设计思想设计思想:根据DFA图用switch-case结构实现状态转换。 惯用词法: 语言的关键字:else if int return void while 专用符号:+ - * / = = != = ; , ( ) /* */ 其他标记是ID和NUM,通过下列正则表达式定义: ID = letter letter* NUM = digit digit* letter = a|.|z|A|.|Z digit = 0|.|9大写和小写字母是有区别的 空格由空白、换行符和制表符组成。空格通常被忽略,除了它必须分开ID、NUM关键字。 注释用通常的C语言符
3、号/ * . . . * /围起来。注释可以放在任何空白出现的位置(即注释不能放在标记内)上,且可以超过一行。注释不能嵌套说明:当输入的字符使DFA到达接受状态的时候,则可以确定一个单词了。初始状态设置为START,当需要得到下一个token时,取得次token的第一个字符,并且按照DFA与对此字符的类型分析,转换状态。重复此步骤,直到DONE为止,输出token类型。当字符为“/”时,状态转换为SLAH再判断下一个字符,如果为“*”则继续转到INCOMMENT,最后以“*”时转到ENDCOMMENT状态,表明是注释,如果其他的则是字符停滞于当前字符,并且输出“/”。2.1.2程序流程图2.1
4、.3 各文件或函数的设计说明扫描程序用到:scanner.h,scanner.cpp scanner.h:声明词法状态,词法分析/DFA中的状态typedef enumSTART = 1, INNUM, INID, INDBSYM, DONE DFAState;/定义的Token的类型(31种),分别对应于else、if、int、return、void、while、+、-、*、/、=、=、!=、=、;、,、(、)、/*、*/、num、id、错误、结束typedef enumELSE = 1,IF,INT,RETURN,VOID,WHILE,PLUS,MINUS,TIMES,OVER,LT,LE
5、Q,GT,GEQ,EQ,NEQ,ASSIGN,SEMI,COMMA,LPAREN,RPAREN,LMBRACKET,RMBRACKET,LBBRACKET,RBBRACKET,LCOMMENT,RCOMMENT,NUM,ID,ERROR,ENDFILE TokenType;/定义的Token结构体,包括类型、对应的串、所在代码的行号struct TokenTokenType tokenType;string tokenString;int lineNo;/每种TokenType对应的串,如tokenTypeStringELSE=ELSEconst string tokenTypeString3
6、2 = OTHER, ELSE, IF, INT, RETURN, VOID, WHILE, PLUS, MINUS, TIMES, OVER, LT, LEQ, GT, GEQ, EQ, NEQ, ASSIGN, SEMI, COMMA, LPAREN, RPAREN, LMBRACKET, RMBRACKET, LBBRACKET, RBBRACKET, LCOMMENT, RCOMMENT, NUM, ID, ERROR, ENDFILE;class Scanner:定义scanner.cpp中函数 scanner.cpp文件函数说明void Scanner : scan():设置输出结
7、果界面以及设置各种输出状态。if(scanSuccess=false)cout词法分析出错!endl;elsecout词法分析成功了!endl;printToken();/*输出Token到文件Token.txt中*/正在删除注释void Scanner : deleteComments()TokenType Scanner : returnTokenType(string s)/返回Token的类型DFAState Scanner : charType(char c)/返回字符的类型typedef enum ENDFILE,ERROR, IF,ELSE,INT,RETURN,VOID,WHI
8、LE, /关键字ID,NUM, ASSIGN,PLUS,MINUS,TIMES,OVER,EQ,UEQ,LT,LPAREN,RPAREN,SEMI,BT,LQ,BQ, DOU,LZGH,RZGH,LDGH,RDGH,/特殊字符:= + - * / = != declaration-list 2.declaration-list-declaration-list declaration | declaration 3.declaration-var-declaration|fun-declaration 4.var-declaration-type-specifier ID;|type-spec
9、fier IDNUM 5.type-specifier-int|void 6.fun-specifier ID(parans) compound-stmt 7.params-params-list|void 8.param-list-param-list,param|param 9.param-type-specifier ID|type-specifier ID pound-stmt-local-declarations statement-list 11.local-declarations-local-declarations var-declaration|empty 12.state
10、ment-list-statement-list statement|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)statement else statement 16.iteration-stmt-while(expression)statement 17.return-stmt-
11、return ;|return expression; 18.expression-var=expression|simple-expression 19.var-ID|IDexpression 20.simple-expression-additive-expression relop additive-expression|additive-expression 21.relop-=|=|=|!= 22.additive-expression-additive-expression addop term|term 23.addop-+|- 24.term-term mulop factor
12、|factor 25.mulop-*|/ 26.factor-(expression)|var|call|NUM 27.call-ID(args) 28.args-arg-list|empty 29.arg-list-arg-list,expression|expression2.1.2语法分析程序流程图 2.1.3 各文件或函数的设计说明语法分析程序包括:parser.cpp,parser.h parser.cpp: Parser : Parser()/界面设计 Token Parser : getToken()/获取scanner中保存在TokenList数组中的Token,并且每次获取完
13、之后数组下标指向下一个void Parser : syntaxError(string s)/出错处理void Parser : match(TokenType ex)/匹配出错TreeNode * Parser : declaration(void)/类型匹配错误TreeNode * Parser : param_list(TreeNode * k)/k可能是已经被取出来的VoidK,但又不是(void)类型的参数列表,所以一直传到param中去,作为其一个子节点 parse.h:对parse.c的函数声明 /19种节点类型,分别表示int、id、void、数值、变量声明、数组声明、函数声明
14、、函数声明参数列表、函数声明参数、复合语句体、if、while、return、赋值、运算、数组元素、函数调用、函数调用参数列表、未知节点typedef enum 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;typedef enum Void,Integer ExpType;o
15、fstream fout_Tree(tokenTree.txt);/输出语法树到文件/treeNode定义 包括子节点、兄弟节点、所处行号、节点类型、属性、表达式返回类型typedef struct treeNode TreeNode * newNode(Nodekind k);/根据节点类型新建节点TreeNode * declaration_list(void);TreeNode * declaration(void);TreeNode * params(void);TreeNode * param_list(TreeNode * k);TreeNode * param(TreeNode
16、* 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(v
17、oid);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。/* A program to perform Eucilds Algor
18、ithm to compute gcd. */int gcd (int u, int v) if (v=0) return u; else return gcd(v,u-u/v*v); /* u-u/v*v= u mod v */void main(void) int x; int y; x=input(); y=input(); output(gcd(x,y);3. 程序代码实现按文件列出主要程序代码, 添加必要的注释.Scanner.cpp:#include #include #include #include #include scanner.h#includeusing namespa
19、ce std;Name: 词法分析器Copyright: Author: XXXDate: 19-05-14 12:00Description: 提取出tokenScanner : Scanner()scanSuccess = true;charIndex = 0;str = ;commentFlag = true;sourseString = ;lineCount = 0;void Scanner : scan()cout开始词法分析.endl;bool doubleSym = false;getSourseStringFromFile(sourseFile.txt);int state =
20、 START;lineCount = 0;char ch;while(state6)ch = getNextChar();if(0=ch)Token t;t.lineNo = lineCount;t.tokenString = ;t.tokenType = ENDFILE;tokenList.push_back(t);break;if(START=state)/初始状态和空格state = charType(ch);if(state!=START)str += ch;else if(INNUM=state)/digitstate = charType(ch);if(state!=INNUM)s
21、tate = DONE;elsestr += ch;else if(INID=state)/letterstate = charType(ch);if(state!=INID)state = DONE;elsestr += ch;else if(INDBSYM=state)/除了=!之外的各种符号if(=ch)str += ch;doubleSym = true;elsedoubleSym = false;state = DONE;if(DONE=state)/接收状态int tp = 0;if(n=ch)tp = 1;Token t;t.lineNo = lineCount-tp;t.tok
22、enString = str;t.tokenType = returnTokenType(str);tokenList.push_back(t);if(ERROR=t.tokenType)scanSuccess = false;int lastState = charType(strstr.length()-1);if(lastState=INNUM | lastState=INID | (lastState=INDBSYM & doubleSym=false)backToLastChar();str = ;state = START;if(doubleSym=true)doubleSym =
23、 false;if(scanSuccess=false)cout词法分析出错!endl;elsecout词法分析成功了!endl;printToken();/输出Token到文件Token.txt中Token Scanner : getTokenAt(int tokenIndex)Token token;token.lineNo = lineCount;token.tokenString = ;token.tokenType = ENDFILE;if(tokenIndextokenList.size()token = tokenList.at(tokenIndex+);return token
24、;void Scanner : getSourseStringFromFile(string path)ifstream fin(path.c_str();string temp; sourseString = ;while(getline(fin,temp)sourseString += temp;sourseString += n;fin.close();charIndex = 0;void Scanner : deleteComments()cout正在删除注释.endl;ofstream fout_Sourse(sourseFile.txt);int state = 1;char ch
25、;while(state6)ch = getNextChar();if(0=ch)/文件结束break;if(1=state)if(/=ch)state = 2;elsestate = 1;fout_Soursech;else if(2=state)if(*=ch)state = 3;commentFlag = false;elsestate = 1;fout_Sourse/ch;else if(3=state)if(*=ch)state = 4;elsestate = 3;else if(4=state)if(*=ch)state = 4;else if(/=ch)state = 5;els
26、estate = 3;if(5=state)/结束状态,处理commentFlag = true;state = 1;if(!commentFlag)cout注释错误,没有结束符!endl;scanSuccess = false;elsecout注释已经成功删除!endl;TokenType Scanner : returnTokenType(string s)/返回Token的类型TokenType t;if(s=else)t = ELSE;else if(s=if)t = IF;else if(s=int)t = INT;else if(s=return)t = RETURN;else i
27、f(s=void)t = VOID;else if(s=while)t = WHILE;else if(s=+)t = PLUS;else if(s=-)t = MINUS;else if(s=*)t = TIMES;else if(s=/)t = OVER;else if(s=)t = LT;else if(s=)t = GT;else if(s=)t = GEQ;else if(s=)t = EQ;else if(s=!=)t = NEQ;else if(s=)t = ASSIGN;else if(s=;)t = SEMI;else if(s=,)t = COMMA;else if(s=(
28、)t = LPAREN;else if(s=)t = RPAREN;else if(s=)t = LMBRACKET;else if(s=)t = RMBRACKET;else if(s=)t = LBBRACKET;else if(s=)t = RBBRACKET;else if(s=/*)t = LCOMMENT;else if(s=*/)t = RCOMMENT;else if(2=charType(ss.length()-1)t = NUM;else if(3=charType(ss.length()-1)t = ID;elset = ERROR;return t;DFAState S
29、canner : charType(char c)/返回字符的类型if( =c | n=c | t=c |r=c)return START; else if(c=0&c=A&c=a&c=z)return INID;else if(c= | c= | c=!)return INDBSYM; elsereturn DONE;char Scanner : getNextChar()if(charIndex0)char ch = sourseStringcharIndex-1;charIndex-;if(n=ch)lineCount-;void Scanner : printToken()ofstre
30、am fout_Token(Token.txt);ifstream fin(sourseFile.txt);string temp; int lineCount = 0;int index = 0;while(getline(fin,temp)fout_TokenlineCount: ;fout_Tokentempendl;while(indextokenList.size()Token t = tokenList.at(index);if(lineCount=t.lineNo)fout_Token lineCount=1&t.tokenType=6)/关键字string tp = ;for(
31、int i = 0; iwidth-t.tokenString.length(); i+)tp += ;fout_TokenkeyWord:headSt.tokenString tptokenTypeStringt.tokenType=7&t.tokenType=27)/符号string tp = ;for(int i = 0; iwidth-t.tokenString.length(); i+)tp += ;fout_Tokensymbols:headSt.tokenString tptokenTypeStringt.tokenTypeendl;else if(t.tokenType=28)
32、/NUMstring tp = ;for(int i = 0; iwidth-t.tokenString.length(); i+)tp += ;fout_Token NUM:headSt.tokenString tptokenTypeStringt.tokenTypeendl;else if(t.tokenType=29)/IDstring tp = ;for(int i = 0; iwidth-t.tokenString.length(); i+)tp += ;fout_Token ID:headSt.tokenString tptokenTypeStringt.tokenTypeendl
33、;else if(t.tokenType=30)/错误string tp = ;for(int i = 0; iwidth-t.tokenString.length(); i+)tp += ;fout_Token error:headSt.tokenString tptokenTypeStringt.tokenTypeendl;else if(t.tokenType=ENDFILE)/结束fout_Token lineCount: ;fout_Tokent.tokenString tokenTypeStringt.tokenTypeendl;if(lineCountt.lineNo)break
34、;lineCount+;fin.close();fout_Token.close();scanner.h:#include#includeusing namespace std;/定义的Token的类型(31种),分别对应于else、if、int、return、void、while、+、-、*、/、=、=、!=、=、;、,、(、)、/*、*/、num、id、错误、结束typedef enumELSE = 1,IF,INT,RETURN,VOID,WHILE,PLUS,MINUS,TIMES,OVER,LT,LEQ,GT,GEQ,EQ,NEQ,ASSIGN,SEMI,COMMA,LPAREN,R
35、PAREN,LMBRACKET,RMBRACKET,LBBRACKET,RBBRACKET,LCOMMENT,RCOMMENT,NUM,ID,ERROR,ENDFILE TokenType;typedef enumSTART = 1, INNUM, INID, INDBSYM, DONE DFAState;/定义的Token结构体,包括类型、对应的串、所在代码的行号struct TokenTokenType tokenType;string tokenString;int lineNo;/每种TokenType对应的串,如tokenTypeStringELSE=ELSEconst string
36、 tokenTypeString32 = OTHER, ELSE, IF, INT, RETURN, VOID, WHILE, PLUS, MINUS, TIMES, OVER, LT, LEQ, GT, GEQ, EQ, NEQ, ASSIGN, SEMI, COMMA, LPAREN, RPAREN, LMBRACKET, RMBRACKET, LBBRACKET, RBBRACKET, LCOMMENT, RCOMMENT, NUM, ID, ERROR, ENDFILE;class Scannerpublic:bool scanSuccess;/词法分析是否成功的标志void getS
37、ourseStringFromFile(string s);/通过提供的文件名获取源代码void deleteComments();/删除注释void scan();/词法分析,将分析的Token放在tokenList数组中Scanner();Token getTokenAt(int);/根据下标从tokenList数组中获取Tokenprivate:DFAState charType(char);/返回字符的类型,如:空格2:数字3:字母等char getNextChar();/获取到下一个字符void backToLastChar();TokenType returnTokenType(string s);/根据字符串返回Token类型void printToken();/将词法分析好的Token输出到文件Token.txt中string sourseString;/获取源代码的字符串