lexyacctutorial内容摘要:

User Subroutines Section • You can use your Lex routines in the same ways you use routines in other programming languages. %{ void foo()。 %} letter [azAZ] %% {letter}+ foo()。 %% … void foo() { … } PLLab, NTHU,Cs2403 Programming Languages 28 User Subroutines Section (cont‟d) • The section where main() is placed %{ int counter = 0。 %} letter [azAZ] %% {letter}+ {printf(“a word\n”)。 counter++。 } %% main() { yylex()。 printf(“There are total %d words\n”, counter)。 } PLLab, NTHU,Cs2403 Programming Languages 29 Usage • To run Lex on a source file, type lex • It produces a file named which is a C program for the lexical analyzer. • To pile , type cc –ll • To run the lexical analyzer program, type ./ inputfile PLLab, NTHU,Cs2403 Programming Languages 30 Versions of Lex • ATamp。 T lex • GNU flex • a Win32 version of flex : or Cygwin : • Lex on different machines is not created equal. PLLab, NTHU,Cs2403 Programming Languages 31 Yacc Yet Another CompilerCompiler PLLab, NTHU,Cs2403 Programming Languages 32 Introduction • What is YACC ? – Tool which will produce a parser for a given grammar. – YACC (Yet Another Compiler Compiler) is a program designed to pile a LALR(1) grammar and to produce the source code of the syntactic analyzer of the language produced by this grammar. PLLab, NTHU,Cs2403 Programming Languages 33 How YACC Works File containing desired grammar in yacc format yacc program C source program created by yacc C piler Executable program that will parse grammar given in yacc cc or gcc PLLab, NTHU,Cs2403 Programming Languages 34 yacc How YACC Works (1) Parser generation time YACC source (*.y) C piler/linker (2) Compile time (3) Run time Token stream Abstract Syntax Tree PLLab, NTHU,Cs2403 Programming Languages 35 An YACC File Example %{include %}%token NAME NUMBER%%statement: NAME 39。 =39。 expression | expression { printf(= %d\n, $1)。 }。 expression: expression 39。 +39。 NUMBER { $$ = $1 + $3。 } | expression 39。 39。 NUMBER { $$ = $1 $3。 } | NUMBER { $$ = $1。 }。 %%int yyerror(char *s){ fprintf(stderr, %s\n, s)。 return 0。 }int main(void){ yyparse()。 return 0。 }PLLab, NTHU,Cs2403 Programming Languages 36 Works with Lex Y A C Cy y p a r s e ( )I n p u t p r o g r a m s12 + 26L E Xy y le x ( )How to work ? PLLab, NTHU,Cs2403 Programming Languages 37 Works with Lex Y A C Cy y p a r s e ( )I n p u t p r o g r a m s12 + 26L E Xy y le x ( )call yylex() [09]+ next token is NUM NUM „+‟ NUM PLLab, NTHU,Cs2403 Programming Languages 38 YACC File Format %{ C declarations %} yacc declarations %% Grammar rules %% Additional C code – Comments enclosed in /* ... */ may appear in any of the sections. PLLab, NTHU,Cs2403 Programming Languages 39 Definitions Section %{ include include %} %token ID NUM %start expr It is a terminal 由 expr 開始 parse PLLab, NTHU,Cs2403 Programming Languages 40 Start Symbol • The first nonterminal specified in the grammar specification section. • To overwrite it with %start declaraction. %start nonterminal PLLab, NTHU,Cs2403 Programming Languages 41 Rules Section • This section defines grammar • Example expr : expr 39。 +39。 term | term。 term : term 39。 *39。 factor | factor。 factor : 39。 (39。 expr 39。 )39。 | ID | NUM。 PLLab, NTHU,Cs2403 Programming Languages 42 Rules Section • Normally written like this • Example: expr : expr 39。 +39。 term | term。 term。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。