15
YACC

YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

Embed Size (px)

Citation preview

Page 1: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

YACC

Page 2: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

Introduction• What is YACC ?

a tool for automatically generating a parser given a grammar written in a yacc specification (.y file)

• YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar and to produce the source code of the syntactic analyzer of a language produced by this grammar.

• A grammar specifies a set of production rules, which define a language.

• A production rule specifies a sequence of symbols, sentences, which are legal in the language.

Page 3: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

History• Yacc original written by• Stephen C. Johnson, 1975.• • Variants:• – lex, yacc (AT&T)• – bison: a yacc replacement (GNU)• – flex: fast lexical analyzer (GNU)• – BSD yacc• – PCLEX, PCYACC (Abraxas• Software)

Page 4: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

How YACC Works

YACC source (translate.y)yacc y.tab.c

(1) Parse

cc/gccy.tab.ca.out

(2) Compile

a.outToken stream 0utput

(3) Run

Page 5: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

Skeleton of a yacc specification (.y file)translate.y

%{

< C global variables,prototypes, comments >

%}

[DEFINITION SECTION]

%%

[PRODUCTION RULES SECTION]

%%

< C auxiliary subroutines>

y.tab.c is generated after running

This part will be embedded into y.yab.c

contains token declarations. Tokens are recognized in lexer.

define how to “understand” the input language, and what actions to take for each “sentence”.

Any user code. For example, a

main function to call the parser function yyparse()

Page 6: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

YACC File Format

• Definition section– declarations of tokens

– type of values used on parser stack

• Rules section– list of grammar rules with semantic routines

• User code

• Comments in /* ... */ may appear in any of the sections

Page 7: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

Declaration Section

• Two optional sections– Ordinary C declarations delimited by %{ and %}– Declarations of grammar tokens• % token DIGIT Declares DIGIT to be a tokenTokens specified in this section can be used as terminal in

the second and third sections.

Page 8: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

Translation Rules Section• Each rule consists of a grammar production and

associated semantic action.• <left side> → <alt1> | <alt2> | …. | <altn> would be written in YACC as <left side> : <alt1> {semantic action1}

| <alt2> {semantic action2} |… | <altn> {semantic action n}

;

Page 9: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

Translation Rules Section• Rule section is a grammar• Exampleexpr : expr '+' term | term;term : term '*' factor | factor;factor : '(' expr ')' | ID | NUM;• Semantic action is a sequence of C statements.• Semantic action is performed when we reduce by the

associated production• Normally the semantic action computes a value for $$ in

terms of $i s.

Page 10: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

The Position of Rulesexpr : expr '+' term { $$ = $1 + $3; }

| term { $$ = $1; };

term : term '*' factor { $$ = $1 * $3; }| factor { $$ = $1; };

factor : '(' expr ')‘ { $$ = $2; }| ID| NUM;

Page 11: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

Supporting C Routines

• A lexical analyzer by the name yylex() should be provided.– yylex() produces a pair consisting of a token and its

attribute value.– If a token such as DIGIT is returened it must be declared in

the first section.– The attribute value associate with a token is

communicated to the parser through a Yacc defined variable yylval.

• Error recovery routines may be added as necessary.

Page 12: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

Sample yacc program%{#include<ctype.h>%}%token DIGITline:expr ‘\n’ {printf(“%D\n”,$1);}

;Expr : expr ‘+’ term {$$=$1+$3}

| term;

Term : term ‘*’ factor {$$=$1*$3}| factor;

Factor : ‘(‘ expr {$$=$2}| DIGIT;

%%

Page 13: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

Sample yacc program%{#include< ctype.h>%}%token DIGIT%%

line :expr ‘\n’ {printf(“%d\n”,$1);};

expr : expr ‘+’ term {$$=$1+$3}

| term;

term : term ‘*’ factor {$$=$1*$3}

| factor;

factor : ‘(‘ expr {$$=$2}| DIGIT;

%%

yylex(){

int c;

c=getchar();if isdigit(c ){ yylval=c-’0’;return DIGIT;}return c;}

Page 14: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

Yacc with ambiguous grammarPrecedence / Association

%token NUMBER%left '+' '-'%left '*' '/'%right UMINUS%%Lines : expr ‘\n’ {printf(“%d\n”, $1);}expr : expr ‘+’ expr { $$ = $1 + $3; }

| expr ‘-’ expr { $$ = $1 - $3; }| expr ‘*’ expr { $$ = $1 * $3; }| expr ‘/’ expr { if($3==0) yyerror(“divide 0”);

else | ‘-’ expr %prec UMINUS {$$ = -$2; }|NUMBER;

%%

Page 15: YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another

Conflictsshift/reduce conflict

– occurs when a grammar is written in such a way that a decision between shifting and reducing can not be made.

ex: IF-ELSE ambigious.To resolve this conflict, yacc will choose to shift

reduce/reduce Conflicts:start : expr | stmt

;expr : CONSTANT

;stmt : CONSTANT

;• Yacc resolves the conflict by reducing using the rule that occursearlier in the grammar.