lecture6 syntax analysis_2

20

Click here to load reader

Upload: mahesh-kumar-chelimilla

Post on 12-Apr-2017

14 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Lecture6 syntax analysis_2

Lecture 6

Syntax Analysis II

Page 2: Lecture6 syntax analysis_2

Derivation

• A derivation is the sequence of productions

S � … � … � … � …

• A derivation can be drawn as a tree• A derivation can be drawn as a tree

– Start symbol is the tree’s root

– For a production X � Y1 … … Yn add children

Y1 … … Yn to node X

X

Y1 … … Yn28-Jan-15 2CS 346 Lecture 5

Page 3: Lecture6 syntax analysis_2

Example: Left-most derivation

• Grammar: E� E + E | E * E | ( E ) | id

• String : id*id+id E

E

� E + E E + E� E + E E + E

� E * E + E

� id * E + E E * E id

� id * id + E

� id * id + id id id Parse Tree

28-Jan-15 3CS 346 Lecture 5

Page 4: Lecture6 syntax analysis_2

Parse Tree

• A parse tree has

– Terminals at the leaves

– Non-terminal at the interior nodes

• An in-order traversal of the leaves is the

original input

• The parse tree shows the association of

operations, the input string does not

28-Jan-15 4CS 346 Lecture 5

Page 5: Lecture6 syntax analysis_2

Right-most derivation

• The example is a left-

most derivation

– At each step, replace the

left most non terminals

E

� E + E

� E * E + E

• There is a equivalent

notion of right-most

derivation

� E * E + E

� id * E + E

� id * id + E

� id * id + id

28-Jan-15 5CS 346 Lecture 5

Page 6: Lecture6 syntax analysis_2

Parse Tree Derivations

• Note that the left-most and right-most

derivation have the same parse tree

• Every parse tree has the left-most and right-• Every parse tree has the left-most and right-

most derivations

28-Jan-15 6CS 346 Lecture 5

Page 7: Lecture6 syntax analysis_2

Ambiguity

• Grammar: E� E + E | E * E | ( E ) | id

• String : id*id+id

• This string has two parse trees

E E

E + E E * E

E * E id id E + E

id id id id

28-Jan-15 7CS 346 Lecture 5

Page 8: Lecture6 syntax analysis_2

Ambiguous Grammar

• A grammar is ambiguous if it has more than

one parse tree for some string

– Equivalently, there is more than one right-most or

left-most derivation for some stringleft-most derivation for some string

• Ambiguity means that some programs are ill-

defined

28-Jan-15 8CS 346 Lecture 5

Page 9: Lecture6 syntax analysis_2

How to handle Ambiguous Grammar

• Several ways to handle

• Rewrite the grammar unambiguously

E � E’ + E| E’

E’ � id * E’ | id| ( E ) * E | ( E )

• Enforce precedence of * over +28-Jan-15 9CS 346 Lecture 5

Page 10: Lecture6 syntax analysis_2

• Grammar: E� E’+ E | E’

E’� id * E’ | id | ( E ) * E’ | ( E )

• String : id*id+id

E

Enforce precedence of * over + by

divide the productions in two classes;

one handle + and one handles *; so one

non terminals for each operators

How to handle Ambiguous Grammar

E

E’ + E

id * E’ E’

id id

non terminals for each operators

E � E’+ E � E’ + E’ + E �

E’+E’+E’+E � E’+… +E’

Handles +

E’ � id * E’ � id*id*E’�

id*id*id*E’ � id*…*id

Handles *28-Jan-15 10CS 346 Lecture 5

Page 11: Lecture6 syntax analysis_2

How it works

• The grammar has two separate groups ofproductions

• All the pluses (+) must be generated before any ofthe times (*); so time(s) (*) are nested moredeeply inside the parse tree; pluses (+) aredeeply inside the parse tree; pluses (+) aregenerated in the outermost levels and times (*)are generated inside the pluses (+).

• So the grammar enforces that the time (*) hashigher precedence than the plus (+)

28-Jan-15 11CS 346 Lecture 5

Page 12: Lecture6 syntax analysis_2

Ambiguity

• Grammar: E� E + E | E * E | ( E ) | id

• String : id*id+id

• This string has two parse trees

E E

E + E E * E

E * E id id E + E

id id id id

28-Jan-15 12CS 346 Lecture 5

Page 13: Lecture6 syntax analysis_2

Ambiguity

• Grammar: E� E + E | E * E | ( E ) | id

• String : id*id+id

• Now, this string has only one parse trees

EE

E + E

E * E id

id id

E’ + E

id * E’ E’

id id

28-Jan-15 13CS 346 Lecture 5

Page 14: Lecture6 syntax analysis_2

Example

• Another Expression

E � if E then E

| if E then E else E

| OTHER

• The expression: if E1 then if E2 then E3 else E4 has two separate

parse treesparse trees

if if

E1 if E4 E1 if

E2 E3 E2 E3 E4

28-Jan-15 14CS 346 Lecture 5

Page 15: Lecture6 syntax analysis_2

How to handle the Ambiguity

• The property that we want is: else matches the closest unmatched then

• Can be resolved as:

E ���� MIF| UIFE ���� MIF| UIF

MIF ���� if E then MIF else MIF

| OTHER

UIF ���� if E then E

| if E MIF else UIF

28-Jan-15 15CS 346 Lecture 5

Page 16: Lecture6 syntax analysis_2

Associativity of Operators

• Let a Grammar : S � S + S | S – S | 0 – 9

• Input: 9 – 5 + 2

S S

S + S S - S S + S S - S

S - S 2 9 S + S

9 5 5 2

(9-5)+2 = 6 9-(5+2) = 228-Jan-15 16CS 346 Lecture 5

Page 17: Lecture6 syntax analysis_2

How to handle

• When an operand like 5 has operators to its left and

right, conventions are needed for deciding which

operator applies to that operand.

• Rule: operator + is associates to the left. Arithmetic • Rule: operator + is associates to the left. Arithmetic

operators (+, -, *, /) are left associative.

• Assignment operator (“=“) in C is right associative.

28-Jan-15 17CS 346 Lecture 5

Page 18: Lecture6 syntax analysis_2

How to handle

• Left Associative

S � S + d | S – d | d

d � [0-9]

• Input: 9 – 5 + 2

S

• Right Associative

r � l = r | l

l � [a-z]

• Input: a = b = c

r

S + d

S – d 2

d 5

9

Left-most derivation

l = r

a l = r

b l

c

Right-most derivation

28-Jan-15 18CS 346 Lecture 5

Page 19: Lecture6 syntax analysis_2

Push Down Automata

• The language for balanced parenthesis, i.e.

{( i ) i | i ≥ 0}, CFG productions are

S � (S)|S

S � ɛ

• The situation can be handled if the DFA is • The situation can be handled if the DFA is

augmented with memory

– Memory implemented as a stack

– Such an automata is called Push Down Automata

(PDA)

28-Jan-15 19CS 346 Lecture 5

Page 20: Lecture6 syntax analysis_2