








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









Definitions Section
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM
%start expr
start from expr
Start Symbol
specification section.
%start non-terminal
Rules Section
expr : expr '+' term | term; term : term '*' factor | factor; factor : '(' expr ')' | ID | NUM;
Rules Section
The Position of Rules
expr : expr '+' term { $$ = $1 + $3; } | term { $$ = $1; } ; term : term '*' factor { $$ = $1 * $3; } | factor { $$ = $1; } ; factor : '(' expr ')' { $$ = $2; } | ID | NUM ;
call yylex() [0-9]+
next token is NUM
NUM ‘+’ NUM
Passing value of token
Passing value of token
%union { double dval; int vblno; char* strval; }
Passing value of token
%union { double dval; int vblno; char strval; }*
yacc -d y.tab.h … extern YYSTYPE yylval;
[0-9]+ { yylval.vblno = atoi(yytext); return NUM;} [A-z]+ { yylval.strval = strdup(yytext); return STRING;} (^) Lex file include “y.tab.h”
Yacc Example
Grammar
expression ::= expression '+' term | expression '-' term | term
term ::= term '*' factor | term '/' factor | factor
factor ::= '(' expression ')' | '-' factor | NUMBER | NAME
#define NSYMS 20 /* maximum number of symbols */
struct symtab { char *name; double value; } symtab[NSYMS];
struct symtab *symlook();
parser.h
(^0) name value 1 name value 2 name value 3 name value (^4) name value (^5) name value 6 name value 7 name value 8 name value 9 name value (^10) name value
Symbol Table
%{ #include "y.tab.h" #include "parser.h" #include <math.h> %} %% ([0-9]+|([0-9]*.[0-9]+)([eE][-+]?[0-9]+)?) { yylval.dval = atof(yytext); return NUMBER; }
[ \t] ; /* ignore white space */
Scanner
scanner.l
[A-Za-z][A-Za-z0-9]* { /* return symbol pointer */ yylval.symp = symlook(yytext); return NAME; }
"$" { return 0; /* end of input */ }
\n |”=“|”+”|”-”|”*”|”/” return yytext[0]; %%
Scanner (cont’d)
scanner.l
Precedence / Association
Precedence / Association
%right ‘=‘
%left '<' '>' NE LE GE
%left '+' '-‘
%left '*' '/'
highest precedence
Precedence / Association
expr : expr ‘+’ expr { $$ = $1 + $3; } | expr ‘-’ expr { $$ = $1 - $3; } | expr ‘*’ expr { $$ = $1 * $3; } | expr ‘/’ expr { if($3==0) yyerror(“divide 0”); else $$ = $1 / $3; } | ‘-’ expr %prec UMINUS {$$ = -$2; }
%left '+' '-' %left '*' '/' %noassoc UMINUS
IF-ELSE Ambiguity
Consider the following rule:
How about the following statement?
IF expr IF expr stmt ELSE stmt
stmt : IF expr stmt | IF expr stmt ELSE stmt ……
Error Messages
void yyerror(char *s) { fprintf(stderr, "line %d: %s\n:", yylineno, s); }
Debug Your Parser
trace parsing status.
Shift and Reducing: Example
stmt: stmt ‘;’ stmt
| NAME ‘=‘ exp
exp: exp ‘+’ exp
| exp ‘-’ exp | NAME | NUMBER
input: a = 7; b = 3 + a + 2
stack:
Recursive Grammar
list: item | list ',' item ;
list: item | item ',' list ;
YACC Declaration Summary
`%start' Specify the grammar's start symbol
`%union' Declare the collection of data types that semantic values may have
`%token' Declare a terminal symbol (token type name) with no precedence or associativity specified
`%type' Declare the type of semantic values for a nonterminal symbol
YACC Declaration Summary
`%right' Declare a terminal symbol (token type name) that is right-associative
`%left' Declare a terminal symbol (token type name) that is left-associative
`%nonassoc' Declare a terminal symbol (token type name) that is nonassociative (using it in a way that would be associative is a syntax error, ex: x op. y op. z is syntax error)