100 likes | 188 Views
LL Parsing – A Review. Took a Grammar: 1) Constructed First Sets 2) Constructed Follow Sets 3) Built LL(1) Parse Table using these sets. 4) Given an input we can decide per token which rules to parse the input. Limitations of LL grammars:. LR Parsing - Introduction. Terminology
E N D
LL Parsing – A Review Took a Grammar: 1) Constructed First Sets 2) Constructed Follow Sets 3) Built LL(1) Parse Table using these sets. 4) Given an input we can decide per token which rules to parse the input. Limitations of LL grammars:
LR Parsing - Introduction Terminology The parser has a stack and an input. The first k tokens of the input are called the lookahead. Based on the contents of the stack and the lookahead, the parser performs 2 kinds of actions: 1) Shift: push the input Token onto the Stack. 2) Reduce: We have a rule X -> A B C. C, B and A are already on the stack. We pop C, B and A off the stack and push X onto the stack. We repeat this process until an error occurs or the input is accepted. Shift/Reduce Parsing < -- > LR Parsing.
Shift/Reduce Parse – An Example Given the following grammar: 1) s -> e 2) e -> e PLUS t 3) e -> t 4) t -> NUM and input: 1 + 2 + 3 $
Constructing LR Parse Tables 2 Steps involved 1) Compute Closure of LR Items 2) Move the “dot” to add new Items. Example: 0) START -> s $ 1) s -> e 2) e -> e PLUS t 3) t -> ( e ) 4) t -> IDENTIFIER
..and here is one I made earlier...
Creating the Parse Table 1) All terminal transistions go into ACTION Table. 2) All non-terminal transistions go into GOTO Table. 3) We then calculate the FOLLOW sets. 4) All states with the “dot” at the end are Reduce States. 5) We fill in Reduces under their FOLLOW sets.
LR Parse Table Action Table Goto Table
Conflicts when Parsing Shift/Reduce Conflict s -> IF stmt s -> IF stmt ELSE other e -> e MULT e e -> e PLUS e e -> IDENT 5 * 6 – 4
Reduce/Reduce Conflict e -> IDENT PLUS LITERAL t -> IDENT PLUS LITERAL b + 5 Resolve by choosing the first production that appears in the grammar. In this case we always reduce with rule e.