Professional Documents
Culture Documents
Languages and Finite Automata - Class09
Languages and Finite Automata - Class09
Languages and Finite Automata - Class09
1
Machine Code
Program Add v,v,0
v = 5; cmp v,5
if (v>5) jmplt ELSE
x = 12 + v; THEN:
while (x !=3) { Compiler
add x, 12,v
x = x - 3; ELSE:
v = 10; WHILE:
} cmp x,3
...... ...
2
Compiler
Lexical
parser
analyzer
input output
machine
program
code
3
A parser knows the grammar
of the programming language
4
Parser
PROGRAM → STMT_LIST
STMT_LIST → STMT; STMT_LIST | STMT;
STMT → EXPR | IF_STMT | WHILE_STMT
| { STMT_LIST }
derivation
Parser
input E => E + E
E -> E + E
=> E + E * E
10 + 2 * 5 |E*E
=> 10 + E*E
| INT
=> 10 + 2 * E
=> 10 + 2 * 5
6
derivation tree
derivation
E
E => E + E E + E
=> E + E * E
=> 10 + E*E 10
E * E
=> 10 + 2 * E
=> 10 + 2 * 5 2 5
7
derivation tree
E machine code
E + E mult a, 2, 5
add b, 10, a
10
E * E
2 5
8
Parsing
9
Parser
input
grammar derivation
string
10
Example:
Parser
S → SS derivation
input
S → aSb
aabb ?
S → bSa
S →
11
Exhaustive Search
S → SS | aSb | bSa |
13
Phase 2 S → SS | aSb | bSa |
S SS SSS
S SS aSbS aabb
Phase 1 S SS bSaS
S SS S SS S
S aSb S aSb aSSb
S aSb aaSbb
S aSb abSab
S aSb ab 14
S → SS | aSb | bSa |
Phase 2
S SS SSS
S SS aSbS aabb
S SS S
S aSb aSSb
S aSb aaSbb
Phase 3
S aSb aaSbb aabb
15
Final result of exhaustive search
(top-down parsing)
Parser
S → SS
input
S → aSb
aabb
S → bSa
S →
derivation
A→
A→ B
Number of phases for string w : 2| w|
Each step in the derivation increases the
length of the sentential form or the number
of nonterminal symbols.
17
Why 2|w| and not just |w|?
Because S=>AS=>ABS=>ABC=>aBC=>abC=>abc
18
For grammar with k rules
k possible derivations
19
Time for phase 2: 2
k
2 possible derivations
k
20
Time for phase 2 | w |: k 2|w|
21
Total time needed for string w:
2 2|w|
k + k ++ k
Extremely bad!!!
22
There exist faster algorithms
for specialized grammars
S-grammar: A → ax
symbol string
of variables
S → aS
S → bSS
S →c
25
For general context-free grammars:
27
A Substitution Rule
Equivalent
grammar
A→a
A→a
A → aaA
A → aaA
A → abBc Substitute B
A → ababbAc
B → abbA
A → abbc
B→b
28
In general:
A → xBz
B → y1 | y2 | | yn
Substitute
B
equivalent
A → xy1z | xy2 z | | xyn z
grammar
29
Useless Productions
S → aSb
S →
S→A
A → aA Useless Production
S A aA aaA aaaA
30
Another grammar:
S→A
A → aA
A→
B → bA Useless Production
31
In general:
If S xAy w
w L(G )
Then variable A is useful
32
A production A → x is useful
if all its variables are useful
33
Removing Useless Productions
Example Grammar:
S → aS | A | C
A→a
B → aa
C → aCb
34
First: find all variables that produce
strings with only terminals
S → aS | A | C Round 1: { A, B}
A→a
B → aa
C → aCb Round 2: { A, B, S}
35
Keep only the variables
{ A, B, S}
that produce terminal symbols
S → aS | A | C
A→a S → aS | A
B → aa A→a
C → aCb B → aa
36
Second: Find all variables
reachable from S
Dependency Graph
S → aS | A
A→a S A B
B → aa not
reachable
37
Keep only the variables
reachable from S
Final Grammar
S → aS | A
S → aS | A
A→a
A→a
B → aa
38
Nullable Variables
− production : A→
39
Removing Nullable Variables
Example Grammar:
S → aMb
M → aMb
M →
Nullable variable
40
Final Grammar
S → aMb
S → aMb
Substitute S → ab
M → aMb M →
M → aMb
M →
M → ab
41
Unit-Productions
Unit Production: A→ B
42
Removing Unit Productions
Observation:
A→ A
Is removed immediately
43
Example Grammar:
S → aA
A→a
A→ B
B→A
B → bb
44
S → aA
S → aA | aB
A→a
Substitute A→a
A→ B A→ B B → A| B
B→A
B → bb
B → bb
45
S → aA | aB S → aA | aB
A→a Remove A→a
B → A| B B→B B→A
B → bb B → bb
46
S → aA | aB
S → aA | aB | aA
A→a Substitute
B→A A→a
B→A
B → bb
B → bb
47
Remove repeated productions
Final grammar
S → aA | aB | aA S → aA | aB
A→a A→a
B → bb B → bb
48
Removing All
49