1 / 9

LL Parsing – A Review

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

pearly
Télécharger la présentation

LL Parsing – A Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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:

  2. 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.

  3. 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 $

  4. 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

  5. ..and here is one I made earlier...

  6. 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.

  7. LR Parse Table Action Table Goto Table

  8. 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

  9. 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.

More Related