Anda di halaman 1dari 8

Machine Code

Program
v = 5;
if (v>5)
x = 12 + v;
while (x !=3) {
x = x - 3;
v = 10;
}
......

Compilers

Add v,v,0
cmp v,5
jmplt ELSE
THEN:
add x, 12,v
ELSE:
WHILE:
cmp x,3
...

Compiler

Compiler
Lexical
analyzer

input

A parser knows the grammar


of the programming language

parser

output
machine
code

program

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

The parser finds the derivation


of a particular input
derivation
input
10 + 2 * 5

Parser
E -> E + E
|E*E
| INT

E =>
=>
=>
=>
=>

E+E
E+E*E
10 + E*E
10 + 2 * E
10 + 2 * 5
6

1
PDF created with pdfFactory Pro trial version www.pdffactory.com

derivation tree
derivation
E =>
=>
=>
=>
=>

derivation tree

E+E
E+E*E
10 + E*E
10 + 2 * E
10 + 2 * 5

E
10

E
E
5

10

machine code

+
E
2

mult a, 2, 5
add b, 10, a

E
5

Parsing

input
string

Parser
derivation

grammar

10

Exhaustive Search

Example:

S SS | aSb | bSa |
Parser
input

aabb

S SS
S aSb
S bSa
S

Phase 1:

derivation

S SS
S aSb
S bSa

Find derivation of

aabb

S
All possible derivations of length 1
11

12

2
PDF created with pdfFactory Pro trial version www.pdffactory.com

Phase 2

S SS | aSb | bSa |

S SS SSS
S SS

aabb

S aSb
S bSa

Phase 1

S SS aSbS
S SS bSaS

S SS

S SS S

S aSb

S aSb aSSb
S aSb aaSbb
S aSb abSab
S aSb ab

S
13

S SS | aSb | bSa |

Phase 2

S SS SSS
S SS aSbS
S SS S

aabb

14

Final result of exhaustive search


(top-down parsing)
Parser

aabb

input

aabb

S aSb aSSb
S aSb aaSbb

S SS
S aSb
S bSa
S
derivation

Phase 3

S aSb aaSbb aabb

S aSb aaSbb aabb


15

Time complexity of exhaustive search

16

For grammar with

k rules

Time for phase 1:

Suppose there are no productions of the form

A
A B
Number of phases for string

k possible derivations
w:

2| w|

17

18

3
PDF created with pdfFactory Pro trial version www.pdffactory.com

Time for phase 2:

Time for phase

k2
k2

2 | w |: k 2|w|
k 2|w|

possible derivations

possible derivations

19

Total time needed for string

There exist faster algorithms


for specialized grammars

w:

k + k 2 + + k 2|w|
phase 1

phase 2

20

S-grammar:

A ax

symbol

phase 2|w|

Extremely bad!!!

Pair

( A, a )

string
of variables
appears once

21

22

For S-grammars:

S-grammar example:

S aS
S bSS
S c

In the exhaustive search parsing


there is only one choice in each phase
Time for a phase:

Each string has a unique derivation


Total time for parsing string

S aS abSS abcS abcc


23

w:

| w|
24

4
PDF created with pdfFactory Pro trial version www.pdffactory.com

For general context-free grammars:


There exists a parsing algorithm
that parses a string | w |
in time | w |3

Simplifications
of
Context-Free Grammars

25

In general:

A Substitution Rule

A xBz

Equivalent
grammar

A a
A aaA
A abBc
B abbA
Bb

Substitute B

26

B y1 | y2 | | yn

A a
A aaA
A ababbAc
A abbc

Substitute
B

A xy1z | xy2 z | | xyn z

equivalent
grammar

27

Another grammar:

Useless Productions

S aSb
S
SA
A aA

28

SA
A aA
Useless Production

A
B bA

Useless Production

Not reachable from S

Some derivations never terminate...

S A aA aaA aa aA
29

30

5
PDF created with pdfFactory Pro trial version www.pdffactory.com

In general:
If

S xAy w
w L(G )

Then variable

is useful

Otherwise, variable

is useless

A production A x is useful
if all its variables are useful

31

Removing Useless Productions

32

First:

Example Grammar:

S aS | A | C
Aa
B aa
C aCb

find all variables that produce


strings with only terminals

S aS | A | C
A a
B aa
C aCb

Round 1:

{ A, B}

Round 2:

{ A, B, S }

33

Keep only the variables


that produce terminal symbols

S aS | A | C
A a
B aa
C aCb

34

Second: Find all variables


reachable from

{ A, B, S }

Dependency Graph

S aS | A
A a
B aa

S aS | A
Aa
B aa
35

B
not
reachable
36

6
PDF created with pdfFactory Pro trial version www.pdffactory.com

Nullable Variables

Keep only the variables


reachable from S
Final Grammar

S aS | A
A a
B aa

S aS | A
Aa

production :

Nullable Variable:

37

38

Removing Nullable Variables


Final Grammar

Example Grammar:

S aMb
M aMb
M

S aMb
M aMb
M

Substitute

S aMb
S ab
M aMb
M ab

Nullable variable
39

Unit-Productions

40

Removing Unit Productions


Observation:

Unit Production:

A A

AB

Is removed immediately

41

42

7
PDF created with pdfFactory Pro trial version www.pdffactory.com

Example Grammar:

S aA
Aa

S aA
Aa

A B
BA
B bb

A B
BA
B bb

S aA | aB
A a

Substitute

A B

B A| B
B bb

43

S aA | aB
A a
B A| B
B bb

Remove

BB

44

S aA | aB
A a

S aA | aB
A a

BA
B bb

BA
B bb

Substitute

BA

S aA | aB | aA
Aa
B bb

45

46

Removing All

Remove repeated productions

Final grammar

S aA | aB | aA
Aa
B bb

S aA | aB
A a
B bb

Step 1: Remove Nullable Variables


Step 2: Remove Unit-Productions
Step 3: Remove Useless Variables

47

48

8
PDF created with pdfFactory Pro trial version www.pdffactory.com

Anda mungkin juga menyukai