Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Parse Trees - Automata and Complexity Theory - Lecture Slides, Slides of Theory of Automata

Some concept of Automata and Complexity Theory are Administrivia, Closure Properties, Context-Free Grammars, Decision Properties, Deterministic Finite Automata, Intractable Problems, More Undecidable Problems. Main points of this lecture are: Parse Trees, Relationship, Left-, Rightmost Derivations, Ambiguity in Grammars, Parse Trees, Leaves, Interior Nodes, Root, Labeled

Typology: Slides

2012/2013

Uploaded on 04/29/2013

shamir_69
shamir_69 🇮🇳

5

(4)

66 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Parse Trees
Definitions
Relationship to Left- and
Rightmost Derivations
Ambiguity in Grammars
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Parse Trees - Automata and Complexity Theory - Lecture Slides and more Slides Theory of Automata in PDF only on Docsity!

1

Parse Trees

Definitions

Relationship to Left- and

Rightmost Derivations

Ambiguity in Grammars

2

Parse Trees

Parse trees

are trees labeled by

symbols of a particular CFG.

Leaves: labeled by a terminal or

ε

Interior nodes: labeled by a variable.

Children are labeled by the right side of aproduction for the parent.

Root: must be labeled by the start symbol.

4

Yield of a Parse Tree

The concatenation of the labels of the leaves in left-to-right order

That is, in the order of a preordertraversal.

is called the

yield

of the parse tree.

Example: yield of

is (())()

S

S

S S

)

(

(

)

(

)

5

Parse Trees, Left- andRightmost Derivations

For every parse tree, there is a uniqueleftmost, and a unique rightmostderivation.

We’ll prove:

If there is a parse tree with root labeledA and yield w, then A =>*

lm

w.

If A =>*

lm

w, then there is a parse tree

with root A and yield w.

7

Part 1 – Induction

Assume (1) for trees of height < h, and let this tree have height h:

By IH, X

i

lm

w

i

Note: if X

i

is a terminal, then

X

i

= w

i

.

Thus, A =>

lm

X

1

…X

n

lm

w

1

X

2

…X

n

lm

w

1

w

2

X

3

…X

n

lm

lm

w

1

…w

n

A

X

1

X

n

...

w

1

w

n

8

Proof: Part 2

Given a leftmost derivation of a terminal string, we need to prove theexistence of a parse tree.

The proof is an induction on the length of the derivation.

10

Part 2 – Induction

Assume (2) for derivations of fewer than k > 1 steps, and let A =>*

lm

w be

a k-step derivation.

First step is A =>

lm

X

1

…X

n

Key point: w can be divided so the first portion is derived from X

1

, the next is

derived from X

2

, and so on.

If X

i

is a terminal, then w

i

= X

i

.

11

Induction – (2)

That is, X

i

lm

w

i

for all i such that X

i

is a variable.

And the derivation takes fewer than ksteps.

By the IH, if X

i

is a variable, then there

is a parse tree with root X

i

and yield w

i

Thus, there is a parse tree

A

X

1

X

n

...

w

1

w

n

13

Parse Trees and Any

Derivation

The proof that you can obtain a parse tree from a leftmost derivation doesn’treally depend on “leftmost.”

First step still has to be A => X

1

…X

n

And w still can be divided so the first portion is derived from X

1

, the next is

derived from X

2

, and so on.

14

Ambiguous Grammars

A CFG is

ambiguous

if there is a string

in the language that is the yield of twoor more parse trees.

Example: S -> SS | (S) | ()

Two parse trees for ()()() on next slide.

16

Ambiguity, Left- and

Rightmost Derivations

If there are two different parse trees, they must produce two differentleftmost derivations by the constructiongiven in the proof.

Conversely, two different leftmost derivations produce different parsetrees by the other part of the proof.

Likewise for rightmost derivations.

17

Ambiguity, etc. – (2)

Thus, equivalent definitions of“ambiguous grammar’’ are:

There is a string in the language that hastwo different leftmost derivations.

There is a string in the language that hastwo different rightmost derivations.

19

Example: Unambiguous Grammar

B -> (RB |

ε

R -> ) | (RR

Construct a unique leftmost derivation for a given balanced string of parentheses byscanning the string from left to right.

If we need to expand B, then use B -> (RB ifthe next symbol is “(” and

ε

if at the end.

If we need to expand R, use R -> ) if the nextsymbol is “)” and (RR if it is “(”.

20

The Parsing Process

Remaining Input:(())()

Steps of leftmost

derivation:

B

Nextsymbol

B -> (RB |

ε

R -> ) | (RR