1、编译原理词法分析器/ LexAnal.cpp: implementation of the CLexAnal class./#include stdafx.h#include LexAnal.h/ Construction/Destruction/CLexAnal:CLexAnal()CLexAnal:CLexAnal() result.clear(); symbol.clear();bool CLexAnal:OpenSrcFile(char *szFilename) s = 200; e = 0; fs.open(szFilename, ios_base:in); if (true=fs.
2、fail() coutCant open source file!n; return false; buffer2*BUFFERLEN=0; / zero end for (int i = 0; i =a)&(ch=A)&(ch=0)&(ch0) e-; else e=BUFFERLEN*2-1; ch = ; col-;int CLexAnal:Reserve() for (int i = 0; i 30; i+) if (!strcmp(keywordsi,strToken) return i+1; return 0;int CLexAnal:InsertId() SYMBOL sym;
3、for (int i = 30; i symbol.size(); i+) if (symboli.name = strToken ) break; if (i symbol.size() return i; / this symbol already exists else sym.kind = 34; sym.name = strToken; sym.type = ?; / its type needs further syntax analysis sym.value = ; symbol.push_back(sym); return i;int CLexAnal:InsertConst
4、(char type) SYMBOL sym; for (int i = 30; i symbol.size(); i+) if (symboli.value = strToken ) break; if (i symbol.size() return i; / this symbol already exists else sym.kind = (type = N)? 37:38 ; sym.name = const; sym.type = type; sym.value = strToken; symbol.push_back(sym); return i;/-void CLexAnal:
5、DoAnal()/添加代码 int code; int value; bool running = true; eosrc = false; e=2*BUFFERLEN; Fetch(); s = e = 0; t=0; strTokent=0; while (running) GetChar(); GetBC(); if (ch=0) break; / analysis finished s = e-1; if (IsLetter() while (IsLetter()|IsDigit() Concat(); GetChar(); Retract(); code = Reserve(); i
6、f (code = 0) value = InsertId(); OutputId(value); / 输出标志符二元式 else OutputKeyword(code); / 输出关键字二元式 else if (IsDigit() while (IsDigit() Concat(); GetChar(); if (IsLetter() coutError at (row,col) : illegal identifier endl; Retract(); else Retract(); value = InsertConst(N); / number type OutputConstInt(
7、value); / 输出整数常量二元式 else if (ch = +) OutputSD(39); / + else if (ch = -) OutputSD(40); / - else if (ch = *) OutputSD(41); / * else if (ch = /) GetChar(); if (ch = *) / 跳过注释行 while (ch!=0) while ( (ch != *) & (ch != 0) ) GetChar(); GetChar(); if (ch = /) break; if (ch = 0) coutunexpected end of file!e
8、ndl; running = false; else Retract(); OutputSD(42); / / else if (ch = =) OutputSD(43); / = else if (ch = ) OutputDD(54); / else if (ch = =) OutputDD(55); / = else Retract(); OutputSD(44); / ) GetChar(); if (ch = =) OutputDD(56); / = else Retract(); OutputSD(45); / else if (ch = () OutputSD(46); / (
9、else if (ch = ) OutputSD(47); / ) else if (ch = ) OutputSD(48); / else if (ch = ) OutputSD(49); / else if (ch = :) GetChar(); if (ch = =) OutputDD(57); / := else Retract(); OutputSD(50); / : else if (ch = .) GetChar(); if (ch = .) OutputDD(58); / . else Retract(); OutputSD(51); / . else if (ch = ;)
10、OutputSD(52); / ; else if (ch = ,) OutputSD(53); / , else if (ch = ) Concat(); / 寻找字符串末尾 GetChar(); Concat(); while ( (ch != ) & (ch != 0) ) GetChar(); Concat(); if (ch = 0) coutunexpected end of string!endl; running = false; else value = InsertConst(S); / string type OutputConstStr(value); / 输出字符串常
11、量 else coutError at (row,col) chendl; t=0; strTokent=0; / reset strToken / ends while/-void CLexAnal:OutputKeyword(int code) RESULT re;/ coutcode keywordscode-1endl; / 输出关键字二元式 re.kind = code; re.value = code-1; result.push_back(re);void CLexAnal:OutputId(int value) RESULT re;/ cout34,value strToken
12、endl; re.kind = 34; re.value = value; result.push_back(re);void CLexAnal:OutputSD(int code) RESULT re;/ coutcode sdcode-39endl; / + re.kind = code; re.value = -1; result.push_back(re);void CLexAnal:OutputDD(int code) RESULT re;/ coutcode ddcode-54endl; / + re.kind = code; re.value = -1; result.push_
13、back(re);void CLexAnal:OutputConstInt(int value) RESULT re;/ cout37,value strTokenendl; / 输出整数常量二元式 re.kind = 37; re.value = value; result.push_back(re);void CLexAnal:OutputConstStr(int value) RESULT re;/ cout38,value strTokenendl; / 输出字符串常量二元式 re.kind = 38; re.value = value; result.push_back(re);vo
14、id CLexAnal:ShowResult() int kind; cout-二元式列表-endl; coutkind value tokenendl; for (int i=0; iresult.size(); i+) kind = resulti.kind; coutkind,resulti.value ; if (resulti.value =0 ) cout=39 & kind =53) cout=54 & kind=58) coutddkind-54; coutendl; void CLexAnal:ShowSymbol() cout-符号表-endl; coutNo. kind
15、name type valueendl; for (int i=0; isymbol.size(); i+) couti symboli.kind symboli.name symboli.type symboli.value 1) strcpy(fn,argv1); if (lex.OpenSrcFile(fn) lex.DoAnal(); lex.ShowResult(); lex.ShowSymbol(); lex.CloseSrcFile(); return 0;编译原理第一次上机实验要求: 一 上机题目: 1 实验题目: 一种简单语言的词法分析程序的设计与实现 2 简单语言SIMPL
16、E的定义: l 字符集 | A | B | | Z | a | b | | z 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 + | - | * | / | = | | ( | ) | | | : | . | ; | , | “ (注:程序以 . 结束,“无种别码。) 2 单词集 | and | array | begin | call | case | char | constant | do | else | end | for | if | input | integer | not | of | or | output | procedure | prog
17、ram | read | real | repeat | set | then | to | until | var | while | write | = | := | . . | /* | */ | | | 用双引号引起来的任意字符串 3 要求 词法分析程序作一遍扫描,采用双缓冲区结构,缓冲区大小为2*64,保留字采用一字一类,其种别码依字典顺序取1至30,标识符的种别码为34,其自身值为该常数在符号表中的行号(位置),字符串常数的种别码为38,其自身值为该常数在符号表中的行号(位置),单界限符采用一符一类,种别码依次为39至53,双界限符采用一符一类,种别码依次取54至58。 输入:源程
18、序文件 输出:二元式文件 符号表文件 出错信息文件 附: (1)源程序: program test; /* this is a tested simple program constant p=8;T=256; /*define constant p & T*/ var a,b,c,i:integer; abc,ab:char; #a:boolean; kjh,def:array2.10,6.21 of integer; begin abc:=I am a student; for I:=2to 10 do kjhi:=i*2+t/i; for a:=6 to21 do if kjha/310
19、a then defa:=6 else defa:=a+6; b:=2; while b21 do begin c:=c+defb;b:=b+1; ab:=abc+I am a student+123 end; end (2)二元式文件 (20,-) (34,1) (52,-) (7,-) (34,2) (43,-) (36,3) (52,-) (34,4) (43,-) (36,5) (52,-) (28,-) (34,6) (53,-) (34,7) (53,-) (34,8) (53,-) (34,9) (50,-) (14,-) (52,-) (34,10) (53,-) (34,11
20、) (50,-) (6,-) (52,-) (34,6) (50,-) (34,12) (52,-) (34,13) (53,-) (34,14) (50,-) (2,-) (48,-) (36,15) (58,-) (36,16) (53,-) (36,17) (58,-) (36,18) (49,-) (16,-) (14,-) (52,-) (3,-) (34,10) (57,-) (38,19) (52,-) (11,-) (34,20) (57,-) (36,15) (26,-) (36,16) (8,-) (34,13) (48,-) (34,9) (49,-) (57,-) (34,9) (41,-) (36,15) (39,-) (34,21)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1