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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

词法分析.docx

1、词法分析#include #include #include #include #include using namespace std;ifstream fp(source.txt,ios:in);char cbuffer;char *key13=if,else,for,while,do,return,break,continue,int,void,main,const,printf; /关键字char *border7= , , ; , , , ( , ) ,/; /分界符char *arithmetic6=+ , - , * , / , + , -; /运算符char *relation

2、7= , , = , = ,!=; /关系运算符 char *lableconst80; /标识符int constnum=40;int lableconstnum=0; int linenum=1; /统计常数和标识符数量 int search(char searchchar,int wordtype) int i=0,t=0; switch (wordtype) case 1: for (i=0;i=12;i+) /关键字 if (strcmp(keyi,searchchar)=0) return(i+1); return(0); case 2: for (i=0;i=6;i+) /分界符

3、 if (strcmp(borderi,searchchar)=0) return(i+1); return(0); case 3: for (i=0;i=5;i+) /运算符 if (strcmp(arithmetici,searchchar)=0) return(i+1); return(0); case 4: for (i=0;i=6;i+) /关系运算符 if (strcmp(relationi,searchchar)=0) return(i+1); return(0); case 5: for (t=40;t=constnum;t+) /常数 if (strcmp(searchcha

4、r,lableconstt)=0)/判断该常数是否已出现过 return(t+1); lableconstt-1=(char *)malloc(sizeof(searchchar);/为新的元素分配内存空间 strcpy(lableconstt-1,searchchar);/为数组赋值lableconst指针数组名 constnum+; /常数个数自加 return(t); case 6: for (i=0;i=lableconstnum;i+) if (strcmp(searchchar,lableconsti)=0) /判断标识符是否已出现过 return(i+1); lableconst

5、i-1=(char *)malloc(sizeof(searchchar); strcpy(lableconsti-1,searchchar); lableconstnum+; /标识符个数自加 return(i); default:cout错误!; char alphaprocess(char buffer) /字符处理过程 int atype; int i=-1; char alphatp20; while (isalpha(buffer)|(isdigit(buffer) /这两个函数分别是判字符和判数字函数位于ctype.h中 alphatp+i=buffer; fp.get(buff

6、er); alphatpi+1=0;/在末尾添加字符串结束标志 if (atype=search(alphatp,1) coutlinenum: linenum String= alphatpttt关键字endl; else atype=search(alphatp,6); /标识符 coutlinenum: linenum String= alphatpttt标识符endl; return(buffer);char digitprocess(char buffer) /数字处理过程 int i=-1; char digittp20; int dtype; while (isdigit(buf

7、fer) digittp+i=buffer; fp.get(buffer); digittpi+1=0; dtype=search(digittp,5); coutlinenum: linenum String= digittpttt数据endl; return(buffer);char otherprocess(char buffer) /分界符、运算符、逻辑运算符、等 int i=-1; char othertp20; int otype,otypetp; othertp0=buffer; othertp1=0; if (otype=search(othertp,3) fp.get(buf

8、fer); othertp1=buffer; othertp2=0; if (otypetp=search(othertp,3) /判断该运算符是否是 /由连续的两个字符组成的 coutlinenum: linenum String= othertpttt运算符endl; fp.get(buffer); goto out; else /单字符逻辑运算符 othertp1=0; coutlinenum: linenum String= othertpttt逻辑运算符endl; goto out; if (otype=search(othertp,4) /关系运算符 fp.get(buffer);

9、 othertp1=buffer; othertp2=0; if (otypetp=search(othertp,4) /判断该关系运算符是否是 /由连续的两个字符组成的 coutlinenum: linenum String= othertpttt关系运算符endl; fp.get(buffer); goto out; else /单字符逻辑运算符 othertp1=0; coutlinenum: linenum String= othertpttt逻辑运算endl; goto out; if (buffer=!) /=的判断 fp.get(buffer); if (buffer=) /co

10、ut!= (2,2)n; fp.get(buffer); goto out; else if (otype=search(othertp,2) /分界符 coutlinenum: linenum String= othertpttt分界符endl; fp.get(buffer); goto out; if (buffer!=n)&(buffer!= ) cout错误!,字符非法tttbufferendl; fp.get(buffer); out: return(buffer); void main() int i; for (i=0;i=50;i+) lableconsti= ;/用于保存标识

11、符 if (!fp) cout文件打开错误!endl; else fp.get (cbuffer); while (!fp.eof() if(cbuffer=n) linenum+; fp.get(cbuffer); else if (isalpha(cbuffer) cbuffer=alphaprocess(cbuffer); else if (isdigit(cbuffer) cbuffer=digitprocess(cbuffer); else cbuffer=otherprocess(cbuffer); cout标识符个数是:lableconstnum分别是endl; i=0; whi

12、le(ilableconstnum) coutlableconsti+ ; coutendl; cout amax then /如果是常量,判断是否大于最大值,若是则报30号错 begin error(30); num :=0 end; val := num /否则保存数值 end; varible: begin level := lev; adr := dx; dx := dx + 1; /如果是变量,填写变量内部表示,LEVEl是变量的层次,adr为地址 end; proc: level := lev /如果是过程,保存过程的层次 end end end enter;/查找符号表的位置 f

13、unction position(id: alfa): integer; var i: integer; begin find indentifier id in table /从后向前查找 table0.name := id; i := tx; /找到保存类型 while tablei.name id do i := i-1; position := i /返回标志符在符号表中的位置 end position; procedure block(lev,tx: integer; fsys: symset); var dx: integer; data allocation index /数据分

14、配索引 tx0: integer; initial table index /初始符号表索引 cx0: integer; initial code index /初始代码索引 procedure enter(k: object1); /填写符号表,下次分析 begin enter object into table tx := tx + 1; with tabletx do begin name := id; kind := k; case k of constant: begin if num amax then begin error(30); num :=0 end; val := nu

15、m end; varible: begin level := lev; adr := dx; dx := dx + 1; end; proc: level := lev end end end enter; function position(id: alfa): integer; /查找符号表,下次分析 var i: integer; begin find indentifier id in table table0.name := id; i := tx; while tablei.name id do i := i-1; position := i end position; proce

16、dure constdeclaration; /常量声明 begin if sym = ident then /如果是标志符,读入一个TOKEN begin getsym; if sym in eql, becomes then /读入的是等号或符值号继续判断 begin if sym = becomes then error(1); /如果是“=”报1号错 getsym; /读入下一个TOKEN if sym = number then /读入的是数字,填写符号表 begin enter(constant); getsym end else error(2) /如果不是数字,报2号错 end

17、 else error(3) /不是等号或符值号,报3号错 end else error(4) /如果不是标志符,报4号错 end constdeclaration; procedure vardeclaration; /变量声明 begin if sym = ident then /读入的是标志符,填写符号表 begin enter(varible); getsym end else error(4) /不是标志符,报4号错 end vardeclaration; procedure listcode; var i: integer; begin list code generated fo

18、r this block for i := cx0 to cx-1 do with codei do writeln(i:5, mnemonicf:5, 1:3, a:5) end listcode; procedure statement(fsys: symset); var i, cx1, cx2: integer; procedure expression(fsys: symset); /表达式分析 var addop: symbol; procedure term(fsys: symset); /项分析 var mulop: symbol; procedure factor(fsys:

19、 symset); /因子分析 var i: integer; begin test(facbegsys, fsys, 24); /读入的是“(”,标志符或数字 while sym in facbegsys do begin if sym = ident then /是标志符,查表 begin i:= position(id); if i = 0 then error(11) else /未找到,报11号错 with tablei do /找到,读入标志符类型 case kind of constant: gen(lit, 0, val); /写常量命令 varible: gen(lod, l

20、ev-level, adr);/写变量命令 proc: error(21) /过程名,报21号错 end; getsym /读入下一个TOKEN end else if sym = number then /读入的是数字 begin if num amax then /如果数字大于最大数,报30号错误 begin error(30); num := 0 end; gen(lit, 0, num); getsym /调用数字命令,读入下一个TOKEN end else if sym = lparen then /读入的是“(” begin getsym; expression(rparen+fs

21、ys); /调用表达式分析函数 if sym = rparen then getsym else error(22) /如果“(”后无“)”,报22号错 end; test(fsys, lparen, 23) end end factor;/因子分析结束 /项分析 begin term factor(fsys+times, slash); /调用因子分析程序 while sym in times, slash do /取得是乘、除号循环 begin mulop:=sym;getsym;factor(fsys+times,slash); /记录符号,调用因子分析 if mulop=times t

22、hen gen(opr,0,4) else gen(opr,0,5) /写乘除指令 end end term; begin expression if sym in plus, minus then /如果是加减号 begin addop := sym; getsym; term(fsys+plus,minus); /记录符号,调用项分析程序 if addop = minus then gen(opr, 0,1) /写加减指令 end else term(fsys+plus, minus); while sym in plus, minus do /如果是加减号循环 begin addop :

23、= sym; getsym; term(fsys+plus,minus); if addop=plus then gen(opr,0,2) else gen(opr,0,3) end end expression; /条件过程 procedure condition(fsys: symset); var relop: symbol; begin if sym = oddsym then /如果是判奇符 begin getsym; expression(fsys); gen(opr, 0, 6) /取下一个TOKEN,调用expression,填指令 end else begin express

24、ion(eql, neq, lss, gtr, leq, geq+fsys); if not(sym in eql, neq, lss, leq, gtr, geq) then /如果不是取到逻辑判断符号,出错.20 error(20) else begin relop := sym; getsym; expression(fsys); case relop of eql: gen(opr, 0, 8); / =,相等 neq: gen(opr, 0, 9); / #,不相等 lss: gen(opr, 0, 10); / ,大于 leq: gen(opr, 0, 13); / ,小于等于 e

25、nd end end end condition; begin statement if sym = ident then /如果是标识符 begin i := position(id); /查找符号表 if i = 0 then error(11) else /未找到,标识符未定义,报11号错 if tablei.kind varible then /如果标识符不是变量,报12号错 begin assignment to non-varible error(12); i := 0 end; getsym; if sym = becomes then getsym else error(13)

26、; /如果是变量读入下一个TOKEN,不是赋值号,报13好错;是则读入一个TOKEN expression(fsys); /调用表达是过程 if i 0 then /写指令 with tablei do gen(sto, lev-level, adr) end else if sym = callsym then /如果是过程调用保留字,读入下一个TOKEN begin getsym; if sym ident then error(14) else /不是标识符报14号错 begin i := position(id); if i = 0 then error(11) else /是标识符,未定义,报13号错 with tablei do /

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1