實驗四 用Yacc實現(xiàn)的語法分析器_第1頁
實驗四 用Yacc實現(xiàn)的語法分析器_第2頁
實驗四 用Yacc實現(xiàn)的語法分析器_第3頁
實驗四 用Yacc實現(xiàn)的語法分析器_第4頁
實驗四 用Yacc實現(xiàn)的語法分析器_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、實驗4 用Yacc工具構(gòu)造語法分析器一、實驗?zāi)康恼莆找七M(jìn)-歸約技術(shù)語法分析技術(shù),利用語法分析器生成工具Yacc/Bison實現(xiàn)語法分析器的構(gòu)造。二、實驗內(nèi)容利用語法分析器生成工具Yacc/Bison編寫一個語法分析程序,與詞法分析器結(jié)合,能夠根據(jù)語言的上下文無關(guān)文法,識別輸入的單詞序列是否文法的句子。三、實驗要求個人完成,提交實驗報告。實驗報告中給出采用測試源代碼片斷,及其對應(yīng)的最右推導(dǎo)過程(形式可以自行考慮,如依次給出推導(dǎo)使用的產(chǎn)生式)。例如,程序片斷4、 實驗步驟1、 根據(jù)文法編寫.y文件:%#include<ctype.h>#include<stdio.h>#de

2、fine YYSIYPE doublevoid yyerror(char *s)printf("%sn",s);%token NUM%token BASIC IF ELSE DO BREAK REAL TRUE FALSE ID LE INDEX%token WHILE AND OR EQ NE GE%left '+''-'%left '*''/'%right UMINUS%program : block printf("program->blockn");block: '&#

3、39;decls stmts'' printf("block->decls stmtsn");decls: decls decl printf("decls->decls decln");| printf("decls->En");decl: type ID '' printf("decl->type idn");type: type '' NUM '' printf("type->type numn"

4、);| BASIC printf("type->basicn");stmts: stmts stmt printf("stmts->stmts stmtn");| printf("stmts->En");stmt: loc '=' bool '' printf("stmt->loc=booln");|IF '(' bool ')' stmt printf("stmt->if(bool) stmtn");

5、|IF '(' bool ')' stmt ELSE stmt printf("stmt->if(bool) stmt else stmtn");|WHILE '(' bool')' stmt printf("stmt->while(bool) stmtn");|DO stmt WHILE '(' bool ')' '' printf("stmt->->do stmt while(bool)n");|

6、BREAK '' printf("stmt->breakn");| block printf("stmt->blockn");loc: loc '' bool '' printf("loc->locbooln"); |ID printf("loc->idn");bool: bool OR join printf("bool->bool|joinn");| join printf("bool->joinn

7、");join: join AND equality printf("join->join && equalityn");| equality printf("join->equalityn");equality : equality EQ rel printf("equality->equality=reln");| equality NE rel printf("equality->equality!=reln");| rel printf("equa

8、lity->reln");rel: expr '<' expr printf("rel->expr<exprn");| expr LE expr printf("rel->expr<=exprn");| expr '>' expr printf("rel->expr>exprn");| expr GE expr printf("rel->expr>=exprn");| expr printf("re

9、l->exprn");expr: expr '+' term printf("expr->expt+termn");| expr '-' term printf("expr->expr-termn");| term printf("expr->termn");term: term '*' unary printf("term->term*unaryn");| term '/' unary printf("

10、;term->term/unaryn");| unary printf("term->unaryn");unary: '!' unary printf("unary->!unaryn");| '-' unary printf("unary->-unaryn");| factor printf("unary->factorn");factor: '(' bool ')' printf("factor-&g

11、t;(bool)n");| loc printf("factor->locn");| NUM printf("factor->numn");| REAL printf("factor->realn");| TRUE printf("factor->truen");| FALSE printf("factor->falsen");%2、 在.y文件中添加main()函數(shù),以及一些外部函數(shù)的聲明:#include "lex.yy.c"int

12、 yyparse();extern void BeginCompileOneFile( const char * filename );extern void EndCompileOneFile(void);void main() char filename200;printf("請輸入源程序文件名:"); scanf("%s:",filename);BeginCompileOneFile( filename ); yyparse();EndCompileOneFile();3、 修改實驗三的.l文件,刪除main()函數(shù):4、5、6、 在VC下建立工程,添加lex.yy.c和my.tab.c文件7、 以文件讀入方式進(jìn)行測試,測試?yán)訛椋篿 = 2;while

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論