freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

編譯原理試驗(yàn)手冊(cè)(編輯修改稿)

2024-07-26 16:44 本頁(yè)面
 

【文章內(nèi)容簡(jiǎn)介】 rgv[0], r )。 else yyin = stdin。yylex()。 /*調(diào)用詞法分析器*/}int yywrap() { return 1。}flex //用VC++ 。運(yùn)行這個(gè)可執(zhí)行文件,會(huì)返回給你數(shù)字,標(biāo)識(shí)符,關(guān)鍵字,運(yùn)算符等.yyin標(biāo)準(zhǔn)輸入或自己制定輸入流yyout標(biāo)準(zhǔn)輸出或自己制定輸入出流注:如果你要將你的輸出定向到文件,你可以重新指定yyout,只要將上面例程中的printf函數(shù)改造為fprintf即可. 實(shí)驗(yàn)二這一部分的重點(diǎn)是掌握語(yǔ)法分析程序生成器bison的用法。用Lex/Yacc構(gòu)件編譯程序的過(guò)程簡(jiǎn)圖:source(yyparse)YACC(bison) VCCompiler output(yylex)LEX(flex)Bison語(yǔ)法格式如下:%{C declarations%}Bison declarations%%Grammar rules%%Additional C code 例程一是執(zhí)行加減運(yùn)算的簡(jiǎn)單計(jì)算器,通過(guò)這個(gè)例程我們要掌握如何將詞法分析程序和語(yǔ)法分析程序整合到一體詞法分析程序():/* calculator 1 */%{ include include void yyerror(char *)。%}%%[09]+ { yylval = atoi(yytext)。 return INTEGER。 }[+\n] { return *yytext。 }[ \t] 。 /* skip whitespace */. yyerror(Unknown character)。%%int yywrap(void) { return 1。}flex //語(yǔ)法分析程序:()%{ include int yylex(void)。 void yyerror(char *)。%}%token INTEGER%%program: program expr 39。\n39。 { printf(%d\n, $2)。 } | 。expr: INTEGER | expr 39。+39。 expr { $$ = $1 + $3。 } | expr 39。39。 expr { $$ = $1 $3。 } 。%%void yyerror(char *s) { fprintf(stderr, %s\n, s)。}int main(void) { yyparse()。 return 0。 }bison –d –b y // (沖突解決方案參照Bison文檔) 編譯運(yùn)行!:注:我們的語(yǔ)法分析器沒(méi)有錯(cuò)誤恢復(fù)功能,所以任何的語(yǔ)法錯(cuò)誤都將導(dǎo)致我們的分析器退出. 實(shí)驗(yàn)三在實(shí)驗(yàn)二的基礎(chǔ)上我們?cè)诰幦齻€(gè)計(jì)算器的例程,通過(guò)這兩個(gè)例程我們將進(jìn)一步理解flex和Bison的整合流程:main()yyparse()yylex()yyparse():語(yǔ)法分析函數(shù)。yylex():詞法分析函數(shù)當(dāng)yyparse()需要token時(shí)調(diào)用yylex()得到即可.需要注意的:Bison生成的是一個(gè)LALR分析表,yyparse()在執(zhí)行規(guī)約的同時(shí)會(huì)執(zhí)行語(yǔ)義動(dòng)作,所以我們?cè)谧隼虝r(shí)一定要對(duì)照課本的知識(shí)點(diǎn).例程一:(“Reverse Polish CALCulator”)%{define YYSTYPE doubleinclude include include %}%token NUM%% /* Grammar rules and actions follow */input: /* empty */ | input line。line: 39。\n39。 | exp 39。\n39。 { printf (\t%.10g\n, $1)。 }。exp: NUM { $$ = $1。 } | exp exp 39。+39。 { $$ = $1 + $2。 } | exp exp 39。39。 { $$ = $1 $2。 } | exp exp 39。*39。 { $$ = $1 * $2。 } | exp exp 39。/39。 { $$ = $1 / $2。 } /* Exponentiation */ | exp exp 39。^39。 { $$ = pow ($1, $2)。 } /* Unary minus */ | exp 39。n39。 { $$ = $1。 }。%%main (){ yyparse ()。}yyerror (s) /* Called by yyparse on error */ char *s。{ printf (%s\n, s)。}yylex (){ int c。 /* skip white space */ while ((c = getchar ()) == 39。 39。 || c == 39。\t39。) 。 /* process numbers */ if (c == 39。.39。 || isdigit (c)) { ungetc (c, stdin)。 scanf (%lf, amp。yylval)。 return NUM。 } /* return endoffile */ if (c == EOF) return 0。 /* return single chars */ return c。 }例程二:/* Infix notation calculatorcalc */%{define YYSTYPE double /*重新定義詞法分析返回的token類(lèi)型 */include %}/* BISON Declarations */%token NUM%left 39。39。 39。+39。%left 39。*39。 39。/39。%left NEG /* negationunary minus */%right 39。^39。 /* exponentiation *//* Grammar follows */%%input: /* empty string */ | input line。line: 39。\n39。 | exp 39。\n39。 { printf (\t%.10g\n, $1)。 }。exp: NUM { $$ = $1。 } | exp 39。+39。 exp { $$ = $1 + $3。 } | exp 39。39。 exp { $$ = $1 $3。 } | exp 39。*39。 exp { $$ = $1 * $3。 } | exp 39。/39。 exp { $$ = $1 / $3。 } | 39。39。 exp %prec NEG { $$ = $2。 } | exp 39。^39。 exp { $$ = pow ($1, $3)。 } | 39。(39。 exp 39。)39。 { $$ = $2。 }。%%main (){ yyparse ()。}yyerror (s) /* Called by yyparse on error */ char *s。{ printf (%s\n, s)。}yylex (){ int c。 /* skip white space */ while ((c = getchar ()) == 39。 39。 || c == 39。\t39。) 。 /* process numbers */ if (c == 39。.39。 || isdigit (c)) { ungetc (c, stdin)。 scanf (%lf, amp。yylval)。 return NUM。 } /* return endoffile */ if (c == EOF) return 0。 /* return single chars */ return c。 }例程3:(一個(gè)比較復(fù)雜的計(jì)算器)這個(gè)計(jì)算器有一些三角函數(shù)和指數(shù)運(yùn)算,通過(guò)這個(gè)例程我們會(huì)看到如何使用符號(hào)表和構(gòu)造簡(jiǎn)單語(yǔ)法樹(shù)(嵌入在語(yǔ)義動(dòng)作中)的流程./* Data type for links in the chain of symbols. */struct symrec{ char *name。 /* name of symbol */ int type。 /* type of symbol: either VAR or FNCT */ union { double var。 /* value of a VAR */ double (*fnctptr)()。 /* value of a FNCT */ } value。 struct symrec *next。 /* link field */}。typedef struct symrec symrec。/* The symbol table: a chain of `struct symrec39。. */extern symrec *sym_table。symrec *putsym ()。symrec *getsym ()。%{include /* For math functions, cos(), sin(), etc. */include /* Contains definition of `symrec39。 */%}%union {double val。 /* For returning numbers. */symrec *tptr。 /* For returning symboltable pointers */}%token val NUM /* Simple double precision number */%token tptr VAR FNCT /* Variable and Function */%type val exp%right 39。=39。%left 39。39。 39。+39。%left 39。*39。 39。/39。%left NEG /* Negationunary minus */%right 39。^39。 /* Exponentiation *//* Grammar follows */%%input: /* empty */ | input line。line: 39。\n39。 | exp 39。\n39。 { printf (\t%.10g\n, $1)。 } | error 39。\n39。 { yyerrok。 }。exp: NUM
點(diǎn)擊復(fù)制文檔內(nèi)容
高考資料相關(guān)推薦
文庫(kù)吧 www.dybbs8.com
備案圖片鄂ICP備17016276號(hào)-1