Presentation is loading. Please wait.

Presentation is loading. Please wait.

CONTEXT FREE GRAMMAR presented by Mahender reddy.

Similar presentations


Presentation on theme: "CONTEXT FREE GRAMMAR presented by Mahender reddy."— Presentation transcript:

1 CONTEXT FREE GRAMMAR presented by Mahender reddy

2 What is Context Free Grammar? Why we are using Context Free Grammar? Applications of Context free Grammar.

3 Definition of CFG: A Context free grammar is 4-tuple G=(V, T, P, S) where, V= set of variables or non-terminals. T= set of terminals. P= set of productions where each production is of the form V->  1 |  2 | …. where  i is an arbitrary string of variables and terminals. S= Start variable

4 String membership: How to say a string belong to the language generated by CFG? 1.Derivation: Head to Body. 2.Recursive inference: Both are equivalent forms. Body to Head.

5 Example of CFG: How CFG works for Palindrome G: S-> 0A0|1A1|0|1|  Consider a string w=001100 G can generate w as follows S->0A0 ->00A00 ->001A100 ->001100

6 Example for even no. of 0’s: A CFG for all binary strings with an even number of 0’s. Find the decomposition. If first symbol is 1, then even number of 0’s remain. If first symbol is 0, then go to next 0; after that again an even number of 0’s remain. This yields: S → 1 S | 0 A 0 S | ε A → 1 A | ε Here is another CFG for the same language. Note that when first symbol is 0, what remains has odd number of 0’s. S → 1 S | 0 T | ε T → 1 T | 0 S

7 More examples of CFG: L 1 = {0 n | n≥0 } L 2 = {0 n | n≥1 } L 3 ={0 i 1 j 2 k | i=j or j=k, where i,j,k≥0} L 4 ={0 i 1 j 2 k | i=j or i=k, where i,j,k≥1}

8 APPLICATIONS OF CONTEXT FREE GRAMMAR

9 1.CFG for Parsers: The component of compiler discovers the structure of source program and represents that structure by a parse tree. Programming languages use parentheses or brackets in a nested and balanced fashion. Example of balanced parentheses. A-> AA|(A)|  Strings of balanced parentheses are (), (( )),(() ())….. Beginnings and endings of code blocks, such as begin and end in Pascal, or the curly braces {…..} of C.

10 Example : A grammar that generates the possible sequences of if and else in C (represented as i and e, respectively) is: S -> SS | iS | iSeS |  Q: Can we generate the following strings using the above grammar, And how?: ieie, iie, iei, ? Some illegal strings are ei,ieeii. What about: iieie ? The answer is yes.

11 Consider an example of C program: if(condition) { ….. if (condition) statement; else statement; if (condition) statement; else statement; ……. } The two else’s match their previous if’s and the first if is unmatched.

12 Consider the following grammar: → if then | if then else → | We can prove it as an Ambiguous Grammar: → if then if then else Two distinct parse trees can be constructed from the same sentential form.

13 First Parse tree if then else if then

14 if then if then else Second parse tree

15 Unambiguous grammar for the above language is: -> | -> if then else | any non-if statement -> if then | if then else Example CFG for “while” and “for” loop. Expr -> for( Expr; Expr; Expr) loop Expr pool | while(Expr) loop Expr pool | id

16 2.CFG for Algebraic expression

17

18 Consider the following grammar for algebraic expression S -> x | y | z S -> S+S |S-S |S*S |S/S S -> (S) Generate the following string using the above grammar ( x + y ) * x - z * y / ( x + x ) It is as follows.

19 S (Start symbol) -> S - S -> S * S - S -> S * S - S / S -> ( S ) * S – S / S -> ( S ) * S – S / ( S ) -> ( S + S ) * S – S / ( S ) -> ( S + S ) * S – S * S /( S ) -> ( S + S ) * S – S * S / ( S + S ) -> ( x + S ) * S – S * S/ ( S+ S ) -> ( x +y ) *S – S *S / ( S + S ) -> ( x +y ) * x- S * y / ( S +S ) -> ( x +y ) *x –z * y / ( S +S ) -> ( x +y )* x –z * y / ( x + S) -> (x + y ) * x- z * y / ( x + x )

20 Picking a different order of expansion will produce a different derivation, but the same parse tree. The parse tree will only change if we pick a different rule to apply at some position in the tree. Can different parse tree will generate same terminal string? Yes, for this particular grammar. Such grammars are called ambiguous grammar. Take an example of x + y * z for the above grammar. And construct a parse tree by applying different rules than two different parse trees can be constructed as follows which is ambiguous.

21 Two different Parse trees for x + y * z: S + S S x S * S yz S S * S S +S xy z However, the language described above grammar is not inherently ambiguous, an alternate unambiguous grammar can be developed.

22 S -> S + T | S - T | S * T | S / T S -> T T -> x | y | z T -> ( S ) The alternative grammar will produce parse tree that is similar to the right one of the previous tree i.e; Implicitly assuming ( x + y ) * z which is not according to standard operator precedence. More elaborate unambiguous and context free grammar can be constructed that produce parse trees that obeys all desired operator precedence rules.

23 23 Syntactic expression in programming languages result = a*b + score + 10 * distance + c Regular languages have only terminals but context free grammar has both terminals and non terminals. terminals variables Operators are also terminals

24 3.Markup languages Tags tell us something about the semantics of various strings within the document. HTML has two major functions i.e; creating links between documents and describing the format of a document. Example people who drive too slow HTML source in the fast lane. The text can be viewed as : People who drive 1. too slow 2. in the fast lane.

25 HTML GRAMMAR Char  a | A | … Text  ε | Char Text Doc  ε | Element Doc Element  Text | Doc | Doc | List ListItem  Doc List  ε | ListItem List

26 HTML is a fixed standard i.e; HTML has predefined tags. XML lets everyone define the data structures they need i.e; XML has user defined tags.

27 XML and DTD XML describes “semantics” of the text but not the format of the document. To make clear what the different kinds of tags are, and what structures may appear between matching pair of these tags. Example: 39 maple St A DTD is essentially CFG, with its own notation for describing the variables and productions The form of DTD is <!DOCTYPE name-of-DTD [ list of element definitions ]> An element definition has the form

28 Example: <!DOCTYPE PcSpecifications [ ]>

29 Is similar to the production PROCESSOR -> MANF MODEL SPEED In the similar way Disk -> HD|CD PC -> Model Price Processor Disks Disks -> Disk | Disk Disks

30 CFG is used where there is a general need of: Matching a symbol with another symbol, or Matching a count of one symbol with that of another symbol, or Recursively substituting one symbol with a string of other symbols

31 References: http://en.wikipedia.org/wiki/Context-free_grammar http://www.epaperpress.com Inroduction to Automata theory,languages and computation, second edition by JOHN E.HOPCROFT,RAJEEV MOTWANI,JEFFREY D.ULLMAN


Download ppt "CONTEXT FREE GRAMMAR presented by Mahender reddy."

Similar presentations


Ads by Google