Context-Free Grammars: Examples and Construction

Slide Note
Embed
Share

Context-free grammars (CFG) are formal grammars used to generate patterns in a given language. This content provides examples of constructing CFGs for different languages, showcasing the process with detailed explanations and visuals.


Uploaded on Aug 03, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. UNIT III Context Free Grammar and Languages Ms. Shital Pawar Assistant Professor Computer Engineering Department Bharati Vidyapeeth s College of Engineering for Women, Pune 43

  2. Formal Definition CFG stands for context-free grammar. It is a formal grammar which is used to generate all possible patterns of strings in a given formal language. Context-free grammar G can be defined by four tuples as: G = (V, T, P, S) Where, G is the grammar, which consists of a set of the production rule. It is used to generate the string of a language. T is the final set of a terminal symbol. It is denoted by lower case letters. V is the final set of a non-terminal symbol. It is denoted by capital letters. P is a set of production rules, which is used for replacing non-terminals symbols(on the left side of the production) in a string with other terminal or non-terminal symbols(on the right side of the production). S is the start symbol which is used to derive the string. We can derive the string by repeatedly replacing a non-terminal by the right-hand side of the production until all non-terminal have been replaced by terminal symbols.

  3. Example 1: Construct the CFG for the language having any number of a's over the set = {a}. Solution: As we know the regular expression for the above language is r.e. = a* Production rule for the Regular expression is as follows: S aS rule 1 S rule 2 Now if we want to derive a string "aaaaaa", we can start with start symbols. S aS aaS rule 1 aaaS rule 1 aaaaS rule 1 aaaaaS rule 1 aaaaaaS rule 1 aaaaaa rule 2 aaaaaa

  4. Example 2 Construct a CFG for the regular expression (0+1)* Solution: The CFG can be given by, Production rule (P): S 0S | 1S S e.g. 1010 1S (S 1S) 10S (S 0S) 101S (S 1S) 1010S (S 0S) 1010 (S ) The rules are in the combination of 0's and 1's with the start symbol. Since (0+1)* indicates { , 0, 1, 01, 10, 00, 11, ....}. In this set, is a string, so in the rule, we can set the rule S .

  5. Example 3 Construct a CFG for a language L = {wcwR | where w (a, b)*} Solution: The string that can be generated for a given language is {aacaa, bcb, abcba, bacab, abbcbba, ....} The grammar could be: S aSa rule 1 S bSb rule 2 S c rule 3 Now if we want to derive a string "abbcbba", we can start with start symbols. S aSa S abSba from rule 2 S abbSbba from rule 2 S abbcbba from rule 3 Thus any of this kind of string can be derived from the given production rules.

  6. Example 4 Construct a CFG for the language L = anb2nwhere n>=1. Solution: The string that can be generated for a given language is {abb, aabbbb, aaabbbbbb....}. The grammar could be: S aSbb | abb Now if we want to derive a string "aabbbb", we can start with start symbols. S aSbb S aabbbb

  7. Example 5 Construct a CFG for the language L={w {a,b}*|w is palindrome of odd length} Solution: As string is odd length palindrome middle letter can be either a or b. Smallest string for given language can be either a or b. e.g: L={a, b, aba, bab, babab, abbba, aabaa, baaab, .} S aSa S bSb S a|b It can be written as S aSa | bSb | a | b

  8. Example 6 Construct a CFG for the language L={w {a,b}*|w is palindrome of even length with |w| > 0} Solution: As string is odd length palindrome middle letter can be either a or b. Smallest string for given language can be either aa or bb. e.g: L={aa, bb, abba, baab, babbab, abbbba, aabbaa, baaaab, .} S aSa S bSb S aa|bb It can be written as S aSa | bSb | aa | bb

  9. Example 7 Construct a CFG for the language L={w {a,b}*|w is palindrome of even length or odd length with |w| > 0} Solution: As string is odd length palindrome middle letter can be either a or b. Smallest string for given language can be either a or b. e.g: L={a, b, aba, bab, babab, abbba, aabaa, baaab,aa, bb, abba, baab, babbab, abbbba, aabbaa, baaaab, .} S aSa S bSb S a|b|aa|bb It can be written as S aSa | bSb | a | b | aa | bb

  10. Example 8 Construct a CFG for the language L={aibjck | i = j + k} Solution: Outer a s should be matched with c s then the remaining a s should be matched with b s . e.g. L = {aabc, aaabcc, aaabbc, aaaabbcc, aaaabccc, ..} S aSc |aXc X aXb |ab

  11. Example 9 Construct a CFG for the language L={aibjck | j = i + k} Solution: Outer a s should be matched with c s then the remaining a s should be matched with b s . e.g. L = {abbc,aabbbc,abbbcc,aabbbbcc, } aibi bkck S XY X aXb |ab Y bYc | bc

  12. Example 10 Construct a CFG for the language (011+1)*(01)* Solution: Let us consider that (011 + 1)* is generated by variable X X 011X | 1X | Let us consider that (01)* is generated by variable Y Y 01Y | By using concatenation rule, we can combine above two production rules: So, CFG for given language is, S XY X 011X | 1X | Y 01Y |

  13. Example 11 Construct a CFG for the language L={anbm| n m} Solution: As n m number a s are different than number of b s. Let us consider an is generated by variable X X aX | a Let us consider bmis generated by variable Y Y bY | b So, CFG for given language is, S aSb | X | Y X aX | a Y bY | b

  14. Derivation Derivation is a sequence of production rules. It is used to get the input string through these production rules. During parsing, we have to take two decisions. These are as follows: We have to decide the non-terminal which is to be replaced. We have to decide the production rule by which the non- terminal will be replaced. We have two options to decide which non-terminal to be placed with production rule.

  15. Leftmost Derivation In the leftmost derivation, the input is scanned and replaced with the production rule from left to right. So in leftmost derivation, we read the input string from left to right. Example: Production rules: E = E + E E = E - E E = a | b Input a - b + a The leftmost derivation is: E = E + E E = E - E + E E = a - E + E E = a - b + E E = a - b + a

  16. Rightmost Derivation In rightmost derivation, the input is scanned and replaced with the production rule from right to left. So in rightmost derivation, we read the input string from right to left. Example Production rules: E = E + E E = E - E E = a | b Input a - b + a The rightmost derivation is: E = E - E E = E - E + E E = E - E + a E = E - b + a E = a - b + a Note: When we use the leftmost derivation or rightmost derivation, we may get the same string. This type of derivation does not affect on getting of a string.

  17. Example 1 Derive the string "abb" for leftmost derivation and rightmost derivation using a CFG given by, S AB | A aB B Sb Solution: Leftmost derivation: Rightmost derivation:

  18. Example 2 Derive the string "aabbabba" for leftmost derivation and rightmost derivation using a CFG given by, S aB | bA A a | aS | bAA B b | bS | aBB Solution: Leftmost derivation: S aB S aB aaBB aabB aabbS aabbaB aabbabS aabbabbA aabbabba B aBB B b B bS S aB B bS S bA A a

  19. Example 2 Derive the string "aabbabba" for leftmost derivation and rightmost derivation using a CFG given by, S aB | bA A a | aS | bAA B b | bS | aBB Rightmost derivation: S aB S aB aaBB B aBB aaBbS aaBbbA aaBbba aabSbba aabbAbba aabbabba B bS S bA A a B bS S bA A a

  20. Example 3 Derive the string "00101" for leftmost derivation and rightmost derivation using a CFG given by, S A1B A 0A | B 0B | 1B | Solution: Leftmost derivation: S A1B 0A1B 00A1B 001B 0010B 00101B 00101

  21. Example 3 Derive the string "00101" for leftmost derivation and rightmost derivation using a CFG given by, S A1B A 0A | B 0B | 1B | Rightmost derivation: S A1B A10B A101B A101 0A101 00A101 00101

  22. Derivation Tree Derivation tree is a graphical representation for the derivation of the given production rules for a given CFG. It is the simple way to show how the derivation can be done to obtain some string from a given set of production rules. The derivation tree is also called a parse tree. Parse tree follows the precedence of operators. The deepest sub-tree traversed first. So, the operator in the parent node has less precedence over the operator in the sub-tree.

  23. Properties of parse tree The root node is always a node indicating start symbols. The derivation is read from left to right. The leaf node is always terminal nodes. The interior nodes are always the non-terminal nodes.

  24. Example 1 Production rules: E = E + E E = E * E E = a | b | c Input a * b + c Step 1: Step 3: Step 2: Step 4:

  25. Example 2 Draw a derivation tree for the string "bab" from the CFG given by S bSb | a | b Solution: Now, the derivation tree for the string "bbabb" is as follows: The above tree is a derivation tree drawn for deriving a string bbabb. By simply reading the leaf nodes, we can obtain the desired string. The same tree can also be denoted by,

  26. Example 3 Construct a derivation tree for the string aabbabba for the CFG given by, S aB | bA A a | aS | bAA B b | bS | aBB Solution: To draw a tree, we will first try to obtain derivation for the string aabbabba Derivation Derivation Tree

  27. Ambiguity in Grammar A grammar is said to be ambiguous if there exists more than one leftmost derivation or more than one rightmost derivation or more than one parse tree for the given input string. If the grammar is not ambiguous, then it is called unambiguous. If the grammar has ambiguity, then it is not good for compiler construction. No method can automatically detect and remove the ambiguity, but we can remove ambiguity by re-writing the whole grammar without ambiguity.

  28. Example 1 Let us consider a grammar G with the production rule E I E E + E E E * E E (E) I | 0 | 1 | 2 | ... | 9 Solution: For the string "3 * 2 + 5", the above grammar can generate two parse trees by leftmost derivation: Since there are two parse trees for a single string "3 * 2 + 5", the grammar G is ambiguous.

  29. Example 2 Check whether the given grammar G is ambiguous or not. S aSb | SS S Solution: For the string "aabb" the above grammar can generate two parse trees Since there are two parse trees for a single string "aabb", the grammar G is ambiguous.

  30. Example 3 Check whether the given grammar G is ambiguous or not. A AA A (A) A a Solution: For the string "a(a)aa" the above grammar can generate two parse trees:

  31. Unambiguous Grammar A grammar can be unambiguous if the grammar does not contain ambiguity that means if it does not contain more than one leftmost derivation or more than one rightmost derivation or more than one parse tree for the given input string.

  32. To convert ambiguous grammar to unambiguous grammar, we will apply the following rules: 1. If the left associative operators (+, -, *, /) are used in the production rule, then apply left recursion in the production rule. Left recursion means that the leftmost symbol on the right side is the same as the non-terminal on the left side. For example, X Xa 2. If the right associative operates(^) is used in the production rule then apply right recursion in the production rule. Right recursion means that the rightmost symbol on the left side is the same as the non-terminal on the right side. For example, X aX

  33. Example 1 Consider a grammar G is given as follows: S AB | aaB A a | Aa B b Determine whether the grammar G is ambiguous or not. If G is ambiguous, construct an unambiguous grammar equivalent to G. Solution: Let us derive the string "aab" As there are two different parse tree for deriving the same string, the given grammar is ambiguous. Unambiguous grammar will be: S AB A Aa | a B b

  34. Example 2 Show that the given grammar is ambiguous. Also, find an equivalent unambiguous grammar. S ABA A aA | B bB | Solution: The given grammar is ambiguous because we can derive two different parse tree for string aa. The unambiguous grammar is: S aXY | bYZ | Z aZ | a X aXY | a | Y bYZ | b |

  35. Example 3 Show that the given grammar is ambiguous. Also, find an equivalent unambiguous grammar. E E + E E E * E E id Solution: Let us derive the string "id + id * id As there are two different parse tree for deriving the same string, the given grammar is ambiguous. Unambiguous grammar will be: E E + T E T T T * F T F F id

  36. Example 4 Check that the given grammar is ambiguous or not. Also, find an equivalent unambiguous grammar. S S + S S S * S S S ^ S S a Solution: The given grammar is ambiguous because the derivation of string aab can be represented by the following string: Unambiguous grammar will be: S S + A | A A * B | B B C ^ B | C C a

  37. Simplification of CFG As we have seen, various languages can efficiently be represented by a context-free grammar. All the grammar are not always optimized that means the grammar may consist of some extra symbols(non-terminal). Having extra symbols, unnecessary increase the length of grammar. Simplification of grammar means reduction of grammar by removing useless symbols. The properties of reduced grammar are given below: Each variable (i.e. non-terminal) and each terminal of G appears in the derivation of some word in L. There should not be any production as X Y where X and Y are non-terminal. If is not in the language L then there need not to be the production X .

  38. Let us study the reduction process in detail. Non Non Reachable Symbol generating Symbol

  39. Removal of Useless Symbols A symbol can be useless if it does not appear on the right- hand side of the production rule and does not take part in the derivation of any string. That symbol is known as a useless symbol. Similarly, a variable can be useless if it does not take part in the derivation of any string. That variable is known as a useless variable.

  40. Example 1 Remove the useless symbols from given grammar T aaB | abA | aaT A aA B ab | b C ad In the above example, the variable 'C' will never occur in the derivation of any string, so the production C ad is useless. So we will eliminate it, and the other productions are written in such a way that variable C can never reach from the starting variable 'T'. Production A aA is also useless because there is no way to terminate it. If it never terminates, then it can never produce a string. Hence this production can never take part in any derivation. To remove this useless production A aA, we will first find all the variables which will never lead to a terminal string such as variable 'A'. Then we will remove all the productions in which the variable A' occurs. Simplified Grammar is: T aaB | aaT B ab | b

  41. Elimination of Production The productions of type S are called productions. These type of productions can only be removed from those grammars that do not generate . Step 1: First find out all null able non-terminal variable which derives . Step 2: For each production A a, construct all production A x, where x is obtained from a by removing one or more non-terminal from step 1. Step 3: Now combine the result of step 2 with the original production and remove productions.

  42. Example 2 Remove the production from the following CFG by preserving the meaning of it. S XYX X 0X | Y 1Y | Solution: Now, while removing production, we are deleting the rule X and Y . To preserve the meaning of CFG we are actually placing at the right-hand side whenever X and Y have appeared. Let us take S XYX If the first X at right-hand side is . Then S YX if the last X in R.H.S. = . Then S XY If Y = then S XX If Y and X are then, S X

  43. Example 3 Remove the production from the following CFG by preserving the meaning of it. S XYX X 0X | Y 1Y | Solution: If both X are replaced by S Y Now, S XY | YX | XX | X | Y Now let us consider X 0X If we place at right-hand side for X then, X 0 X 0X | 0 Similarly Y 1Y | 1 Collectively we can rewrite the CFG with removed production as S XY | YX | XX | X | Y X 0X | 0 Y 1Y | 1

  44. Removing Unit Productions The unit productions are the productions in which one non-terminal gives another non-terminal. Use the following steps to remove unit production: Step 1: To remove X Y, add production X a to the grammar rule whenever Y a occurs in the grammar. Step 2: Now delete X Y from the grammar. Step 3: Repeat step 1 and step 2 until all unit productions are removed.

  45. Example 4 construct reduced grammar equivalent to the grammar S 0A | 1B | C A 0S | 00 B 1 | A C 01 Solution: S C is a unit production. But while removing S C we have to consider what C gives. So, we can add a rule to S. S 0A | 1B | 01 Similarly, B A is also a unit production so we can modify it as B 1 | 0S | 00 Thus finally we can write CFG without unit production as S 0A | 1B | 01 A 0S | 00 B 1 | 0S | 00 C 01 As C is non-reachable symbol, we have to remove C. so simplified grammar after removing C is: S 0A | 1B | 01 A 0S | 00 B 1 | 0S | 00

  46. Example 5 construct reduced grammar equivalent to the grammar S aAa A Sb | bcc | DaA C abb | DD D aDA E ac Finding non - generating symbols: D is non generating symbol So, reduced grammar is after elimination of non generating symbol D is: S aAa A Sb | bcc C abb E ac Finding Non- reachable symbols: C and E are non- generating symbols: So, reduced grammar after elimination of non -reachable symbols A and E is S aAa A Sb | bcc As there are no unit productions and productions in given grammar so reduced grammar is : S aAa A Sb | bcc

  47. Example 6 Simplify the following grammar: S 0A0 | 1B1 | BB A C B S| A C S| Solution: Elimination of production: S 0A0 | 1B1 | BB | 00 | 11 | B A C B S| A C S Eliminate Unit Productions: As B A C S B S 0A0 | 1B1 | BB | 00 | 11 A 0A0 | 1B1 | BB | 00 | 11 B 0A0 | 1B1 | BB | 00 | 11 C 0A0 | 1B1 | BB | 00 | 11 Elimination of non reachable symbol C: S 0A0 | 1B1 | BB | 00 | 11 A 0A0 | 1B1 | BB | 00 | 11 B 0A0 | 1B1 | BB | 00 | 11

  48. Example 7 Simplify the following grammar: S ASB | A aAS | a B SbS | A | bb Solution: Eliminate productions: S ASB | AB A aAS| aA | a B SbS | Sb | bS | b | A | bb Eliminate Unit Production: Eliminate unit production A form production rule B S ASB | AB A AaS | Aa | a B SbS | Sb | bS | b | aAS | aA | bb |a As there are no useless symbols , so simplified grammar is: S ASB | AB A AaS | Aa | a B SbS | Sb | bS | b | aAS | aA | bb |a

  49. Example 8 Simplify the following grammar: S aC| SB A bSCa B aSB|bBC C aBC|ad Solution: Elimination of non generation symbols: symbol B is non generating symbol S aC A bSCa C ad Elimination of non reachable symbols: Symbol A is non reachable symbol S aC C ad As there is no production and there is no unit production in given grammar the reduced grammar is: S aC C ad

  50. Example 9 Simplify the following grammar S ABA | BA | AA | AB | A | B A Aa | a B bB | b Solution: Eliminate unit production: A and B are unit productions. S ABA | BA | AA | AB | Aa | a | bB | b A Aa | a B bB | b As there is no production and useless symbol in this grammar. So simplified grammar is: S ABA | BA | AA | AB | Aa | a | bB | b A Aa | a B bB | b

Related


More Related Content