Upload
kai-arrasmith
View
214
Download
0
Embed Size (px)
Citation preview
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
Stack
S
Stack
aSb
4
Top-Down Parser
S -> aSb
S -> c
a a c b b
input
Stack
S
Stack
aSb
Stack
Sb
5
Top-Down Parser
S -> aSb
S -> c
a a c b b
input
Stack
S
Stack
aSb
Stack
Sb
Stack
aSbb
6
Top-Down Parser
S -> aSb
S -> c
a a c b b
input
Stack
S
Stack
aSb
Stack
Sb
Stack
aSbb
Stack
Sbb
7
Top-Down Parser
S -> aSb
S -> c
a a c b b
input
S
aSb
Sb
aSbb
Sbb
cbb
8
Top-Down Parser
S -> aSb
S -> c
a a c b b
input
S
aSb
Sb
aSbb
Sbb
cbb
9
Top-Down Parser
S -> aSb
S -> c
a a c b b
input
S
aSb
Sb
aSbb
Sbb
bb
10
Top-Down Parser
S -> aSb
S -> c
a a c b b
input
S
aSb
Sb
aSbb
Sbb b
Stack emptyInput empty
DONE!!
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
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!!
14
Bottom-Up Parser
S -> aSb
S -> c a a c b b
a
Shift onto stack
15
Bottom-Up Parser
S -> aSb
S -> c a a c b b
a
aa
16
Bottom-Up Parser
S -> aSb
S -> c a a c b b
aaa
caa
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
18
Bottom-Up Parser
S -> aSb
S -> c a a c b b
aaa
caa
Saa
bSaa
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
20
Bottom-Up Parser
S -> aSb
S -> c a a c b b
aaa
caa
Saa
bSaa
bSa
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!!
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
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
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