Languages and Finite Automata - Class09

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

Compilers

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 }

EXPR → EXPR + EXPR | EXPR - EXPR | ID


IF_STMT → if (EXPR) then STMT
| if (EXPR) then STMT else STMT
WHILE_STMT → while (EXPR) do STMT
5
The parser finds the derivation
of a particular input

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

Phase 1: S  SS Find derivation of


S  aSb aabb
S  bSa
S
All possible derivations of length 1
12
S  SS aabb
S  aSb
S  bSa
S

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

S  aSb  aaSbb  aabb


16
Time complexity of exhaustive search

Suppose there are no productions of the form

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

Time for phase 1: k

k possible derivations

19
Time for phase 2: 2
k

2 possible derivations
k

20
Time for phase 2 | w |: k 2|w|

2|w| possible derivations


k

21
Total time needed for string w:

2 2|w|
k + k ++ k

phase 1 phase 2 phase 2|w|

Extremely bad!!!
22
There exist faster algorithms
for specialized grammars

S-grammar: A → ax

symbol string
of variables

Pair ( A, a ) appears once


23
S-grammar example:

S → aS
S → bSS
S →c

Each string has a unique derivation…

S  aS  abSS  abcS  abcc


24
For S-grammars:

In the exhaustive search parsing


there is only one choice in each phase

Time for a phase: 1

Total time for parsing string w: | w|

25
For general context-free grammars:

There exists a parsing algorithm


that parses a string | w |
in time | w |3

Linz 6th, Theorem 5.3, page 144.

Linz 6th, Section 6.3, page 178ff – the


CYK algorithm. 26
Simplifications
of
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

Some derivations never terminate...

S  A  aA  aaA    aaaA  
30
Another grammar:

S→A
A → aA
A→
B → bA Useless Production

Not reachable from S

31
In general:

If S    xAy    w
w L(G )
Then variable A is useful

Otherwise, variable A is useless

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

Nullable Variable: 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

Step 1: Remove Nullable Variables

Step 2: Remove Unit-Productions

Step 3: Remove Useless Variables

49

You might also like