Title: Compilers
1Compilers
2Machine Code
Program
Add v,v,0 cmp v,5 jmplt ELSE THEN add x,
12,v ELSE WHILE cmp x,3 ...
v 5 if (vgt5) x 12 v while (x !3)
x x - 3 v 10 ......
Compiler
3Compiler
Lexical analyzer
parser
input
output
machine code
program
4A parser knows the grammar of the programming
language
5Parser
PROGRAM STMT_LIST STMT_LIST STMT
STMT_LIST STMT STMT EXPR IF_STMT
WHILE_STMT
STMT_LIST EXPR EXPR EXPR EXPR - EXPR
ID IF_STMT if (EXPR) then STMT
if (EXPR) then STMT else STMT WHILE_STMT
while (EXPR) do STMT
6The parser finds the derivation of a particular
input
derivation
Parser
input
E gt E E gt E E E gt 10 EE gt
10 2 E gt 10 2 5
E -gt E E E E INT
10 2 5
7derivation tree
derivation
E
E gt E E gt E E E gt 10 EE gt
10 2 E gt 10 2 5
E
E
10
E
E
2
5
8derivation tree
E
machine code
E
E
mult a, 2, 5 add b, 10, a
10
E
E
2
5
9Parsing
10Parser
input string
derivation
grammar
11Example
Parser
derivation
input
?
12Exhaustive Search
Phase 1
Find derivation of
All possible derivations of length 1
13(No Transcript)
14Phase 2
Phase 1
15Phase 2
Phase 3
16Final result of exhaustive search
(top-down parsing)
Parser
input
derivation
17Time complexity of exhaustive search
Suppose there are no productions of the form
Number of phases for string
18For grammar with rules
Time for phase 1
possible derivations
19Time for phase 2
possible derivations
20Time for phase
possible derivations
21Total time needed for string
phase 1
phase 2w
phase 2
Extremely bad!!!
22There exist faster algorithms for specialized
grammars
S-grammar
symbol
string of variables
appears once
Pair
23S-grammar example
Each string has a unique derivation
24For S-grammars
In the exhaustive search parsing there is only
one choice in each phase
Time for a phase
Total time for parsing string
25For general context-free grammars
There exists a parsing algorithm that parses a
string in time
26Simplifications of Context-Free Grammars
27A Substitution Rule
Equivalent grammar
Substitute B
28In general
Substitute B
equivalent grammar
29Useless Productions
30Another grammar
Not reachable from S
31In general
If
Then variable is useful
Otherwise, variable is useless
32A production is useful if all its
variables are useful
33Removing Useless Productions
Example Grammar
34First
find all variables that produce strings with only
terminals
Round 1
Round 2
35Keep only the variables that produce terminal
symbols
36Second
Find all variables reachable from
Dependency Graph
not reachable
37Keep only the variables reachable from S
Final Grammar
38Nullable Variables
Nullable Variable
39Removing Nullable Variables
Example Grammar
Nullable variable
40Final Grammar
Substitute
41Unit-Productions
Unit Production
42Removing Unit Productions
Observation
Is removed immediately
43Example Grammar
44Substitute
45Remove
46Substitute
47Remove repeated productions
Final grammar
48Removing All
- Step 1 Remove Nullable Variables
- Step 2 Remove Unit-Productions
- Step 3 Remove Useless Variables