Download presentation
Presentation is loading. Please wait.
1
LL(1) Parsing LL(1) is a Top Down parsing scheme. Applies productions from goal symbol to derive grammar sentence. First L – Scanner moves from left to right. Second L – Parser expands leftmost non-terminal first. (1) – Scanner supplies parser with one terminal/token to figure out which production to apply next.
2
Top-Down Parse E 1 / | \ E 2 + T 5 | / | \ T 3 T 6 * F 8 | | | F 4 F 7 id | | id id
3
Parse Stack for $ id + id * id $ ETFidTF +++++**** ETTTTTTFFFFF $$$$$$$$$$$$$$
4
Def: Unambiguous Grammar Context Free. Every sentence in grammar has at most one leftmost derivation.
5
LR(1) Grammar LR(1) is a bottom-up parsing scheme. Reduces from grammar sentence to goal symbol using reductions. L – Scanner moves from left to right. R – Rightmost derivation in reverse. (1) – Scanner supplies parser with one terminal/token to figure out which reduction to apply next.
6
Bottom-Up Parse E 8 -------------------- | | T 7 E 3 | | | | --------------- T 2 | T 5 | | | | | | | F 1 | F 4 | F 6 | | | | | id + id * id
7
Rightmost Derivation in Reverse id + id * id F 1 + id * id T 2 + id * id E 3 + id * id E + F 4 * id E + T 5 * id E + T * F 6 E + T 7 E 8 Notice that proceeding backwards, the rightmost non-terminal symbol (highest number) is expanded.
8
Grammar Design Design an unambiguous context-free grammar G 1 where +, -, *, and / are left associative ^ (exponentiation) is right associative Precedence from lowest to highest is: 1)Binary +, - 2)Binary *, / 3)Binary ^ 4)Unary -
9
Grammar G 1 E E + T | E – T | T T T * F | T / F | F F P ^ F | P P -P | Q {or P -Q | Q} Q ( E ) | id
10
Semantic Actions Grammar G 5 E E + T [a] | T T T * F [m] | F F ( E ) | id [p] [a] = add – add top two elements of semantic stack and emit add instruction. [m] =multiply – multiply top two elements of semantic stack and emit multiply instruction. [p] = load current token in register R i and push current token on semantic stack
11
Input String for G 2 $ id 0 + id 1 * id 2 $
12
Parse Stack for Grammar G 2 id 1 id 0 TF[p] ETF **id 2 ++FF[p] TT[m] E[a] $$$
13
Semantic Stack for G 2 id 2 id 1 R2R2 id 0 R2R2
14
Instructions Emitted by G 2 load R 0 id 0 load R 1 id 1 load R 2 id 2 mul R 1 R 2 add R 0 R 2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.