日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

编译与解释实践(1)-flex and bison 配置安装

發(fā)布時(shí)間:2025/3/12 编程问答 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编译与解释实践(1)-flex and bison 配置安装 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
sudo dnf instal flex bison

下面先開始測(cè)試flex
編輯test.l

%option noyywrap //增加的語句 %{ int chars = 0; int words = 0; int lines = 0; %} %% [a-zA-Z]+ {words++; chars+= strlen(yytext);} \n {chars++; lines++;} . {chars++;} %% int main(int argc, char** argv){ yylex(); printf("%d,%d,%d\n", lines, words, chars); }

形成詞法分析

$ ./a.out hello world good this book 4,5,28 (base) [myhaspl@localhost flexbison]$ flex test.l (base) [myhaspl@localhost flexbison]$ gcc lex.yy.c (base) [myhaspl@localhost flexbison]$ ./a.outhello world good this book 4,5,28

$vim testcalc.l

%option noyywrap %{enum yytokentype{NUMBER=258,ADD=259,SUB=260,MUL=261,DIV=262,ABS=263,EOL=264}; int yylval; %} %% "+" {return ADD;} "-" {return SUB;} "*" {return MUL;} "/" {return DIV;} "|" {return ABS;} [0-9]+ {yylval = atoi(yytext); return NUMBER;} \n {return EOL;} [ \t] {return EOL;} . {printf("Mystery character %c\n", *yytext);} %% int main(int argc,char **argv) {int tok;while(tok=yylex()){printf("%d",tok);if(tok==NUMBER) printf(" = %d\n",yylval);else printf("\n");} } $cc lex.yy.c $ ./a.out a /31 + |19 Mystery character a 264 262 258 = 31 264 259 264 263 258 = 19 264

編輯testcalc.y和testcalc.l文件

(base) [myhaspl@localhost flexbison]$ cat testcalc.l %{ #include"testcalc.tab.h" %} %option noyywrap %% "+" {return ADD;} "-" {return SUB;} "*" {return MUL;} "/" {return DIV;} "|" {return ABS;} [0-9]+ {yylval = atoi(yytext); return NUMBER;} \n {return EOL;} [ \t] {return EOL;} . {printf("Mystery character %c\n", *yytext);} %% (base) [myhaspl@localhost flexbison]$ cat testcalc.y %{ #include<stdio.h> %} %token NUMBER %token ADD SUB MUL DIV ABS %token EOL %%calclist:| calclist exp EOL {printf("=%d\n", $2);}; exp: factor | exp ADD factor {$$ = $1 + $3;}| exp SUB factor {$$ = $1 - $3;}; factor: term | factor MUL factor {$$ = $1 * $3;}| factor DIV term {$$ = $1 / $3;}; term:NUMBER | ABS term {$$ = $2 >= 0? $2 : -$2;}; %% main(int argc, int **argv){ yyparse(); } yyerror(char *s) { fprintf(stderr, "error:%s\n", s); }

生成可執(zhí)行文件

(base) [myhaspl@localhost flexbison]$ bison -d testcalc.y testcalc.y: 警告: 2 項(xiàng)偏移/歸約沖突 [-Wconflicts-sr] (base) [myhaspl@localhost flexbison]$ flex testcalc.l (base) [myhaspl@localhost flexbison]$ cc -o testcalc testcalc.tab.c lex.yy.c (base) [myhaspl@localhost flexbison]$ ./testcalc 2*3+4 =10 5+6*9 =59 與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的编译与解释实践(1)-flex and bison 配置安装的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。