Professional Documents
Culture Documents
Compiler Compiler).
Automatically generate a parser for a context free grammar
(LALR parser)
Allows syntax direct translation by writing grammar productions and
semantic actions
LALR(1) is more powerful than LL(1).
Work with lex. YACC calls yylex to get the next token.
YACC and lex must agree on the values for each token.
Like lex, YACC pre-dated c++, need workaround for some
constructs when using c++ (will give an example).
Yacc environment
Yacc processes a yacc specification file and produces a y.tab.c file.
An integer function yyparse() is produced by Yacc.
Calls yylex() to get tokens.
Return non-zero when an error is found.
Return 0 if the program is accepted.
In yacc:
%token INTEGERCONST 2
%token PLUSNUM 4
All tokens used in the yacc grammar need to be specified. Some tokens
recognized by lex may not be in the yacc grammar token. See lexer.l Nonterminals do not need to be specified.
Resolving conflicts
Semantic actions
Semantic actions associate with productions can be
specified.
item : LPARENnumber exp RPARENnumber
{$$ = $2;}
| ICONSTnumber
{$$ = $1;}
;
$$ is the attribute associated with the left handside of the
production
$1 is the attribute associated with the first symbol in the
right handside, $2 for the second symbol,
An action can be in anywhere in the production, it is also
counted as a symbol.
Semantic actions
Semantic actions can be in anywhere in the
production, an action is also counted as a
symbol.
item : LPARENnumber {cout << debug;} exp RPARENnumber
{$$ = $3;}
| ICONSTnumber
{$$ = $1;}
;