Task Construct CFG that generates the language L = {w  {a,b}*: length(w)  2 and second letter of w from right is a}

Slides:



Advertisements
Similar presentations
Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
Advertisements

Regular Expressions, Backus-Naur Form and Reverse Polish Notation.
Syntax Directed Definitions Synthesized Attributes
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Syntax Analysis The recognition problem: given a grammar G and a string w, is w  L(G)? The parsing problem: if G is a grammar and w  L(G), how can w.
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.
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
Chapter 5 Context-Free Grammars
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
Lecture # 5 Pumping Lemma & Grammar
Context Free Grammars.
Lecture 11 Theory of AUTOMATA
Re-enter Chomsky More about grammars. 2 Parse trees S  A B A  aA | a B  bB | b Consider L = { a m b n | m, n > 0 } (one/more a ’s followed by one/more.
CHP-3 STACKS.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Exercises on Chomsky Normal Form and CYK parsing
Chomsky Normal Form.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Recap lecture 31 Context Free Grammar, Terminals, non- terminals, productions, CFG, context Free language, examples.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Lecture # 21.
Regular Expressions, Backus-Naur Form and Reverse Polish Notation
CONTEXT-FREE LANGUAGES
Recap lecture 35 Regular grammar, null productions and examples, nullable productions and examples, unit productions and example, Chomsky Normal Form (Definition)
Context-Free Grammars: an overview
CS510 Compiler Lecture 4.
Chapter 3 Context-Free Grammar and Parsing
Recap lecture 33 Example of trees, Polish Notation, examples, Ambiguous CFG, example,
Ambiguity Parsing algorithms
Syntax Specification and Analysis
CSC312 Automata Theory Grammatical Format Chapter # 13 by Cohen
Recap Lecture 34 Example of Ambiguous Grammar, Example of Unambiguous Grammer (PALINDROME), Total Language tree with examples (Finite and infinite trees),
Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.
Theory Of Automata By Dr. MM Alam
Compiler Construction
Recap lecture 38 Example of PDA with table for running a string, Equivalent PDA, PDA for EVEN EVEN Language. Non-Derterministic PDA, Example of Non-Derterministic.
Compiler Construction (CS-636)
Stack application: postponing data usage
PART II STACK APPLICATIONS
Jaya Krishna, M.Tech, Assistant Professor
Context-Free Languages
Compiler Design 4. Language Grammars
Programming Language Syntax 2
Stdnt id: roll no..
Chapter 2: A Simple One Pass Compiler
Thinking about grammars
Decidablity Following are the decidable problems w.r.t. CFG
CHAPTER 2 Context-Free Languages
Recap lecture 6 Language of strings, beginning with and ending in different letters, Accepting all strings, accepting non-empty strings, accepting no string,
Section 9.3 by Andrew Watkins
Grammar design: Associativity
Recap Lecture 16 Examples of Kleene’s theorem part III (method 3), NFA, examples, avoiding loop using NFA, example, converting FA to NFA, examples, applying.
ReCap Chomsky Normal Form, Theorem regarding CNF, examples of converting CFG to be in CNF, Example of an FA corresponding to Regular CFG, Left most and.
Theory of Computation Lecture #
Teori Bahasa dan Automata Lecture 9: Contex-Free Grammars
Recap lecture 42 Row language, nonterminals defined from summary table, productions defined by rows, rules for defining productions, all possible productions.
Recap lecture 37 New format for FAs, input TAPE, START, ACCEPT , REJECT, READ states Examples of New Format of FAs, PUSHDOWN STACK , PUSH and POP states,
Recap Lecture 17 converting NFA to FA (method 3), example, NFA and Kleene’s theorem method 1, examples, NFA and Kleene’s theorem method 2 , NFA corresponding.
Recap lecture 25 Intersection of two regular languages is regular, examples, non regular languages, example.
RECAP Lecture 7 FA of EVEN EVEN, FA corresponding to finite languages(using both methods), Transition graphs.
Recap lecture 20 Recap Theorem, Example, Finite Automaton with output, Moore machine, Examples.
Recap lecture 30 Deciding whether two languages are equivalent or not, example, deciding whether an FA accept any string or not, method 3, examples, finiteness.
Recap lecture 40 Recap of example of PDA corresponding to CFG, CFG corresponding to PDA. Theorem, HERE state, Definition of Conversion form, different.
Finite Automaton with output
PDA corresponding to CFG
Recap lecture 41 Recap of PDA in conversion form, example of PDA in conversion form, joints of the machine, new pictorial representation of PDA in conversion.
Thinking about grammars
Context Free Grammars-II
Presentation transcript:

Task Construct CFG that generates the language L = {w  {a,b}*: length(w)  2 and second letter of w from right is a}

Solution of the task Construct CFG that generates the language L = {w  {a,b}*: length(w)  2 and second letter of w from right is a} It can be observed that the language L can be expressed by (a+b)*(aa+ab). Here the nonterminal S should be replaced the nonterminals X or Y where X should generate the strings corresponding to (a+b)* and Y should generate the strings corresponding to (aa+ab). Thus the required CFG may be (1) S  XY (2) X  aX|bX| (3) Y  aa|ab

Task Construct the CFG for the language of strings, defined over ={a,b}, beginning and ending in same letters. Solution: It may be noted that the above language contains the strings a and b as well. So while constructing the required CFG the strings a and b must be generated. Thus the required CFG may be S  aXa|bXb|a|b X  aX|bX|

Example Consider the following CFG S  S+S|S*S|number where S and number are non-terminals and the operators behave like terminals. The above CFG creates ambiguity as the expression 3+4*5 has two possibilities (3+4)*5=35 and 3+(4*5)=23 which can be expressed by the following production trees

Example continued … S 3 4 + * 5 (i) S 5 * (ii) 3 + 4

Example continued … The expressions can be calculated starting from bottom to the top, replacing each nonterminal by the result of calculation e.g. S 3 4 5 + * (i)  20 23

Example continued … Similarly The ambiguity that has been observed in this example can be removed with a change in the CFG as discussed in the following example S 5 * (ii)   7 35 3 4 +

Example S  (S+S)|(S*S)|number where S and number are nonterminals, while (, *, +, ) and the numbers are terminals. Here it can be observed that S  (S+S)  (S+(S*S))  (3+(4*5)) = 23 S  (S*S)  ((S+S)*S)  ((3+4)*5) = 35

Polish Notation (o-o-o) There is another notation for arithmetic expressions for the CFG SS+S|S*S|number. Consider the following derivation trees S 3 4 + * 5 (i) S 5 * (ii) 3 + 4

Polish Notation (o-o-o) The arithmetic expressions shown by the trees (i) and (ii) can be calculated from the following trees, respectively Here most of the S’s are eliminated. 5 S 3 4 + * (i) S 5 * (ii) 3 + 4

Polish notation continued … The branches are connected directly with the operators. Moreover, the operators + and * are no longer terminals as these are to be replaced by numbers (results). To write the arithmetic expression, it is required to traverse from the left side of S and going onward around the tree. The arithmetic expressions will be as under (i) + 3 * 4 5 (ii) * +3 4 5 The above notation is called operator prefix notation.

Polish notation continued … To evaluate the strings of characters, the first substring (from the left) of the form operator-operand-operand (o-o-o) is found and is replaced by its calculation e.g. (i) +3*4 5 = +3 20 = 23 (ii) *+3 4 5 = * 7 5 = 35 It may be noted that 4*5+3 is an infix arithmetic expression, while an arithmetic in (o-o-o) form is a prefix arithmetic expression. Consider another example as follows

Example To calculate the arithmetic expression of the following tree S * + 5 6 1 2 3 4

Example continued … it can be written as *+*+1 2+3 4 5 6 The above arithmetic expression in (o-o-o) form can be calculated as *+*+1 2+3 4 5 6 = *+*3+3 4 5 6 = *+*3 7 5 6 = *+21 5 6 = *26 6 = 156. Following is a note

Note *+*+1 2+3 4 5 6 = *+*+1 2 (3+4) 5 6 = *+*(1+2) (3+4) 5 6 The previous prefix arithmetic expression can be converted into the following infix arithmetic expression as *+*+1 2+3 4 5 6 = *+*+1 2 (3+4) 5 6 = *+*(1+2) (3+4) 5 6 = *(((1+2)*(3+4)) + 5) 6 = (((1+2)*(3+4)) + 5)*6

Task Convert the following infix expressions into the corresponding prefix expressions. Calculate the values of the expressions as well 2*(3+4)*5 ((4+5)*6)+4

Ambiguous CFG The CFG is said to be ambiguous if there exists atleast one word of it’s language that can be generated by the different production trees. Example: Consider the following CFG SaS|Sa|a The word aaa can be generated by the following three different trees

Example continued … S a S a S a Thus the above CFG is ambiguous, while the CFG SaS|a is not ambiguous as neither the word aaa nor any other word can be derived from more than one production trees. The derivation tree for aaa is as follows