200 likes | 393 Views
Introduction to YACC. Panfeng Xue pxx101020@utdallas.edu. LEX and YACC. LEX Split the source file into tokens YACC Find the hierarchical structure of the program . LEX and YACC. Architecture. YACC Specifications. Similar structure to LEX Declaration Section Translation Rules
E N D
Introduction to YACC PanfengXue pxx101020@utdallas.edu
LEX and YACC • LEX • Split the source file into tokens • YACC • Find the hierarchical structure of the program
YACC Specifications • Similar structure to LEX • Declaration Section • Translation Rules • Supporting C/C++ code Declaration %% Translation rules %% Supporting C/C++ code
YACC Declaration • Declaration Section • C/C++ Code • YACC definition • %token • %start • Others • %{ • #include <stdio.h> • #include <string.h> • #define MAXSTRINGLENGTH 128 • intTotalParaNo=0; • …. • %} • %token TOK_constantsTOK_functions
YACC Specifications • Similar structure to LEX • Declaration Section • Translation Rules • Supporting C/C++ code Declaration %% Translation rules %% Supporting C/C++ code
YACC Rules • The rules section represents a grammar. The left-hand side of a production is followed by a colon. • Actions associated with a rule are entered in braces. statements: | statement statements { printf(" statements founded”); } ;
YACC Rules • Prog -> SS • SS -> S SS | ε • %% • programs: • statements • ; statements: /*empty*/ | statement statements ;
Actions • Actions: associated with a rule are entered in braces. • Similar with the LEX statements: | statement statements { printf(" statements founded”); } ;
Symbol Values • $1, $2….$n can be refer to the values associated with symbols • $$ refer to the value of the left • Every symbol have a value associated with it (including token and non-terminals) • Default action: • $$ = $1 statement: identifier '+' identifier { $$ = $1 + $3; } | identifier '-' identifier { $$ = $1 - $3; } ;
Actions • Inherited Attributes • { fn id PP } • How to transfer the value of fn and id to PP? • Using the stack information • $1 designates the first term on the right-hand side. • We can index backwards, using $0, $-1, and so on.
Symbol Types • Declaring Symbol Types %union { intdval; char *sval; }% …………………………… %token <dval> NUMBER %token <sval> IDENTIFIER %type <dval> statement
YACC Specifications • Similar structure to LEX • Declaration Section • Translation Rules • Supporting C/C++ code Declaration %% Translation rules %% Supporting C/C++ code
C/C++ Codes • Supporting C/C++ code • Token • In the Lex: return (TOKEN) • In the Parser: • %token TOKEN • // then TOKEN can be used in your yacc code
Feedback • Feed Information back to the LEX • YACC • Lex %{ inttop_layer = 1; }% …………………………… %% Program: statement { top_layer = 0;} ; %{ extern inttop_layer; }% ……………………………
Make • Make file • yacc –d FP.yacc # create y.tab.h, y.tab.c • lex FP.lex # create lex.yy.c • cc lex.yy.cy.tab.c –o FP # compile/link
How to Debug • Add the following into your YACC file • Add –-debug into your makefile %% extern intyy_flex_debug; int main(void) { yy_flex_debug = 1; yyparse(); }