Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
An example Parse
id id Id Id
Bottom up
“LR” and its cousin “LALR” (machine-generated choice
[Yacc/Bison])
Operator-precedence (outdated)
(9-5)+2 or 9-(5+2)
… …
expr.t = 95-2+
expr.t = 9 term.t = 5
term.t = 9
9 - 5 + 2
north
A sequence of such instructions is (2,1)
generated by the following grammar.
west begin
(-1,0) (0,0)
seq -> seq instr | begin north
instr -> east | north |
west | south
south east
east east
(-1,-1) (2,-1)
Changes in the position of the robot
on input
begin west south east east
east north north
seq.x = -1 instr.dx = 0
seq.y = 0 instr.dy = -1
instr.dx = -1
seq.x = 0
instr.dy = 0
seq.y = 0
expr.t = 9 term.t = 5
-
term.t = 9
{print(‘5’)} {print(‘2’)}
9 {print(‘9’)} 5 + 2
<program>
<program>
<program>
SimpleStmt ; <stmts>
<program>
SimpleStmt ; <stmts>
SimpleStmts ; <stmts>
<program>
SimpleStmt ; <stmts>
SimpleStmts ; <stmts>
<stmts>
l
<program> begin <stmts> end $
<stmts> SimpleStmt ; <stmts>
<stmts> begin <stmts> end ; <stmts>
<stmts> l
Bottom-up Parsing Example
Input: begin SimpleStmt; SimpleStmt; end $
<stmts>
SimpleStmts ; <stmts>
l
<program> begin <stmts> end $
<stmts> SimpleStmt ; <stmts>
<stmts> begin <stmts> end ; <stmts>
<stmts> l
Bottom-up Parsing Example
Input: begin SimpleStmt; SimpleStmt; end $
<stmts>
SimpleStmt ; <stmts>
SimpleStmts ; <stmts>
l
<program> begin <stmts> end $
<stmts> SimpleStmt ; <stmts>
<stmts> begin <stmts> end ; <stmts>
<stmts> l
Bottom-up Parsing Example
Input: begin SimpleStmt; SimpleStmt; end $
<program>
SimpleStmt ; <stmts>
SimpleStmts ; <stmts>
l
<program> begin <stmts> end $
<stmts> SimpleStmt ; <stmts>
<stmts> begin <stmts> end ; <stmts>
<stmts> l
Top Down Parsing
To introduce top-down parsing we consider the following
context-free grammar:
and show the construction of the parse tree for the input
string: 9 - 5 + 2.
expr
term rest
expr
term rest
term rest
- term rest
9
expr
term rest
- term rest
9
term rest
- term rest
9
expr
term rest
- term rest
9
term rest
- term rest
9
if l First(X1 ...Xm)
(First(X1 ...Xm)-l) U Follow(A)
else
First(X1 ...Xm)
rest()
{
if (lookahead == '+')
{match('+'); term(); print('+'); rest(); return; }
else if (lookahead == '-')
{match('-'); term(); print('-'); rest(); return; }
else { return; }
}
term()
{
if (isdigit(lookahead))
{ print(lookahead); match(lookahead); return ; }
else { ReportErrorAndHalt(); }
}
Prefix ( E )
F V Tail
+ E
V Tail
l
LR Parsing
Shift Reduce
Use a parse stack
Initially empty, it contains symbols already
parsed (T & NT)
Tokens are shifted onto stack until the top of
the stack contains the handle
The handle is then reduced by replacing it on
the stack with the non terminal that is its parent
in the derivation tree
Success when no input left and goal symbol on
the stack
Shift Reduce Parser
Useful Data Structures
Action table – determines whether to shift,
reduce, terminate with success, or an error has
occurred
Parse stack – contains parse states
They encode the shifted symbol and the
handles that are being matched
GoTo Table – defines successor states after a
token or LHS is matched and shifted.
Shift Reduce Parser
S – top parse stack state
T – Current input token
push(S0) // start state
Loop forever
case Action(S,T)
error => ReportSyntaxError()
accept => CleanUpAndFinish()
shift => Push(GoTo(S,T))
Scanner(T) // yylex()
reduce => Assume X -> Y1...Ym
Pop(m) // S' is new stack top
Push(GoTo(S',X))
Shift Reduce Parser
Example
Consider the following grammar G0:
4 exp 3 op 2 exp
( 5 exp ) * number
8 exp 7 op 6 exp
number - number
Left recursion: A A x | y
yxx: A
A x
A x
y
Right recursion: A x A | y
– xxy: A
x A
x A
y
113 Chapter 3: Context Free Grammars and February, 2010
Parsers
Repetition & Recursion, cont.
Sometimes we care which way recursion goes: operator
associativity
Sometimes we don’t: statement and expression sequences
Parsing always has to pick a way!
The tree may remove this information (see next slide)
- 42
34 3
statement statement
Correct one
if-stmt if-stmt
(Most-closely nested rule: easy to state, but hard to put into the
grammar itself.)
Bracketing keyword
Note that a “bracketing keyword” can remove the ambiguity:
if-stmt if ( exp ) stmt end
| if ( exp )stmt else stmt end
#define MAXCHILDREN 3
op assign
(<) repeat write
(fact)
id const
op op (x) (0)
(*) (-)
id id id const
(fact) (x) (x) (1)
http://www.lysator.liu.se/c/ANSI-C-grammar-y.html
factor
>
> number