24
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)

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

Embed Size (px)

Citation preview

Page 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

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)

Page 2: 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

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.

Page 3: 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

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

Stack

aSb

Page 4: 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

4

Top-Down Parser

S -> aSb

S -> c

a a c b b

input

Stack

S

Stack

aSb

Stack

Sb

Page 5: 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

5

Top-Down Parser

S -> aSb

S -> c

a a c b b

input

Stack

S

Stack

aSb

Stack

Sb

Stack

aSbb

Page 6: 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

6

Top-Down Parser

S -> aSb

S -> c

a a c b b

input

Stack

S

Stack

aSb

Stack

Sb

Stack

aSbb

Stack

Sbb

Page 7: 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

7

Top-Down Parser

S -> aSb

S -> c

a a c b b

input

S

aSb

Sb

aSbb

Sbb

cbb

Page 8: 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

8

Top-Down Parser

S -> aSb

S -> c

a a c b b

input

S

aSb

Sb

aSbb

Sbb

cbb

Page 9: 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

9

Top-Down Parser

S -> aSb

S -> c

a a c b b

input

S

aSb

Sb

aSbb

Sbb

bb

Page 10: 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

10

Top-Down Parser

S -> aSb

S -> c

a a c b b

input

S

aSb

Sb

aSbb

Sbb b

Stack emptyInput empty

DONE!!

Page 11: 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

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

Page 12: 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

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

Page 13: 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

13

Bottom-Up Parser

S -> aSb

S -> c a a c b b

input

Stack

Nothing on Stack to start the parsing process

StackS

If you end up with S on the stack and nothing else in input, then you are done!!

Page 14: 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

14

Bottom-Up Parser

S -> aSb

S -> c a a c b b

a

Shift onto stack

Page 15: 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

15

Bottom-Up Parser

S -> aSb

S -> c a a c b b

a

aa

Page 16: 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

16

Bottom-Up Parser

S -> aSb

S -> c a a c b b

aaa

caa

Page 17: 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

17

Bottom-Up Parser

S -> aSb

S -> c a a c b b

aaa

caa

Wait! We have a match!

Reduce c to S

Saa

Page 18: 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

18

Bottom-Up Parser

S -> aSb

S -> c a a c b b

aaa

caa

Saa

bSaa

Page 19: 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

19

Bottom-Up Parser

S -> aSb

S -> c a a c b b

aaa

caa

Saa

bSaa

Wait! Another match!

Reduce aSb to S

Sa

Page 20: 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

20

Bottom-Up Parser

S -> aSb

S -> c a a c b b

aaa

caa

Saa

bSaa

bSa

Page 21: 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

21

Bottom-Up Parser

S -> aSb

S -> c a a c b b

aaa

caa

Saa

bSaa

Another match!

SSa

bSa

Input is empty.Stack has only S on it. DONE!!

Page 22: 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

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

Page 23: 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

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.

Page 24: 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

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

programpreprocessor