Professional Documents
Culture Documents
PARSING
1
1. Make an assumption:
Example: If all shift/reduce conflicts could be resolved by
shifting rather than reducing, no need to rewrite the grammar
2. Rewrite the grammar
3. Look ahead at additional input characters
Example: Look ahead at k input symbols. (Grammar would be
called LR(k))
GoTo
ACTION TABLE: Determines whether a shift or reduce is to be
Action
invoked
GOTO TABLE: Indicates which stack symbol is to be pushed
Table
on the stack after a reduction
Table
Fadzlin Ahmadon – UiTM JASIN
LR Parsing with Tables
14
Action
•Input pointer is retained
G 5
1. Expr → Expr + Term
2. Expr → Term
3. Term → Term * Factor
4. Term → Factor
5. Factor →( Expr ) ▽
6. Factor → var Initial Stack
▽(Expr5 )*var↵
shift )
▽(Expr5) *var↵ reduce 5 push Factor4
▽Factor4 *var↵ reduce 4 push Term2
▽Term2 *var↵ shift *
▽Term2* var↵ shift var
▽Term2*var ↵ reduce 6 push Factor3
▽Term2*Factor3 ↵ reduce 3 push Term2
▽Term2 ↵ reduce 2 push Expr1
▽Expr1 ↵ Accept
SableCC
25
SableCC
Translation.java Compiler.java
javac javac
Translation.class Compiler.class
x? // An optional x (0 or 1 occurrences of x)
x* // 0 or more occurrences of x
x+ // 1 or more occurrences of x
Grammar:
1. Expr → Expr + Term
2. Expr → Expr - Term
3. Expr → Term
4. Term → Term ∗ Factor
5. Term → Term / Factor
6. Term → Factor
7. Factor → ( Expr )
8. Factor → number
Productions Productions
expr =
{term} term |
Alternative {plus} expr plus term |
definition
{minus} expr minus term
using ‘|’ and
have name ;
term =
{factor} factor |
{mult} term mult factor |
{div} term div factor
;
factor =
{number} number |
{paren} l_par expr r_par
;
Fadzlin Ahmadon – UiTM JASIN