6

15

Click here to load reader

description

gnnn

Transcript of 6

Page 1: 6

Classification of grammarsDefinition: A grammar G is said to be 1) Right-linear if each production in P is

of the form A xB or A x where A and B are in V and x in T*.

2) Context-free if each production is of the form A , where A is in V and is in (VT)*.

3) Context-sensitive if each production is of the form , where || ||.

4) A grammar with no restrictions as above is called unrestricted.

Page 2: 6

Context-Free Grammars

•Language generated by a CFG

•Derivation sequence

•Leftmost derivation sequence

•Rightmost derivation sequence

•Parse tree (Derivation tree)

•Yield of a parse tree

•Recursive inference

Page 3: 6

Examples:1) A

A 0

A 1

A 0 A 0

A 1 A 1

2) L = {0n1n | n 0}

Page 4: 6

Leftmost derivation – always replace the the leftmost variable (nonterminal) by a production body (right side of the production).

Rightmost derivation – always replace the the rightmost variable (nonterminal) by a production body (right side of the production).

*

lmlm

*

rmrm

Page 5: 6

Example:

E E + T | T

T T * F | F

F (E) | a

Page 6: 6

Parse tress – tree representation for derivations

• Each interior node is labeled by a nonterminal

• If an interior node is labeled by A and its children (from left to right) are X1, X2, …, Xk, then A → X1X2…Xk is a production

• Leaf nodes are nonterminals, terminals, or

Page 7: 6

Example:

S → SS | (S) |

Page 8: 6

Recursive inference:

S w0X1w1X2w2 … Xkwk

wi in T* and Xj in V

Then w generated by S is the concatenation

wi and strings generated by Xj in the order

Page 9: 6

Inference, Derivations, and Parse Trees

Given a grammar G = (V, T, P, S), the following are equivalent:

1. The recursive inference procedure determines that a terminal string w is in the language of variable A.

2. A * w3. There is a parse tree with root A and yield

w.

Page 10: 6

Leftmost derivation

Rightmost derivation

Parse tree

Recursive inferenceDerivation

Page 11: 6

Theorem 5.12 (From Inference to Tree)

Let G = (V, T, P, S) be a CFG. If the recursive inference procedure tells us that the string w is in the language of a nonterminal A, then there is a parse tree with root A and yield w.

Page 12: 6

Theorem 5.14 (Tree to leftmost derivation)

Let G = (V, T, P, S) be a CFG and thre be a parse tree with root labeled A and yield w where w is in T*. Then thre is a leftmost derivation sequence A * w in G.

Page 13: 6

Theorem 5.18 (Derivation to inference)

Let G = (V, T, P, S) be a CFG and suppose there is a derivation A * w, where w is in T*. Then the recursive inference procedure applied to G determines that w is in the language of A.

Page 14: 6

Ambiguous Grammars: A CFG G is ambiguous if there is at least one sentence w in L(G) for which there is more than one distinct derivation tree with frontier w.

Inherent Ambiguity: A CFL L is said to be inherently ambiguous if all its grammars are ambiguous

Page 15: 6

Example:

(1) E E + E | E * E | (E) | a

is ambiguous

(2) L = {aibjck | i = j or j = k}

is inherently ambiguous