Download presentation

Presentation is loading. Please wait.

Published byKai Arrasmith Modified over 2 years ago

1
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
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
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 Stack S aSbaSb

4
4 Top-Down Parser S -> aSb S -> c a a c b b input Stack S aSbaSb SbSb

5
5 Top-Down Parser S -> aSb S -> c a a c b b input Stack S aSbaSb SbSb aSbbaSbb

6
6 Top-Down Parser S -> aSb S -> c a a c b b input Stack S aSbaSb SbSb aSbbaSbb SbbSbb

7
7 Top-Down Parser S -> aSb S -> c a a c b b input S aSbaSb SbSb aSbbaSbb SbbSbb cbbcbb

8
8 Top-Down Parser S -> aSb S -> c a a c b b input S aSbaSb SbSb aSbbaSbb SbbSbb cbbcbb

9
9 Top-Down Parser S -> aSb S -> c a a c b b input S aSbaSb SbSb aSbbaSbb SbbSbb bbbb

10
10 Top-Down Parser S -> aSb S -> c a a c b b input S aSbaSb SbSb aSbbaSbb SbbSbb b Stack empty Input empty DONE!!

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

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

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

16
16 Bottom-Up Parser S -> aSb S -> c a a c b b a aaaa caacaa

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

18
18 Bottom-Up Parser S -> aSb S -> c a a c b b a aaaa caacaa SaaSaa bSaabSaa

19
19 Bottom-Up Parser S -> aSb S -> c a a c b b a aaaa caacaa SaaSaa bSaabSaa Wait! Another match! Reduce aSb to S SaSa

20
20 Bottom-Up Parser S -> aSb S -> c a a c b b a aaaa caacaa SaaSaa bSaabSaa bSabSa

21
21 Bottom-Up Parser S -> aSb S -> c a a c b b a aaaa caacaa SaaSaa bSaabSaa Another match! S SaSa bSabSa Input is empty. Stack has only S on it. DONE!!

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

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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google