Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classification of grammars Definition: 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.

Similar presentations


Presentation on theme: "Classification of grammars Definition: 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."— Presentation transcript:

1 Classification of grammars Definition: 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 (V  T) *. 3)Context-sensitive if each production is of the form , where |  |  |  |. 4)A grammar with no restrictions as above is called unrestricted.

2 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

3 Examples: 1) A   A  0 A  1 A  0 A 0 A  1 A 1 2) L = {0 n 1 n | n  0}

4 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).

5 Example: E  E + T | T T  T * F | F F  (E) | a

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 X 1, X 2, …, X k, then A → X 1 X 2 …X k is a production Leaf nodes are nonterminals, terminals, or 

7 Example: S → SS | (S) | 

8 Recursive inference: S  w 0 X 1 w 1 X 2 w 2 … X k w k w i in T * and X j in V Then w generated by S is the concatenation w i and strings generated by X j in the order

9 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  * w 3.There is a parse tree with root A and yield w.

10 Leftmost derivation Rightmost derivation Parse tree Recursive inference Derivation

11 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.

12 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.

13 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.

14 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

15 Example: (1)E  E + E | E * E | (E) | a is ambiguous (2)L = {a i b j c k | i = j or j = k} is inherently ambiguous


Download ppt "Classification of grammars Definition: 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."

Similar presentations


Ads by Google