《编译原理课程设计-词法语法分析器.docx》由会员分享,可在线阅读,更多相关《编译原理课程设计-词法语法分析器.docx(20页珍藏版)》请在课桌文档上搜索。
1、编译原理课程设计Course Design of Compiling 课程代码3273526半期题目: 词法和语法分析器课程设计1-C语言词法分析器1.题目C语言词法分析2.容选一个能正常运行的c语言程序,以该程序出现的字符作为单词符号集,不用处理c语言的所有单词符号。将解析到的单词符号对应的二元组输出到文件中保存可以将扫描缓冲区与输入缓冲区合成一个缓冲区,一次性输入源程序后就可以进展预处理了3.设计目的掌握词法分析算法,设计、编制并调试一个词法分析程序,加深对词法分析原理的理解4.设计环境电脑语言环境语言环境:C语言CPU:i7HQ6700存:8G5.概要设计单词符号表,状态转换图5.1词法
2、分析器的结构词法分析程序的功能:输入:所给文法的源程序字符串。输出:二元组syn,token或sum构成的序列。词法分析程序可以单独为一个程序;也可以作为整个编译程序的一个子程序,当需要一个单词时,就调用此法分析子程序返回一个单词.为便于程序实现,假设每个单词间都有界符或运算符或空格隔开,并引入下面的全局变量与子程序:1) ch 存放最新读进的源程序字符2) strToken 存放构成单词符号的字符串3) Buffer字符缓冲区4struct keyType 存放保存字的符号和种别5.2待分析的简单词法1保存字break、case、char、const、int、do、while2运算符和界符=
3、 、+、-、 * 、/、%、,、;、?、#5.3各种单词符号对应的种别码单词符号种别码 单词符号种别码ID0sizeof24INT1static25auto2struct26break3switch27case4typedef28char5union29const6unsigned30continue7void31default8volatile32do9while33double10=34else11+35enum12-36extern13*37float14/38for15%39goto16,40if17;41int18(42long19)43register20?44return21cl
4、ear45short22#46signed23lettetletter|digit*47dight dight*485.3 状态转换图6.详细设计数据结构,子程序算法思想:首先设置3个变量:strToken用来存放构成单词符号的字符串;ch用来字符;struct keyType用来存放单词符号的种别码。扫描子程序主要局部流程如下列图所示。子程序结构:子程序名功能GETCHAR()读一个字符到 ch中GETBC()读一个非空白字符到ch中CONCAT()把CHAR 中字符连接到strToken 之后LETTER()判断CHAR 中字符是否为字母DIGIT()判断ch中字符是否为数字RESERVE
5、()用strToken中的字符串查找保存字表,并返回保存字种别码,假设返回零,那么非保存字RETRACT()把CHAR 中字符回送到缓冲区7.程序清单/ ConsoleApplication1.cpp : 定义控制台应用程序的入口点。/#includestdafx.h#includestdio.h#includestdlib.h#includeconio.h#includestring.h#defineN 47char ch;char strToken20;/存放构成单词符号的字符串char buffer1024;/字符缓冲区structkeyType char keyname256;int
6、value;KeyN = $ID,0 , $INT,1 , auto,2 , break,3 , case,4 , char,5 , const,6 , continue,7 , default,8 , do,9 , double,10 , else,11 , enum,12 , extern,13 , float,14 , for,15 , goto,16 , if,17 , int,18 , long,19 , register,20 , return,21 , short,22 , signed,23 , sizeof,24 , static,25 , struct,26 , switc
7、h,27 , typedef,28 , union,29 , unsigned,30 , void,31 , volatile,32 , while,33 , =,34 , +,35 , -,36 , *,37 , /,38 , %,39 , ,40 , ;,41 , (,42 , ),43 , ?,44 , clear,45 , #,46 ;void GetChar()/读一个字符到ch中int i;if (strlen(buffer)0) ch = buffer0;for (i = 0; i256; i+)bufferi = bufferi + 1;elsech = 0;void GetB
8、C()/读一个非空白字符到ch中int i;while (strlen(buffer) i = 0;ch = bufferi;for (; i= A&ch = a&ch = 0&ch = 9)returntrue;elsereturnfalse;int Reserve()/用strToken中的字符查找保存字表,并返回保存字种别码,假设返回0,那么非保存字int i;for (i = 0; i0; i-)bufferi = bufferi - 1;buffer0 = ch;ch = 0;keyType ReturnWord()strcpy(strToken, 0);int c;keyType
9、tempkey;GetBC();if (ch = A&ch = a&ch = 0&ch 0) ch = buffer0;for (i = 0; i256; i+)bufferi = bufferi + 1;elsech = 0;void GetBC()/读一个非空白字符到ch中int i;while (strlen(buffer) i = 0;ch = bufferi;for (; i= A&ch = a&ch = 0&ch = 9)returntrue;elsereturnfalse;int Reserve()/用strToken中的字符查找保存字表,并返回保存字种别码,假设返回0,那么非保
10、存字int i;for (i = 0; i0; i-)bufferi = bufferi - 1;buffer0 = ch;ch = 0;keyType ReturnWord()strcpy(strToken, 0);int c;keyType tempkey;GetBC();if (ch = A&ch = a&ch = 0&ch = 9) ConCat();GetChar();while (Digit() ConCat();GetChar();Retract();strcpy(tempkey.keyname, strToken);tempkey.value = 1;else ConCat()
11、;strcpy(tempkey.keyname, strToken);tempkey.value = Reserve();return tempkey;bool GetwordStack() int i;wordStack.len = 0;keyType temp;while (strlen(buffer) temp = ReturnWord();/词法分析器获得一个分析词if (temp.value = 1)/常数$c,10strcpy(wordStack.elemwordStack.len.word, temp.keyname);wordStack.elemwordStack.len.va
12、lue = 10;elseif (temp.value = 0)/变量$i,9strcpy(wordStack.elemwordStack.len.word, temp.keyname);wordStack.elemwordStack.len.value = 9;else for (i = 0; iNUM; i+) if (strcmp(temp.keyname, wordTypei.word) = 0)/关键字wordStack.elemwordStack.len = wordTypei;break;elseif (i = NUM - 1) printf(输入串中出现未识别单词!n);ret
13、urnfalse;wordStack.len+;wordStack.elemwordStack.len+ = wordType12;returntrue;/*从单词串中取单词*/WordType GetWord() WordType temp = wordStack.elem0;for (int i = 0; iwordStack.len - 1; i+)wordStack.elemi = wordStack.elemi + 1;wordStack.len-;return temp;void ClearwordStack()/清空单词串wordStack.len = 0;void Clearm
14、ainStack()/清空归约栈mainStack.len = 0;/*查看变量在变量表中的位置*/int CheckvarTable(chara) for (int i = 0; iM; i+)if (strcmp(a, varTable.elemi.varname) = 0)return i;elseif (i = M - 1)return -1;/*添加变量*/void AddvarTable(VarWorda) varTable.elemvarTable.len =a;varTable.len+;/*初始化归约栈*/void InitmainStack()mainStack.elem0
15、 = wordType12;mainStack.len = 1;/*添加归约栈*/void AddmainStack(WordTypea)mainStack.elemmainStack.len =a;mainStack.len+;/*归约*/bool Handle()int i;/常量归约if (mainStack.elemmainStack.len - 1.value = 10) mainStack.elemmainStack.len - 1.value = 13;/变量归约elseif (mainStack.elemmainStack.len - 1.value = 9) mainStac
16、k.elemmainStack.len - 1.value = 13;i = CheckvarTable(mainStack.elemmainStack.len - 1.word);if (i0) printf(n变量 %s 未定义!, mainStack.elemmainStack.len - 1.word);returnfalse;elsestrcpy(mainStack.elemmainStack.len - 1.word, varTable.elemi.value);/赋值归约elseif (mainStack.elemmainStack.len - 2.value = 1) if (
17、mainStack.elemmainStack.len - 3.value = 9) i = CheckvarTable(mainStack.elemmainStack.len - 3.word);if (i0) VarWord temp;strcpy(temp.varname, mainStack.elemmainStack.len - 3.word);strcpy(temp.value, mainStack.elemmainStack.len - 1.word);temp.flag = true;AddvarTable(temp);elsestrcpy(varTable.elemi.val
18、ue, mainStack.elemmainStack.len - 1.word);strcpy(mainStack.elemmainStack.len - 3.word, mainStack.elemmainStack.len - 1.word);mainStack.elemmainStack.len - 3.value = 13;elseif (mainStack.elemmainStack.len - 3.value = 13)strcpy(mainStack.elemmainStack.len - 3.word, mainStack.elemmainStack.len - 1.word
19、);mainStack.len = mainStack.len - 2;/运算归约elseif (mainStack.elemmainStack.len - 2.value = 3) int a, b;a = atoi(mainStack.elemmainStack.len - 1.word);b = atoi(mainStack.elemmainStack.len - 3.word);a = a + b;itoa(a, mainStack.elemmainStack.len - 3.word, 10);mainStack.len = mainStack.len - 2;elseif (mai
20、nStack.elemmainStack.len - 2.value = 5) int a, b;a = atoi(mainStack.elemmainStack.len - 1.word);b = atoi(mainStack.elemmainStack.len - 3.word);a = a*b;itoa(a, mainStack.elemmainStack.len - 3.word, 10);mainStack.len = mainStack.len - 2;/输出语句elseif (wordStack.elem0.value = 2 & mainStack.elemmainStack.
21、len - 1.value = 13 & mainStack.elemmainStack.len - 2.value = 12) printf(n表达式的值为 %s, mainStack.elemmainStack.len - 1.word);mainStack.len = 0;/完毕归约elseif (mainStack.elemmainStack.len - 2.value = 12 & wordStack.elem0.value = 12)mainStack.len = mainStack.len - 2;returntrue;bool MainHandle() while (wordS
22、tack.len)if (prioritymainStack.elemmainStack.len - 1.valuewordStack.elem0.value != 1)AddmainStack(GetWord();else if (mainStack.len) /printf(n);for (int i = 0; imainStack.len; i+)printf(%s,%d), mainStack.elemi.word, mainStack.elemi.value);Handle();break;if (mainStack.len)MainHandle();returntrue;/*主函数
23、*/int main() int ok = 1;WordType temp;int ii = 0;while (ok) printf(n);printf(输入第 %d 语句串:,+ii);scanf(%s, buffer);printf(单词串:);if (!GetwordStack()return 0;for (int i = 0; iwordStack.len; i+)printf(%s,%d), wordStack.elemi.word, wordStack.elemi.value);printf(n当前归约栈:);InitmainStack();MainHandle();printf(完毕n);return 0;八、 运行结果九、 实验体会通过本次语法分析设计实验,我加深了对语法分析过程的理解。并在实际的设计过程深入的了解了编译原理思想。对编译原理课程有了更深的理解20 / 20