Presentation is loading. Please wait.

Presentation is loading. Please wait.

Context Free Grammars-II

Similar presentations


Presentation on theme: "Context Free Grammars-II"— Presentation transcript:

1 Context Free Grammars-II
CSC312 Automata Theory Lecture # 27 Chapter # 12 by Cohen Context Free Grammars-II

2 Example: The following CFG generate all valid variable (identifier) names in ‘C’ language according to the following rules. The variable name can only start with alphabet or underscore there is no maximum length for the variable name after first character any alphabet, digit, underscore or $ can appear. S  XY X  alpha | _ Y  alpha Y| digit Y | _Y | $Y |  alpha  A | B| C | … | Z | a | b | c | … | z digit  0 | 1 | 2 | … | 9

3 Note: Any ambiguous CFG for a regular language can be converted into un-ambiguous CFG by first finding out the language and then making DFA for that language. Then convert the DFA into CFG once again. The new CFG will be un-ambiguous as the DFA is always deterministic and un-ambiguous.

4 Trees Trees: The graphical representation of the derivation of a word is called syntax tree, parse tree, derivation tree, generation tree or production tree. Note:The syntax tree of a word should only contains leaf nodes i.e. only terminals should be at the end of the tree. Example: Consider the CFG S  AA A  AAA|bA|Ab|a The syntax tree to generate a word “babbaaa” is as follows

5 operator-operand-operand (o-o-o) prefix notation + 3 * 4 5
LUKASIEWICS Notation: (also known as Polish notation) operator-operand-operand (o-o-o) prefix notation + 3 * 4 5 operand-operand-operator (o-o-o) postfix notation * The expression * 5 is an infix arithmetic expression.

6 Total Language Tree: (TLT)
A total language tree is a tree with the start symbol S as its root and whose nodes are working strings of terminals and non-terminals. The descendents of each node are all possible results of applying every production to the working string. Examples: Practice Question: Q. 1, 2, 3, 5, 7, 8, 10, 15, 16, 17, 18, 19


Download ppt "Context Free Grammars-II"

Similar presentations


Ads by Google