Parsing Algorithm to Build the Table Example The
Parsing
Algorithm to Build the Table
Example: The “Dangling Else” Grammar
Example: The “Dangling Else” Grammar
Example: The “Dangling Else” Grammar
Example: The “Dangling Else” Grammar
Example: The “Dangling Else” Grammar
Example: The “Dangling Else” Grammar
Example: The “Dangling Else” Grammar
Example: The “Dangling Else” Grammar
Example: The “Dangling Else” Grammar
Example: The “Dangling Else” Grammar
Example: The “Dangling Else” Grammar
Example: The “Dangling Else” Grammar
Generate the parse table for the following grammar:
Solution
LL(1) Grammar • LL(1) grammars – Are never ambiguous. – Will never have left recursion. • Furthermore. . . – If we are looking for an “A” and the next symbol is “b”, – Then only one production must be possible
LL(1) Grammar • A grammar G is LL(1) if an only if whenever A a | b are two distinct productions of G the following conditions hold: 1. For no terminal a do both a and b derive strings beginning with a. 2. At most one of a and b can derive the empty string. 3. If then b * e , then a does not derive any string beginning with a terminal in FOLLOW(A).
Bottom Parsing • LR Parsing – Also called “Shift-Reduce Parsing” • Find a rightmost derivation • Finds it in reverse order • LR Grammars – Can be parsed with an LR Parser • LR Languages – Can be described with LR Grammar – Can be parsed with an LR Parser
LR Parsing Techniques • LR Parsing – Most General Approach • SLR – Simpler algorithm, but not as general • LALR – More complex, but saves space
LL vs. LR • LR (shift reduce) is more powerful than LL (predictive parsing) • Can detect a syntactic error as soon as possible. • LR is difficult to do by hand (unlike LL)
Rightmost Derivation
Rightmost Derivation In reverse
Rightmost Derivation In reverse LR parsing corresponds to rightmost derivation in reverse
LR(k) Parsing – Bottom Up • Construct parse tree from leaves, ‘reducing’ the string to the start symbol (and a single tree) • During parse, we have a ‘forest’ of trees • Shift-reduce parsing – ‘Shift’ a new input symbol – ‘Reduce’ a group of symbols to a single non -terminal – Choice is made using the k lookaheads • LR(1)
Shift Reduce Parsing S a. TRe T Tbc|b R d Remaining input: abbcde Rightmost derivation: S a. TRe a. Tde a. Tbcde abbcde
Shift Reduce Parsing S a. TRe T Tbc|b R d Remaining input: bcde Shift a, Shift b a b Rightmost derivation: S a. TRe a. Tde a. Tbcde abbcde
Shift Reduce Parsing S a. TRe T Tbc|b R d Remaining input: bcde Shift a, Shift b Reduce T b T a b Rightmost derivation: S a. TRe a. Tde a. Tbcde abbcde
Shift Reduce Parsing S a. TRe T Tbc|b R d Remaining input: de Shift a, Shift b Reduce T b Shift b, Shift c T a b b c Rightmost derivation: S a. TRe a. Tde a. Tbcde abbcde
Shift Reduce Parsing S a. TRe T Tbc|b R d Shift a, Shift b Reduce T b Shift b, Shift c Reduce T T b c Remaining input: de T T a b b c Rightmost derivation: S a. TRe a. Tde a. Tbcde abbcde
Shift Reduce Parsing S a. TRe T Tbc|b R d Shift a, Shift b Reduce T b Shift b, Shift c Reduce T T b c Shift d Remaining input: e T T a b b c d Rightmost derivation: S a. TRe a. Tde a. Tbcde abbcde
Shift Reduce Parsing S a. TRe T Tbc|b R d Shift a, Shift b Reduce T b Shift b, Shift c Reduce T T b c Shift d Reduce R d Remaining input: e T T a b R b c d Rightmost derivation: S a. TRe a. Tde a. Tbcde abbcde
Shift Reduce Parsing S a. TRe T Tbc|b R d Shift a, Shift b Reduce T b Shift b, Shift c Reduce T T b c Shift d Reduce R d Shift e Remaining input: T T a b R b c d e Rightmost derivation: S a. TRe a. Tde a. Tbcde abbcde
Shift Reduce Parsing S a. TRe T Tbc|b R d Shift a, Shift b Reduce T b Shift b, Shift c Reduce T T b c Shift d Reduce R d Shift e Reduce S a T R e Remaining input: S T T a b R b c d e Rightmost derivation: S a. TRe a. Tde a. Tbcde abbcde
- Slides: 34