1 / 24

Week 9

Week 9. Questions / Concerns Hand back Test#2 What’s due: Final Project due next Thursday June 5. Final Project check-off on Friday June 6 in class. Next class meeting: Next Friday June 6. Please use the other class times for your final project. Top-Down vs. Bottom-Up Parsers

manasa
Download Presentation

Week 9

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. Week 9 • Questions / Concerns • Hand back Test#2 • What’s due: • Final Project due next Thursday June 5. • Final Project check-off on Friday June 6 in class. • Next class meeting: Next Friday June 6. • Please use the other class times for your final project. • Top-Down vs. Bottom-Up Parsers • LR(1) parsers • Parser tools • Lex&Yacc (Bottom-Up) • Microsoft’s Spirit (Top-Down) • Gold Parser (Bottom-Up)

  2. Top Down vs. Bottom Up parsers • Problems in top-town parsers • Left-recursion • Common prefixes requiring left factor. • Bottom-up parsers can handle the largest class of grammars that can be parsed deterministically.

  3. Top – Down Parser • General procedure: • Put Start symbol on the stack • Grab a token and grab a rule. • The rule goes on the stack. • Try to match the tokens with rules on the stack. S -> aSb S -> c a a c b b input aS b S Stack Stack

  4. Top-Down Parser S -> aSb S -> c a a c b b input aS b S b S Stack Stack Stack

  5. Top-Down Parser S -> aSb S -> c a a c b b input a S b b aS b S b S Stack Stack Stack Stack

  6. Top-Down Parser S -> aSb S -> c a a c b b input S b b a S b b aS b S b S Stack Stack Stack Stack Stack

  7. Top-Down Parser S -> aSb S -> c a a c b b input S b b c b b a S b b aS b S b S

  8. Top-Down Parser S -> aSb S -> c a a c b b input S b b c b b a S b b aS b S b S

  9. Top-Down Parser S -> aSb S -> c a a c b b input S b b b b a S b b aS b S b S

  10. Top-Down Parser S -> aSb S -> c a a c b b input Stack empty Input empty DONE!! S b b b a S b b aS b S b S

  11. Bottom Up Parser • General Procedure • Table-driven parser. • LR(1) parser table • R – stands for rightmost derivation. Working from the bottom up to the top of the parse tree. • There are 2 basic operations: • Shift – moves input to the stack. • Reduce – remove items on the stack and reduces to one non-terminal. • Also called shift-reduce parsers

  12. Bottom Up Parser table • Unlike LL(1), LR(1) has 3 different types of tables: • SLR(1) – simple LR(1) • LALR(1) – look ahead LR • LR(1) • All 3 parser tables are one token look ahead. • Unlike LL(1), left recursions in the grammar are okay. Most grammar rules do not need to be left factored. • Unit productions are also okay. • Lambdas can be very tricky. Try to reduce duplicate lambdas. Ex: S -> aSb A -> aA S -> A A -> bA S ->  A -> 

  13. Bottom-Up Parser S -> aSb S -> c a a c b b input If you end up with S on the stack and nothing else in input, then you are done!! Nothing on Stack to start the parsing process S Stack Stack

  14. Bottom-Up Parser S -> aSb S -> c a a c b b Shift onto stack a

  15. Bottom-Up Parser S -> aSb S -> c a a c b b a a a

  16. Bottom-Up Parser S -> aSb S -> c a a c b b c a a a a a

  17. Bottom-Up Parser S -> aSb S -> c a a c b b Wait! We have a match! Reduce c to S c a a S a a a a a

  18. Bottom-Up Parser S -> aSb S -> c a a c b b bS a a c a a S a a a a a

  19. Bottom-Up Parser S -> aSb S -> c a a c b b Wait! Another match! Reduce aSb to S bS a a c a a S a a S a a a a

  20. Bottom-Up Parser S -> aSb S -> c a a c b b bS a a bS a c a a S a a a a a

  21. Bottom-Up Parser S -> aSb S -> c a a c b b Input is empty. Stack has only S on it. DONE!! Another match! bS a a S a c a a S a a bS a a a S a

  22. SLR(1) table 0: E’ -> E 1: E -> E + T 2: E -> T 3: T -> T * F 4: T -> F 5: F -> ( E ) 6: F -> id

  23. Parser Tools Grammar Specification (Regular Expression, Context-Free Grammar, BNF grammar) Parser Generator Tools Parser Code (source files) Most parser generator tools build table-driven parsers. They just have to produce the tables.

  24. Structure of Compilers skeletal source program preprocessor Modified Source Program Syntax Analysis (Parser) Lexical Analyzer (scanner) Tokens Syntactic Structure Semantic Analysis Intermediate Representation Optimizer Symbol Table Code Generator Target machine code

More Related