YACC Primer

11
YACC Primer CS 671 January 29, 2008

description

YACC Primer. CS 671 January 29, 2008. Yacc. Y et A nother C ompiler C ompiler Automatically constructs an LALR(1) parsing table from a set of grammar rules Yacc/Bison specification:. file.y. parser declarations %% grammar rules %% auxiliary code. bison –vd file.y -or- - PowerPoint PPT Presentation

Transcript of YACC Primer

Page 1: YACC Primer

YACC Primer

CS 671January 29, 2008

Page 2: YACC Primer

2 CS 671 – Spring 2008

Yacc

•Yet Another Compiler Compiler

•Automatically constructs an LALR(1) parsing table from a set of grammar rules

•Yacc/Bison specification:

parser declarations%%grammar rules%%auxiliary code

bison –vd file.y

-or-

yacc –vd file.y

y.tab.cy.tab.hy.output

file.y

Page 3: YACC Primer

3 CS 671 – Spring 2008

Yacc/Bison

Input: A CFG and a translation scheme – file.y

Output: A parser file.tab.c (bison) or y.tab.c (yacc)• An output file file.output containing the parsing

tables (when invoked with –v option)• A file file.tab.h containing declarations (if invoked

with –d option)• The parser called yyparse()• Parser expects to use a function called yylex() to get

tokens

Page 4: YACC Primer

4 CS 671 – Spring 2008

Yacc Declaration Section

% {

c code

% }

% token PLUS MULTIPLY DIVIDE

% left PLUS MINUS

% left MULT DIV

% nonassoc EQ NEQ LT GT

% prec UMINUS

Terminal symbolsAssigned enumPlaced in f.tab.h

Page 5: YACC Primer

5 CS 671 – Spring 2008

Yacc Grammar Rules Section

exp : exp PLUS exp { semantic action }

Non-terminal

Terminal

C code. Executed when parser reduces this rule

Page 6: YACC Primer

6 CS 671 – Spring 2008

Example Grammar

P L

S id := id

S while id do S

S begin L end

S if id then S

S if id then S else S

L S

L L ; S

Page 7: YACC Primer

7 CS 671 – Spring 2008

Corresponding Yacc Specification

%{int yylex(void);%}% token ID WHILE BEGIN END DO …% start prog%%

[please fill in your solution]

P LS id := idS while id do SS begin L endS if id then SS if id then S else SL SL L ; S

Page 8: YACC Primer

8 CS 671 – Spring 2008

Conflicts

Yacc reports shift-reduce and reduce-reduce conflicts

Default behavior:

shift/reduce: choose shift

reduce/reduce: uses earlier rule

State 17: shift/reduce conflict

(shift ELSE, reduce 4)

stm: IF ID THEN stm .

stm: IF ID THEN stm . ELSE stm

ELSE shift 19

. reduce by rule 4

Resolve all conflicts!! (Use precedence rules)

Page 9: YACC Primer

9 CS 671 – Spring 2008

Must Manage Conflicts

% left PLUS;

% left TIMES; // TIMES > PLUS

E : E PLUS E | E TIMES E | ...

E→ E . + E …E→ E E . +

E→ E + E . E→ E . E …

Rule: in conflict, choose reduce if production symbol higher precedence than shifted symbol; choose shift if vice-versa

Page 10: YACC Primer

10 CS 671 – Spring 2008

Precedence Directives

E E * E . +

E E . + E (any)

E

E E

E E+

*

E

+E

E E*

E

shift reduce

%nonassoc EQ NEQ

%left PLUS MINUS

%left TIMES DIV

%right EXP

%left prefers reducing

%right prefers shifting

%nonassoc error

Page 11: YACC Primer

11 CS 671 – Spring 2008

The %prec Directive

%{ declarations of yylex and yyerror %}% token INT PLUS MINUS TIMES UMINUS% start exp

% left PLUS MINUS% left TIMES% left UMINUS%%

exp : INT | exp PLUS exp | exp MINUS exp | exp TIMES exp | MINUS exp %prec UMINUS