Professional Documents
Culture Documents
YACC (Parser Generator) : Muhammad Usman Azhar BCSF09A025 Danial Hussain BCSF09A027
YACC (Parser Generator) : Muhammad Usman Azhar BCSF09A025 Danial Hussain BCSF09A027
declarations of tokens
type of values used on parser stack Rules section
%%
production : symbol1 symbol2 { action } | symbol3 symbol4 { action }
|
production: symbol1 symbol2 { action }
%%
An example
%%
statement : expression { printf ( = %g\n, $1); } expression : expression + expression { $$ = $1 + $3; } | expression - expression { $$ = $1 - $3; }
| NUMBER { $$ = $1; }
%%
statement expression expression expression
expression
number
expression
expression number expression number + 2
%right =' %left '-' '+' %left '*' '/' %right '^'
Defining Values
expr : expr '+' term | term ; term : term '*' factor | factor ; factor : '(' expr ')' | ID | NUM ; { $$ = $1 + $3; } { $$ = $1; } { $$ = $1 * $3; } { $$ = $1; } { $$ = $2; }
Defining Values
$1
expr : expr '+' term | term ; term : term '*' factor | factor ; factor : '(' expr ')' | ID | NUM ; { $$ = $1 + $3; } { $$ = $1; } { $$ = $1 * $3; } { $$ = $1; } { $$ = $2; }
Defining Values
expr : expr '+' term | term ; term : term '*' factor | factor ; factor : '(' expr ')' | ID | NUM ; { $$ = $1 + $3; } { $$ = $1; } { $$ = $1 * $3; } { $$ = $1; } { $$ = $2; }
$2
Defining Values
expr : expr '+' term | term ; term : term '*' factor | factor ; factor : '(' expr ')' | ID | NUM ; { $$ = $1 + $3; } { $$ = $1; } { $$ = $1 * $3; } { $$ = $1; } { $$ = $2; }
$3
Ambiguous Grammars
A Grammar that produces more than one Parse Tree for the same sentences or the Production rules in a grammar is said to be ambiguous. e.g. consider a simple mathematical expression E->E*E This can have two Parse tree according to associativity of the operator '* E E / \ / \ * E E * / \ / \ E E E E Yacc also tell us that the given language is ambiguous or not.