flex、bison、mingwインストールパッケージを使用したlexとyaccのインストールと構成



Installation Configuration Lex





%{ int yywrap(void) %} %% %% int yywrap(void) { return 1 }

%{ void yyerror(const char *s) %} %% program: %% void yyerror(const char *s) { } int main() { yyparse() return 0 }

上記のコードの意味については、ここでは説明しません。コンソールを開き、今作成したファイル[lex.l] [yacc.y]が置かれているフォルダーに入ります。



1. [flexlex.l]と入力します

2.【bisonyacc.y】と入力します

3.現在のフォルダーにさらに2つのファイル[yacc.tab.c] [lex.yy.c]が表示されている場合は、次に、lex && yaccが正常にインストールおよび構成されたことを意味します、そしてこれらの2つのガジェットの魅力をお楽しみください。



2.ケース(ラフ)

ケースI:

1.新しいテキストファイルを作成し、名前を次のように変更します。インタプリタ.lex、次のコードを入力します-------字句解析プログラムのソースコード

%{ int wordCount = 0 int numcount = 0 %} chars [A-Za-z\_'.'] numbers ([0-9])+ delim [' ' ] whitespace {delim}+ words {chars}+ %% while {ECHO printf('%s ',yytext)} {words} { wordCount++ /* increase the word count by one*/ } {whitespace} { /* do nothing*/ } ([0-9])+ { numcount++ /* one may want to add some processing here*/ } %% void main() { printf('ok1 ') yylex() /* start the analysis*/ printf('ok2 ') printf(' No of words: %d number: %d ', wordCount, numcount) } int yywrap() { return 1 }

2.新しいテキストファイルを作成し、名前を変更しますsourcecode.c、次のコードを入力します--------これは入力ソースコードです

asd asdf 23 q a1 b2 !#@ while

3.メニューを開き、実行して、cmdと入力します。

入力:cdフォルダーパス

入力:flexinterpreter.lexそしてEnterキーを押してlex.yy.cファイルを生成します

次のように入力します。gcc-ointerpreterlex.yy.cそしてEnterキーを押してinterpreter.exeファイルを生成します。これで、単純な字句解析プログラム[インタプリタ。EXE】、以下は、サブ字句アナライザーを使用して[sourcecode.c]ファイルを分析することです。

入力:interpreter.exe output.txtを入力し、Enterキーを押してoutput.txtファイルを生成します

ケースII :(出力トークンテーブル)

1.sample2ディレクトリの下に新規作成しますインタプリタ.lex

%{ #include /* struct{ int linenum char name[255] int typenum } tokenItem //mine {Real} { printf('%15s %6d ', yytext, NUMSYM)} */ #define PROGRAM 1 #define IDNAME 58 #define VARSYM 2 #define SEMICOLONSYM 47 #define COLONSYM 41 #define INTEGER 3 #define BEGINSYM 8 #define ASSIGNSYM 39 #define NUMSYM 53 #define PLUSSYM 30 #define ENDSYM 26 #define DOTSYM 50 #define CHARSYM 6 int linenum = 1 int errornum = 0 %} blank [ ] Digit0 [1-9] Digit [0-9] Letter [a-zA-Z] Integer {Digit}+ Id {Letter}({Letter}|{Digit}|_)* delim [' ' ] newline [ ] whitespace {delim}+ Real ({Integer}[.]{Integer})([Ee][+-]?{Integer})? invalId {Digit}({Letter}|{Digit}|_)* invalLetter [^(Letter)(Digit)-*/+|_''!] %% [pP][rR][oO][gG][rR][aA][mM] { printf('%-5d %10s %6d ',linenum, yytext, PROGRAM)} [Bb][Ee][Gg][Ii][Nn] { printf('%-5d %10s %6d ',linenum, yytext, BEGINSYM)} [eE][nN][dD] { printf('%-5d %10s %6d ',linenum, yytext, ENDSYM)} [iI][nN][tT][eE][gG][eE][rR] { printf('%-5d %10s %6d ',linenum, yytext, INTEGER)} [vV][aA][rR] { printf('%-5d %10s %6d ',linenum, yytext, VARSYM)} [cC][hH][aA][rR] { printf('%-5d %10s %6d ',linenum, yytext, CHARSYM)} {newline} { linenum++} {whitespace} { /* do nothing*/ } '.' { printf('%-5d %10s %6d ',linenum, yytext, DOTSYM)} '' { printf('%-5d %10s %6d ',linenum, yytext, SEMICOLONSYM)} ':' { printf('%-5d %10s %6d ',linenum, yytext, COLONSYM)} ':=' { printf('%-5d %10s %6d ',linenum, yytext, ASSIGNSYM)} '+' { printf('%-5d %10s %6d ',linenum, yytext, PLUSSYM)} {Id} { printf('%-5d %10s %6d ',linenum, yytext, IDNAME)} {Integer}|{Real} { printf('%-5d %10s %6d ',linenum, yytext, NUMSYM)} {invalLetter} {errornum++ printf('[ERROR]:line %d: Unrecognized characters: %s ', linenum, yytext)} {invalId} {errornum++ printf('[ERROR]:line %d: Unrecognized identifier: %s ', linenum, yytext)} %% void main() { //print('[START]start the analysis...') //yylex() /* start the analysis*/ printf('token table information: ') printf('%-10s %10s %10s ','line number','name','species code') while(yylex()){ } printf(' Lescal analysis error message: ') printf('%d error(s) ', errornum) } int yywrap() { return 1 }

2.新規sourcecode.c

program example var a:integer begin x:=3+3.5+3.5e4 end.

3.新規soucecode2.c

program example var 3a:char α:integer

4.sourcecode.cおよびsourcecode2.cのトークンテーブルを出力します

(1)、cd [sample2のPATH]

(2)、flexinterpreter.lex

(3)、gcc-oインタプリタlex.yy.c

(4)、interpreter.exe output.txt

(5)、interpreter.exe output2.txt

実行中のスクリーンショットは次のとおりです。

その他の使用法のリファレンス:

https://blog.csdn.net/wp1603710463/article/details/50365495

https://www.cnblogs.com/wp5719/p/5528896.html

https://wenku.baidu.com/view/715f1ce8b90d6c85ed3ac62d.html