ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:18.84KB ,
资源ID:3730126      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3730126.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(编译原理词法分析器.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

编译原理词法分析器.docx

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