Top Banner
Lecture # 8 Chapter # 4: Syntax Analysis
23

Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Dec 14, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Lecture # 8

Chapter # 4: Syntax Analysis

Page 2: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Practice Context Free Grammars

a) CFG generating alternating sequence of 0’s and 1’s

b) CFG in which no consecutive b’s can occur but consecutive a’s can occur

c) CFG for the following language: L(G)= {an b2n | n>=0}

Page 3: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Practice Answers

a) S 0A | 1B c) S aSbb | є A 1B | 0 B 0A | 1 b) S aS | bT |a |b T aS | a

Page 4: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Example

• Design a CFG for the language L(G)= {0n 1m | n <> m}There are two cases:– For n>m– For n<m– Write two separate set of rules and combine them

Page 5: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Example

• For n>m S1 AB B0A1 | Combining both: A0A | 0 S S1 | S2

For n<m S2 XY X0X1 | Y1Y | 1

Є

Є

Page 6: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Practice CFG

• Design a CFG for the language L(G)= {0i 1j 2k| i=j or j=k}

There are two cases:– For i=j– For j=k– Write two separate set of rules and combine them

Page 7: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Solution of Practice

• For i=j S1 AB A0A1 | ε Combining both: B2B | ε S S1 | S2

For j=k S2 XY X0X | ε Y1Y2 | ε

Page 8: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Derivations

• The one-step derivation is defined by A

where A is a production in the grammar• In addition, we define– is leftmost lm if does not contain a nonterminal– is rightmost rm if does not contain a nonterminal– Transitive closure * (zero or more steps)– Positive closure + (one or more steps)

• The language generated by G is defined byL(G) = {w T* | S + w}

Page 9: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

9

Derivation (Example)

Grammar G = ({E}, {+,*,(,),-,id}, P, E) withproductions P = E E + E

E E * EE ( E )E - EE id

E - E - id

E * E

E + id * id + id

E rm E + E rm E + id rm id + id

Example derivations:

E * id + id

Page 10: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Example

• For the given grammar derive the string 9-5+2 using Left most derivation Then derive the same string using Right most

derivation

Page 11: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

11

Example Grammar

list list + digit

list list - digit

list digit

digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

G = <{list,digit}, {+,-,0,1,2,3,4,5,6,7,8,9}, P, list>

with productions P =

Context-free grammar for simple expressions:

Page 12: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

12

Derivation for the Example Grammar

list list + digit list - digit + digit digit - digit + digit 9 - digit + digit 9 - 5 + digit 9 - 5 + 2

This is an example leftmost derivation, because we replacedthe leftmost nonterminal (underlined) in each step.

Likewise, a rightmost derivation replaces the rightmostnonterminal in each step

Page 13: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

13

Chomsky Hierarchy: Language Classification

• A grammar G is said to be– Regular if it is right linear where each production is of the

formA w B or A w

or left linear where each production is of the formA B w or A w

– Context free if each production is of the formA

where A N and (NT)*– Context sensitive if each production is of the form

A where A N, ,, (NT)*, || > 0

– Unrestricted

Page 14: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

14

Chomsky Hierarchy

L(regular) L(context free) L(context sensitive) L(unrestricted)

Where L(T) = { L(G) | G is of type T }That is: the set of all languages

generated by grammars G of type T

L1 = { anbn | n 1 } is context free

L2 = { anbncn | n 1 } is context sensitive

Every finite language is regular! (construct a FSA for strings in L(G))

Examples:

Page 15: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

15

Parse Trees

• The root of the tree is labeled by the start symbol• Each leaf of the tree is labeled by a terminal (=token)

or • Each interior node is labeled by a nonterminal• If A X1 X2 … Xn is a production, then node A has

immediate children X1, X2, …, Xn where Xi is a (non)terminal or ( denotes the empty string)

Page 16: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

16

Parse Tree for the Example Grammar

Parse tree of the string 9-5+2 using grammar G

list

digit

9 - 5 + 2

list

list digit

digitThe sequence ofleafs is called the

yield of the parse tree

Page 17: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Example of Parse Tree

• Suppose we have the following grammar E → E + E E → E * E E → ( E ) E → - E E → id

Perform Left most derivation, right most derivation and construct a parse tree for the string

id+id*id

Page 18: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Two possible Parse Trees using Leftmost derivation

Page 19: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Parse Tree via Right most derivation

Page 20: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Ambiguity

• Grammar is ambiguous if more than one parse tree is possible for some string as shown in the previous example. If there are more than one left most derivations or more than one right most derivations.

• Ambiguity is not acceptable– Unfortunately, it’s undecidable to check whether a given

CFG is ambiguous– Some CFLs are inherently ambiguous (do not have an

unambiguous CFG)

Page 21: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

21

Ambiguity (cont’d)

string string + string | string - string | 0 | 1 | … | 9

G = <{string}, {+,-,0,1,2,3,4,5,6,7,8,9}, P, string>

with production P =

Consider the following context-free grammar:

This grammar is ambiguous, because more than one parse treerepresents the string 9-5+2

Page 22: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

22

Two Parse Trees for the same string

string

string

9 - 5 + 2

string

string string

string

string

9 - 5 + 2

string

string string

Page 23: Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.

Practice

• Show that the following grammar is ambiguous: (Find out strings and two parse trees)

1) S AB | aaB 2) S a | abSb |aAb A a | Aa A bS | aAAb Bb

3) S aSb | SS | ε