150 likes | 358 Views
2-1. LEX & YACC. Overview. Syntax What its program looks like Context-free grammar, BNF Syntax-directed translation A grammar-oriented compiling technique Semantics Yacc. Syntax Definition. Grammar : describing the hierarchical structures of many programming language structures
E N D
Overview • Syntax What its program looks like • Context-free grammar, BNF • Syntax-directed translation • A grammar-oriented compiling technique • Semantics • Yacc
Syntax Definition • Grammar : describing the hierarchical structures of many programming language structures • stmt if (expr) stmtelsestmt • 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
An Example expression expression + term | term term term * factor | factor factor (expression) | identifier 실제 예를 들어서 설명함.
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 • 앞의 예를 이용하여 설명
Ambiguity, Associativity, Precedence • Ambiguity of a grammar • A grammar having more than one parse tree generating a given string of tokens • EE+E | E*E| id • Associativity of operators • between operators with same precedence • left/right associative • Precedence of operators • between different operators • 예로서 설명 +, *, **
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
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 ;
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
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}
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 ;
%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 <stdio.h> #include "lex.yy.c" main() { return yyparse(); } yyerror(char *s) { fprintf(stderr,"%s\n",s); }
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
사이트 PCYACC 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 • http://www.uman.com/lexyacc.shtml