欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    2023年语义分析实验报告2.docx

    • 资源ID:72766053       资源大小:23.83KB        全文页数:9页
    • 资源格式: DOCX        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    2023年语义分析实验报告2.docx

    实验三语法分析 科3 李君林一.实验目的:通过使用、剖析和扩充TINY语言的语义分析程序,掌握编译器的语义分析 程序的构造方法。二.实验内容(一)运营T INY的语义分析程序(二)扩充T I NY的语法分析程序提醒:。考虑作用域(如:函数)和数组时也许需要修改符号表。三,实验环节.先读懂T INY语义程序(相关联的文献:MAIN. C ANA L YZE.C AN ALYZE.H)(1) b u i 1 d Sym t ab(s y nt a xTree);根据语法树建立符号表通过递归调用 tr a verse(syntaxTr e e, insert No de, null P r oc);进行 static void insertN o d c ( Tre e Node * t ),这样将碰到与 I D 有关的 N o de 信息通过 voi d st_in s ert( char * name, int li n e no, i n t loc,int len )加入到 hashT a blch数据结构中。(2 )接着调用typeCheck (syntaxTre e);进行类型检测通过递归调用 tr a ver se(synt a xT r e e, n ullProc, checkN o de);将语法树遍 历然后调用s t atic voi d c heckNode(T r eeNo d e * t)对节点进行类型检测2.扩充TINY的语法分析程序本次实验我一方面将源程序实现的功能改成符合C.MINUS的符号表与类型检 测a dd 1 ine num be r o f us e only */st_insert(t->attr.name,t- >1 i nen o , 0,-1);b r eak;。 d e fau 1 t:bre a k;)b rea k;d e f a ult: break;)/* F unction b uildSymtab const r u c ts the symbo 1* t a ble b y preor d er t rav e rsai oflhesy n t a x tree*/v o id b u i ldSymtab(Tre e N ode * sy n taxTree)(fp r i n t f(li s ting/'XnunDecErr o r an d arra y Cal 1 Er r or c h eckn");trav e r s e(syn t ax T r e e,inse r tNode,nullPr o c);f printf(lis ting ," n u n De c Err o r and a r ra y C allE r r or ch e ck fi n i s h ed n");if (Tra c eA n al y ze)(i f (T r ac e Ana 1 yz e ) f printf( 1 i s t i ng," nB u i 1 di n g Sym b o 1 Table. An");prin t SymTab(l i sti n g);/* P roced u re c heck Nod epe r fo r ms* t ype ch e cki n g at a s ingle t ree n ode*/s t a t ic v oid check Node (Tree N ode * t)(sw i t c h (t-> n o dek i nd) c ase Exp K:switc h ( t > k ind.ex p ) c ase OpK:if ( t ->child 0 ->(y p e != I n leger) |(t-> c h i 1 d 1 ->typc != Int e g er)t ypeErro r (t,"O p appl i e d t o no n - i nl e g er ");if (t->attr. op = EQ) | (t-> a t tr. o p = LT) | (t>attr. o p = BG)g | I ( t ->attr. op = LE) | ( t ->att r .o p = BG) | (t->attr.o p = U N EQ)t->t y pe = Bo o lean;el s et-> t y p e = Int e ger;b r e a k;c a se C o nstK:c a se I d K:t > t ype = Integer;def ault:brea k;)b r eak;case Stm t K :sw i tch (t-> k ind.stmt) c a s e Se 1 K:i f (t->ch i ld 0 ->t y p e = I nt e g e r)ty p e E rro r (t->child0, " i f t e s t i s no t Boo 1 ean "); bre a k;case I teK:i f (t- >ch i Id 0>typ e = In t e g er)t y peError( t ->c h i ldOb "while tes t i s no t Boolean"); break;ca s e W r iteK:i f (t-> c h ild0-> t ypc ! = Int e ger)“ y p e E r ror(t->c h ild 0 /'w r i( e of non- i nl e g er v a 1 u e '* ); obrea k ;def a u 1 t: break;)bre a k;default:/ * Pr o ce d u rc t y p e Ch e c k performs ty p e c h eck i n g* b y a po s t o rde r s y n t a x tree traversa 1vo i d (yp e Check( Tre eNode * syntaxTr e e)otra v e r s e (s y ntaxTree, n ullPr o c,c h ec k Node);然后加入没中明调用与数组调用错误即数组没申明而调用数组类型。四.实验结果1 .对的的测试程序/ */i n t g c d (i n t u, i nt v j)(if( v = 0 )r e t u r n u;elseretu r n g c d(v,u);)voi d main(void)int x;i n t y;rea d x ;x =y=2;w h i 1 e(x>0)y=y-hwrite y;re t urn (gcd(x,y);)/ */运营结果:unDecErroi* and arrayCa 1 lError checkunDecError and arrayCa1lError check f inishedQBuilding Symbol Table .Uariable NameLocationLineNumbersmain391257u2247X411111314y511111315gcd02717Checking Types Type CheckingFinishedPress any keyto continue17151617经检查测试程序代码无语义错误2 .错误测试程序/ */int gcd (i n t u ,in t v)i f ( v =0)retur n u;e Iser e t u rn gc d (v, u );)vo i d main(void)int x; i n t y;re a d x ;t=l;x=y= 2 ;wh i le(x> 0 )y=y-l;wr i te y;re t u r n ( g cd(x,y);/*/实验结果:unDecError Type error Type errorkm DecErrorand arvayCallError check at line 13: the t doesn't declaration at line 15: the ID x isn't a Array and arrayCa1lError check f inishedBuilding Symbol Table.Uariable NameLocationLineNumbersmain3912572247X41111141619y51111141717gcd02719becking Types.1819Type Checking Finished Press any key to continue检测到13行t没有申明 检测到15行 x不是一个数组五.实验心得通过本次实验学会了使用、剖析和扩充TIN Y语言的语义分析程序,掌握 编译器的语义分析程序的构造方法。加深了对书本语义分析的理解,感受到学以 致用的快感,增强对本课程的爱好。实验中碰到的最大问题:如何查询符号表判断 数组,后面在其数据结构中增长了一个属性Le n,假如不是数组将其赋为-1.六.关键程序代码(ANALYZE.C)/* * * * * * * * * * * * * * * * */* File: analy z e.c*/ * Sema ntic an a 1 y zer i m p lementa t i on* /* fo r the TINY com p i 1 er* / * C om p iler Con stru c tion: Pr i n c i p les and P r a ctice * /* Ke n neth C. Lou den* / * 木* * * *水* * *:木* * * *水* * 木水* * * *火* *#i n clu d e M g 1 obals.h"#i n clu d e H s y mt a b ,h"#inc I udc "anal y z c.h"/* c o u n t er fbr v a r iable m e inory 1 o c a ti o n s */stati c i nt 1 o cation = 0;/* Procedur e tr a ver s e i sa gen eric re c u r s i ve* s y ntax tree t r a ve r sal rou t i ne:* it a p p 1 ies pr e Pr o c in p re o r d e r and poslProc* i n po s t o r d e r to t re e p o i n t ed to by t*/st a t i c v o id t r a ver s e( T reeN o de 火 t,vo i d (* prcP r oc) (TrceNo d e *),voi d (* post P roc) (TreeNode *) if ( t != NULL) preProc (t); i nti;for (i=0;i < MAXCHILDREN; i+)t r averse(t->ch i 1 di , p reP r oc, p o stProc):)p o s t Pr o c( t );t r a verse(t->sibl i ng,preP r oc,p o stProc);)/* null Proc i s a do-n o t h i ng pro c ed u r e to* generate pre ord e r -only o r p ost o r der-only* traversals from trav e r se* /stati c v oid n u HProc(T r e eNode * t) if ( t =NULL) r etum;else re t u r n;Istatic voi d lypeEr r or(T r e e N o de * t, char * me s sa g e ) f p r i nt f (listing,"Type e r ror a t line %d: %sn " , t ->lin e n o ,m e s sa g e);Error = TRU E;stati c void u nDe c Error( T r e eN o de * t )fp r i n tf( 1 i s ti n g ,n T ype error at line %d: the %s doesn't declarat i on n't->l i n e no, t - >at t r. name);Error = TRU E;)s t a t ic vo i d no t A rrayEir o r( T reeN o de * t)fpr i n t f ( 1 i sting, " Type er r or at line %d: the ID %s i snz t a Arra y n", t->l i ne n o , t - >at t r . name);Error = TRUE;/* Pro c e dure i n se r tNode i n se r ts* ident i f i ers s t ored i n t i ntothe symbol table */sta t ic v o id insertN o d e( Tr e e No d e * t ) switc h (t->n o de k i n d ) case S t mtK : swit c h ( t ->k i n d .simt)d e f a ult: brea k;) b re a k:cas e Exp K :switch (t->k i nd. e xp) case IdK:if (s t _lo o k up( t ->a t tr.nam e ) = -1 ) (/* n ot yet i n ta b le, s o t r eat as n e w def i ni t ion */。 unDec E rr o r ( t );g / /st_ i nsert(t->attr. name,t- >line n o,location+,0);a ) g cis e/ * already i n table, so ignor e 1 ocat i o n,a dd li n e number of use on 1 y * /g“/ pr i ntf("LEN :%d n " , t -> 1 e ngt h );if(t->l e ng t h! = 1 &&st_ i s Arra y (t->at t r. n ame) =-1 ) n o tAr r ayErr o r(t);。 else。 s t _ins e rt(t->attr.name, t ->l i ne n o,0,-l);00break;defaul t :b r eak;)brea k;oc a s e DecK :« switch ( t ->kind.dec k )。case VarK:if (st_1 o o ku p (t->att r .n a me) = -1)° (/ * no t yet i n t a b 1 e , s o t r eat as n ew d ef i nitio n */g。 i f (t->len g t h =-l) st_in s e r t ( t ->attr.name, t >line n o, lo c ation+, 1);g。 e lse(8gst_in s er t (t->attr. name, t> 1 ine n o,loc a tion+,t-> 1 en g th);)if (t>leng t h ! =-l)® 1 oc a tion+=t- >lengt h -1;else/* al read y i n tabi e , s o ignore 1 o c a tio n , o add line number o f use only * /s t _i n sert(->atl r .n a me,t-> 1 inen o ,0> - 1);° c ase Pa r aK:。 «i f (st_ 1 ookup( t ->at t r .name) = -1)/* no t ye t i n table, so ire a t as new de f inition */。 if (t>le n g th=-l) g 。 s t _ i nsert( t >attr.nam e ,t-> 1 i n e n o ,1 o ca t ion+<l);g ) e 1 s e(g。 s t _inse r t (t->a t t r .name, t-> 1 i n e n o ,1 o ca t ion+,t->l e ngt h );。 »els e/* a Ir e a d y in t able, s o ignore localio n, ad d I i ne n um b er of use o n ly */ st_inser t (t->a t tr.n a m e , t->lineno,0,-l); break;a s e FunK:i f (st_lo o k up(t->attr. n a me) = = 1)/ * n o t yet i n tabi e , so tre a t a s new d ef i niti o n * / st_ins e rt(t >a t tr. nam e ,t>lin e n o,loc a tion+4-,-1); else/* alrea d y i n table, s o ig n o re 1 o cation.

    注意事项

    本文(2023年语义分析实验报告2.docx)为本站会员(太**)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开