资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
第9页 / 共10页
第10页 / 共10页
亲,该文档总共10页全部预览完了,如果喜欢就下载吧!
资源描述
实验一 词法分析器的设计一、实验目的 (1)学会针对转换图实现相应的高级语言源程序。 (2)深刻领会状态转换图的含义,逐步理解有限自动机。二、实验内容 (1)某计算机语言的编译程序的词法分析部分实现。 (2)从左到右扫描每行该语言源程序的符号,拼成单词,换成统一的内部表示(token),送给语法分析程序。三、实现原理程序中先判断这个句语句中每个单元为关键字、常数、运算符、界符,对与不同的单词符号给出不同编码形式的编码,用以区分之。PL/0语言的EBNF表示:=;:=|;:=+|-:=*|/:=|#|=:=a|b|X|Y|Z:=0|1|2|8|9四、设计过程1 关键字:void,main,if,then,break,int,Char,float,include,for,while,printfscanf 并为小写。 2+”;”-”;”*”;”/”;”:=“;”:”;”“;”“;”=“;”“;”=“;”(“;”)”;”;”;”#”为运算符。3 其他标记 如字符串,表示以字母开头的标识符。 4 空格符跳过。 5 各符号对应种别码 关键字分别对应1-13运算符分别对应401-418,501-513。字符串对应100常量对应200结束符#五、心得体会其实匹配并不困难,主要是C+知识要求相对较高,只要把握住指针就好了。附源程序:#include#include#include #includeint i,j,k,flag,number,status;/*status which is use to judge the string is keywords or not!*/char ch;char words10 = ;char program500;int Scan(char program) char *keywords13 = void,main,if,then,break,int, char,float,include,for,while,printf, scanf;number = 0;status = 0;j = 0;ch = programi+; /* To handle the lettle space ands tab*/ /*handle letters*/if (ch = a) & (ch = a) & (ch = z )wordsj+=ch;ch=programi+; i-; wordsj+ = 0; for (k = 0; k = 0) & (ch = 0 ) & (ch : if (ch = ) wordsj+ = ch; wordsj = 0; ch = programi+; if (ch = =) wordsj+ = ch; wordsj = 0; flag = 403; else i-; flag = 404; break;case: if (ch = ) wordsj+ = ch; wordsj = 0; ch = programi+; if (ch = =) wordsj+ = ch; wordsj = 0; flag = 405; else i-; flag = 406; break;case!: if (ch = !) wordsj+ = ch; wordsj = 0; ch = programi+; if (ch = =) wordsj+ = ch; wordsj = 0; flag = 407; else i-; flag = 408; break;case+: if (ch = +) wordsj+ = ch; wordsj = 0; ch = programi+; if (ch = =) wordsj+ = ch; wordsj = 0; flag = 409; else if (ch = +) wordsj+ = ch; wordsj = 0; flag = 410; else i-;flag = 411; break;case-: if (ch = -) wordsj+ = ch; wordsj = 0; ch = programi+; if (ch = =) wordsj+ = ch;wordsj = 0;flag = 412; else if( ch = -) wordsj+ = ch;wordsj = 0;flag = 413; else i-;flag = 414; break;case*: if (ch = *) wordsj+ = ch; wordsj = 0; ch = programi+; if (ch = =) wordsj+ = ch; wordsj = 0; flag = 415; else i-; flag = 416; break; case/: if (ch = /) wordsj+ = ch; wordsj = 0; ch = programi+; if (ch = =) wordsj+ = ch; wordsj = 0; flag = 417; else i-;
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号