Implementation Details Group Yacc Bison Compilers Don Bahls

Implementation Details Group Yacc / Bison Compilers Don Bahls CS 331 Spring 2002

Terminology • Yacc: Yet another compiler, uses C (Unix version) • Bison: Yacc Compatible compiler, uses C (GNU version) • Yacc++/Bison++: C++ enhanced versions of Yacc and Bison • Lex: Lexical Analyzer (Unix version) • LALR(1) Parser: 1 token Look Ahead Left to Right parser

What is Yacc? • Yacc is a compiler • More generally, Yacc builds a LALR(1) parser for user defined languages • Turns a specification in BNF and builds a subroutine to handle input

How Yacc works • User defines the structure of input language (BNF) • Define C functions to call when certain rules (or reductions) are recognized • Lex or user defined subroutines find the tokens in the input

General Yacc Syntax C and parser declarations • List of tokens • May contain precedence and associativity of operators Grammar rules and actions • Define context-free grammar (rules) for the language • Rules can also have an action associated with them C subroutines • C code there must be a main(), yyparse(), and yyerror() C and parser declarations %% Grammar rules and actions %% C subroutines

Example C Declarations %{ and }% allow global C code to be inserted anywhere in the declarations and rules sections of a Yacc file %{ /* C code can be placed in the declarations section or within the rules and actions section all declarations are global */ #include <math. h> int x = 0; int func(int x) { }% return 2 * x; }

Example Tokens, Rules and Actions Token and associativity examples %token NUMBER %token IDENTIFIER %left '-' '+' %right '^‘ Some simple rules with an action A : B C ; { x = $1 ; y = $2 ; } /* x takes on the value of B, y the value of C */ expr : ‘(‘ expr ‘)’ { $$ = $2 ; } /* return second element in the rule on the stack. By default the value of a rule is the first element ($1) */

Dealing with Ambiguities An ambiguous rule expr : expr ‘-’ expr Can be structured with left association or right association ( expr - expr ) - expr - ( expr - expr ) Best solution is to rewrite ambiguous rules

How Yacc Deals with Ambiguities in Rules Conflicts are resolved by following two rules: 1) When there is a shift/reduce conflict, Yacc will shift by default. 2) When the conflict is a reduce/reduce conflict, Yacc will default to the rule defined earlier The parser will report the number of conflicts resolved by each of these methods, by calling the yyerror() function

Error Handling at the Parser Level When an error is encountered during parsing • Generally not acceptable to stop all processing • May need to delete or alter items in the symbol table • May need to clear items from the parse tree Methods Yacc provides to accomplish these goals • Yacc reserves the token error for error handling • The token error can be used in rules where errors are expected • The parser remains in an error state until three tokens have been successfully shifted and read (to avoid cascading error messages).

Reverse Polish Notation Calculator Example Rules %{ #define YYSTYPE double #include <math. h> %} %token NUM %% /* Grammar rules and actions follow */ input: /* empty */ | input line ; line: exp: %% 'n' | exp 'n' { printf ("t%. 10 gn", $1); } ; NUM { $$ | exp '+' { $$ | exp '-' { $$ | exp '*' { $$ | exp '/' { $$ /* Exponentiation */ | exp '^' { $$ /* Unary minus */ | exp 'n' { $$ = = = $1; $1 + $1 $1 * $1 / $2; $2; } } } = pow ($1, $2); } = -$1; } ;

Reverse Polish Notation Calculator Example Lexical Analysis %% #include <ctype. h> yylex () { int c; /* skip white space */ while ((c = getchar ()) == ' ' || c == 't') ; /* process numbers */ if (c == '. ' || isdigit (c)) { ungetc (c, stdin); scanf ("%lf", &yylval); return NUM; } /* return end-of-file */ if (c == EOF) return 0; /* return single chars */ return c; }

Reverse Polish Notation Calculator Example main() main () { yyparse (); } yyerror (s) /* Called by yyparse on error */ char *s; { printf ("%sn", s); } Complete RPN calculator code available from http: //home. uninet. ee/~ragnar/yacc/rpn/rpcalc. y

Resources Best source: Yacc: Yet Another Compiler-Compiler; Stephen C. Johnson http: //www. combo. org/lex_yacc_page/yacc. html Other sources: Compiler Construction using Flex and Bison; Anthony Aaby http: //cs. wwc. edu/~aabyan/464/Book/ RPN Calculator Code: http: //home. uninet. ee/~ragnar/yacc/rpn/rpcalc. y A Compact Guide to Lex & Yacc; Thomas Niemann http: //epaperpress. com/lexandyacc/ GNU Bison Manual http: //www. gnu. org/manual/bison/html_mono/bison. html ANSI C Yacc grammar http: //www. lysator. liu. se/c/ANSI-C-grammar-y. html The C Programming Language- Second Edition; Kernigan & Ritchie ANSI C Grammar, pp 234 -239
- Slides: 14