Presentation is loading. Please wait.

Presentation is loading. Please wait.

2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.

Similar presentations


Presentation on theme: "2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling."— Presentation transcript:

1 2-1. LEX & YACC

2 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling technique –Semantics  Yacc

3 3 Syntax Definition  Grammar : describing the hierarchical structures of many programming language structures  stmt  if (expr) stmt else stmt  A context-free grammar –A set of tokens, known as terminal symbols –A set of nonterminals –A set of productions left-side -> right side (left-side: nonterminal, right side: a sequence of tokens and/or nonterminals –A designation of one of the nonterminals as the start symbol

4 4 An Example expression  expression + term | term term  term * factor | factor factor  (expression) | identifier  실제 예를 들어서 설명함.

5 5 Parse Trees  정의 –The root is labeled by the start symbol –Each leaf is labeled by a token or by ∈ –Each interior node is labeled by a nonterminal When A is a node then X1, …, Xn are children if A -> X1 … Xn is a production  앞의 예를 이용하여 설명

6 6 Ambiguity, Associativity, Precedence  Ambiguity of a grammar –A grammar having more than one parse tree generating a given string of tokens –E  E+E | E*E| id  Associativity of operators – between operators with same precedence – left/right associative  Precedence of operators – between different operators  예로서 설명  +, *, **

7 7 Syntax-directed translation ProductionSemantic Rule expr -> expr 1 + term expr -> expr 1 – term expr -> term term -> 0 term -> 1 … term -> 9 expr.t := expr 1.t || term.t || ‘ + ’ expr.t := expr 1.t || term.t || ‘ - ’ expr.t := term.t term.t := ‘ 0 ’ term.t := ‘ 1 ’ … term.t := ‘ 9 ’ Figure 2.5 syntax-directed definition for infix to postfix translation

8 8 Figure 2.6 Attribute values at nodes In parse tree Figure 2.8 Annotated parse tree for begin west south

9 9 Lex is a lexical analyzer generator and Yacc is a parser generator. Lex programs recognize regular expressions and yacc generates parsers that accept a large class of context-free grammars. Below is a figure which shows how lex and yacc can be combined to perform the "lexical analysis" phase of a compiler

10 10 Lex - specify a set of lexical rules to lex in a source file. The general format of the source file is: {definitions} % {rules} % {programmer subroutines} digit [0-9] digits {digit}+ whitespace [ \t\n] % "[" { printf("OPEN_BRAC\n");} "]" { printf("CLOSE_BRAC\n");} "+" { printf("ADDOP\n");} "*" { printf("MULTOP\n");} {digits} { printf("NUMBER = %s\n", yytext);} whitespace ;

11 11 lex foo.lex cc lex.yy.c -ll $ a.out /* a.out expects it's input from standard input */ input: [ 1 + 2 * 3 ] output: OPEN_BRAC NUMBER = 1 ADDOP NUMBER = 2 MULTOP NUMBER = 3 CLOSE_BRAC

12 12 Yacc -The parser gets it's input (a sequence of tokens) from the lexical analyzer (created using lex). -The format of grammar rules {declarations} % {rules} % {programs}

13 13 digit [0-9] digits {digit}+ whitespace [ \t\n] % "[" { return (OPEN_BRAC);} "]" { return (CLOSE_BRAC);} "+" { return (ADDOP); } "*" { return (MULTOP); } {digits} { yylval = atoi(yytext); return (NUMBER); } whitespace ;

14 14 %start mystartsymbol %token ADDOP MULTOP NUMBER OPEN_BRAC CLOSE_BRAC %left ADDOP %left MULTOP % mystartsymbol : expr { printf("the value of the expression is %d\n", $1);} expr : OPEN_BRAC expr CLOSE_BRAC { $$ = $2; } | expr ADDOP expr { $$ = $1 + $3 ;} | expr MULTOP expr { $$ = $1 * $3 ;} | NUMBER { $$ = $1; } ; % /* start of programs */ #include #include "lex.yy.c" main() { return yyparse(); } yyerror(char *s) { fprintf(stderr,"%s\n",s); }

15 15 Shell Program ::: echo lex expr.lex lex expr.lex echo yacc expr.yacc yacc expr.yacc echo cc y.tab.c -ll cc y.tab.c -ll

16 16 사이트  http://www.uman.com/lexyacc.shtml http://www.uman.com/lexyacc.shtml PCYACCPCYACC 9.0 is a complete language development environment that generates C, C#, C++, Java, Delphi, and VBS source code from input Language Description Grammars for building Assemblers, Compilers, Interpreters, Browsers, Page Description Languages, Language Translators, Syntax Directed Editors, Language Validators, Natural Language Processors, Expert System Shells, and Query Languages. The PCYACC Tool-Kit includes PCLEX, Visual Debugging Tools, Object- Oriented Class Library's, and Pre-Written "Drop-In" Language engines for virtually every computer language in the world


Download ppt "2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling."

Similar presentations


Ads by Google