Intro
Scanner
Top-Down Parser
Syntaxt-Directed Code Generator
Interfacing LL(1) Parsers with Code Generators
Botom-Up Parsing
Interfacing LR(1) with LL(1) and Code Generator
Lex: A Scanner Generator
Yacc: A Parser Generator
Symbol Tables
Storage Organization
Run-Time Storage Managment
Code Generation for Basic Blocks
Program Analysis
Code Improvement
| S | |
ASA | |
|
ASB | ||
|
A | ||
| A | |
a | |
| B | |
b |
Input: aab
| S | |
aA | |
|
bB | ||
| A | |
Sa | |
|
Ca | ||
| B | |
bS | |
|
c | ||
| C | |
cB |
Input: acca
| E | |
TE' | |
| E' | |
+TE' | |
|
|
||
| T | |
FT' | |
| T' | |
*FT' | |
|
|
||
| F | |
(E) | |
|
I |
| S | |
CSAc | |
|
BAD | ||
| A | |
a | |
|
|
||
| B | |
bS | |
|
|
||
| C | |
c | |
| D | |
d | |
|
|
Assumptions:
happens, the top |
| entities in INFO are replaced with |A| entities.
Input: b ( ( a a ) a ) b
Grammar:
| 1. | S | |
bAb |
| 2. | A | |
(B |
| 3. | |
a | |
| 4. | B | |
Aa) |
"State" = nonterminal symbol augmented with info from production rules
Added Rule:
| S' | |
S |
| a | b | ( | ) | $ | A | B | S | S' | |
| I-0 | S-1 | S-4 | |||||||
| I-1 | S-5 | S-6 | S-2 | ||||||
| I-2 | S-3 | ||||||||
| I-3 | R-3,S | R-3,S | R-3,S | R-3,S | R-3,S | ||||
| I-4 | accept | ||||||||
| I-5 | R-1,A | R-1,A | R-1,A | R-1,A | R-1,A | ||||
| I-6 | S-5 | S-6 | S-8 | S-7 | |||||
| I-7 | R-2,A | R-2,A | R-2,A | R-2,A | R-2,A | ||||
| I-8 | S-9 | ||||||||
| I-9 | S-10 | ||||||||
| I-10 | R-3,B | R-3,B | R-3,B | R-3,B | R-3,B |
Grammar:
| S | |
A|B |
| A | |
aAc|a |
| B | |
aB|C |
| C | |
bCc|b |
LR(1)-oriented, where states with identical cores are merged
LR(0) + computed lookahed symbols for cores
Grammar:
| E | |
E+T|T |
| T | |
T*F|F |
| F | |
(E)|id |
| S' | |
S |
| S | |
AS |
|
b | |
| A | |
Sa |
|
a |
| FOLLOW(S') | = | {$} |
| {S} | {} | {$, a} |
| {A} | {} | {a, b} |
| S | |
Aa |
|
bAc | |
|
dc | |
|
bda | |
| A | |
d |
| FOLLOW(S) | = | {$} |
| {A} | {} | {a, c} |
| S | |
Aa |
|
bAc | |
|
Bc | |
|
bBa | |
| A | |
d |
| B | |
d |
NOTE: In LALR we should merge states I1 and I9 since they have the same core
Push start symbol `<program' to pda stack, and repeatedly act according to the entry on top of the stack.
auxiliary definitions:
letter = A | B | ... | Z
digit = 0 | 1 | ... | 9
Translation rules
BEGIN {return 1}
END {return 2}
letter(letter|digit)* {yyval := lookid(yytext); /* value */
return 3 /* class */ }
users can change the above default rules
L1: goto L4 L2: goto L2 BEGIN goto L1 goto L3 goto L4 L1: goto L2 END L4: goto L1 L5: