Document Sample

Examples of CFG Example 1 How to generate aabb? S aSb aabb (use Rule 1) (use Rule 2) S aSb S ab How to generate aaabbb? S aSb aaSbb aaabbb (use Rule 1) (use Rule 1) (use Rule 2) CFG Notice that: – We must start with the start symbol – We can use any production any number of times. – The final string can only contain terminals. Examples of CFG Example 2 How to represent the language of balanced parentheses, i.e., {, (), (()), ()(), (()()), …..}? Consider the following CFG G2: S SS S (S) S In this example, variables are {S}, terminals are {(, )}, start symbol is S and productions are the above three rules. Examples of CFG Example 2 How to generate ()? S (S) (use Rule 2) () S (S) (SS) ((S)S) ((S)(S)) (()(S)) (()()) (use Rule 3) (use Rule 2) (use Rule 1) (use Rule 2) (use Rule 2) (use Rule 3) (use Rule 3) S SS S (S) S How to generate (()())? Examples of CFG Example 3 How can we represent all the arithmetic expressions with “plus”, “minus”, “parentheses” and variables x, y and z, i.e., {x, y, z, x+y, x-y, y+x, x+(y-z), ….. }? Consider the following CFG G3: EE+E EE-E E (E) EV Vx Vy Vz In this example, variables are {E, V}, terminals are {+, -, (, ), x, y, z}, start symbol is S and productions are the above seven rules. CFG Example 3 How to generate x+(y-z)? EE+E (use Rule 1) V+E x+E x + (E) x + (E - E) x + (V - E) x + (y - E) x + (y - V) x + (y - z) (use Rule 4) (use Rule 5) (use Rule 3) (use Rule 2) (use Rule 4) (use Rule 6) (use Rule 4) (use Rule 7) E E + E | E - E | (E) | V Vx|y|z * Note that we can write the productions in this way. Class Discussion 1. Generate the following string in L(G). a). Tim ate Cheese. b). Big jim ate green cheese. c). Big cheese ate jim. 2. Write CFG for the a). „if „ loop in “C” Language. b). For the string which do not contain three consecutive „b‟s. Derivation Consider the context free grammar G3: EE+E EE-E E (E) EV Vx Vz Vy How to derive the string x+(y-z)? Parse Tree (Derivation Tree) We can represent the derivation with a tree: E Grammar: E E + E | E - E | (E) | V Vx|y|z E + E V ( E ) x E - E We can get x+(y-z) by reading the leaves from left to right. This is called the yield of the tree. V y V z Derivation: EE+E V+E x+E x + (E) x + (E - E) x + (V - E) x + (y - E) x + (y - V) x + (y - z) Parse Tree Formally, let G = (V, T, P, S) be a CFG, a parse tree for G must be such that: • Every vertex has a label from VT {}. • The root is labeled S. • The label of any internal vertex is in V. • If a vertex is labeled , it must be a leaf and has no sibling. • If a vertex is labeled A and its children are labeled X1, X2, ..., Xk from left to right, then A X1 X2 … X3 is a production in P. Left Derivation Always derive the leftmost variable first: E E + E V ( E ) x E - E V y V z EE+E V+E x+E x + (E) x + (E - E) x + (V - E) x + (y - E) x + (y - V) x + (y - z) Right Derivation Always derive the rightmost variable first: E E + E V ( E ) x E - E V y V z EE+E E + (E) E + (E - E) E + (E - V) E + (E - z) E + (V - z) E + (y - z) V + (y - z) x + (y - z) Ambiguity • Each parse tree has one unique leftmost derivation and one unique rightmost derivation. • A grammar is ambiguous if some strings in it have more than one parse trees, i.e., it has more than one leftmost derivations (or more than one rightmost derivations). Ambiguity Consider the following grammar G: S AS | a | b A SS | ab A string generated by this grammar can have more than one parse trees. Consider the string abb: S S S A b S A S a S a b b b Ambiguity As another example, consider the following grammar: E E + E | E * E | (E) | x | y | z There are 2 leftmost derivations for x + y + z: E E + E x y + z EE+E x+E x+E+E x+y+E x+y+z EE+E E+E+E E + E x+E+E x+y+E z x + y x+y+z E Class Discussion Consider the grammar of an if-statement: <if-stat> <if-stat> <cond> <stat> if <cond> then <stat> if <cond> then <stat> else <stat> P|Q <if-stat> | R | S Consider the following if-statement: if P then if Q then R else S This grammar is ambiguous since the above statement can have two parse trees. What are they?

DOCUMENT INFO

Shared By:

Categories:

Tags:

Stats:

views: | 62 |

posted: | 11/12/2007 |

language: | English |

pages: | 16 |

OTHER DOCS BY honeytech

Docstoc is the premier online destination to start and grow small businesses. It hosts the best quality and widest selection of professional documents (over 20 million) and resources including expert videos, articles and productivity tools to make every small business better.

Search or Browse for any specific document or resource you need for your business. Or explore our curated resources for Starting a Business, Growing a Business or for Professional Development.

Feel free to Contact Us with any questions you might have.