Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.

Slides:



Advertisements
Similar presentations
Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)
Advertisements

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.
Regular Expressions, Backus-Naur Form and Reverse Polish Notation.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Chapter 4 Normal Forms for CFGs Chomsky Normal Form n Defn A CFG G = (V, , P, S) is in chomsky normal form if each rule in G has one of.
CS5371 Theory of Computation
Transparency No. P2C4-1 Formal Language and Automata Theory Part II Chapter 4 Parse Trees and Parsing.
Chapter 3: Formal Translation Models
Specifying Languages CS 480/680 – Comparative Languages.
COP4020 Programming Languages
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
Problem of the DAY Create a regular context-free grammar that generates L= {w  {a,b}* : the number of a’s in w is not divisible by 3} Hint: start by designing.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
CMSC 330: Organization of Programming Languages
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
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.
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
Grammars CPSC 5135.
PART I: overview material
Lecture # 5 Pumping Lemma & Grammar
Context Free Grammars.
CMSC 330: Organization of Programming Languages
Lecture 11 Theory of AUTOMATA
Prefix, Postfix and Infix. Infix notation  A-B/(C+D)  evaluate C+D (call the result X),  then B/X (call the result Y),  and finally A-Y.  The order.
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.
1 Chapter 6 Simplification of CFGs and Normal Forms.
CHP-3 STACKS.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
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.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
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.
Lecture 17: Theory of Automata:2014 Context Free Grammars.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Regular Expressions, Backus-Naur Form and Reverse Polish Notation
CONTEXT-FREE LANGUAGES
Context-free grammars
Context-Free Grammars: an overview
Formal Language & Automata Theory
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Recap lecture 33 Example of trees, Polish Notation, examples, Ambiguous CFG, example,
Syntax Specification and Analysis
Theorem 29 Given any PDA, there is another PDA that accepts exactly the same language with the additional property that whenever a path leads to ACCEPT,
Recap Lecture 34 Example of Ambiguous Grammar, Example of Unambiguous Grammer (PALINDROME), Total Language tree with examples (Finite and infinite trees),
Context-Free Grammars
Theory Of Automata By Dr. MM Alam
Compiler Construction
PART II STACK APPLICATIONS
Context-Free Grammars
Decidablity Following are the decidable problems w.r.t. CFG
CHAPTER 2 Context-Free Languages
Finite Automata and Formal Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Theory of Computation Lecture #
Teori Bahasa dan Automata Lecture 9: Contex-Free Grammars
(Part 2) Infix, Prefix & Postfix
Task Construct CFG that generates the language L = {w  {a,b}*: length(w)  2 and second letter of w from right is a}
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.
Context Free Grammars-II
Answer Questions about Exam2 problems
COMPILER CONSTRUCTION
Faculty of Computer Science and Information System
Presentation transcript:

Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s

Σ = {a,b} Productions: S → SS S → XS S → Λ S → YSY X → aa X → bb Y → ab Y → ba

Remarks We have seen that some regular languages can be generated by CFGs, and some non-regular languages can also be generated by CFGs. In Chapter 13, we will show that ALL regular languages can be generated by CFGs. In Chapter 16, we will see that there is some non-regular language that cannot be generated by any CFG. Thus, the set of languages generated by CFGs is properly larger than the set of regular languages, but properly smaller than the set of all possible languages.

Trees Consider the following CFG: S → AA A → AAA|bA|Ab|a The derivation of the word bbaaaab is as follows: S  AA  bAAAA  bbAaaAb  bbaaaab

We can use a tree diagram to show that derivation process: We start with the symbol S. Every time we use a production to replace a nonterminal by a string, we draw downward lines from the nonterminal to EACH character in the string. Reading from left to right produces the word bbaaaab. Tree diagrams are also called syntax trees, parse trees, generation trees, production trees, or derivation trees.

Example Consider the following CFG for a simplified version of arithmetic expressions: S  S + S | S * S | number where the only nonterminal is S, and the terminals are number together with the symbols +,* . Obviously, the expression 3 + 4 * 5 is a word in the language defined by this CFG; however, it is ambiguous since it is not clear whether it means (3 + 4) * 5 (which is 35), or 3 + (4 * 5) (which is 23). To avoid ambiguity, we often need to use parentheses, or adopt the convention of “hierarchy of operators” (i.e., * is to be executed before +). We now present a new notation that is unambiguous but does not rely on operator hierarchy or on the use of parentheses.

Example Which can be expressed by the following production trees

Example The ambiguity that has been observed in the previous example can be removed with a change in the CFG as discussed in the following 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

New Notation: Lukasiewicz notation We can now construct a new notation for arithmetic expressions: – We walk around the tree and write down symbols, once each, as we encounter them. – We begin on the left side of the start symbol S and head south. – As we walk around the tree, we always keep our left hand on the tree.

Using the algorithm above, the first derivation tree is converted into the notation: + 3 * 4 5. The second derivation tree is converted into * + 3 4 5.

To evaluate a string in this new notation, we proceed as follows: The above strings are said to be in operator prefix notation because the operator is written in front of the operands it combines. We will show that this notation is unambiguous. Each of the above strings determines a unique calculation without the need for using parentheses or operator hierarchy. To evaluate a string in this new notation, we proceed as follows: – Read the string from left to right. – When we find the first substring of the form operator - operand - operand (or o-o-o for short), we replace these three symbols with the result of the indicated arithmetic calculation. – We then re-scan the string form the left. – We continue this process until there is only one number left, which is the value of the original expression.

Example Consider the expression: + 3 * 4 5:

Consider the second expression: * + 3 4 5:

Example Convert the following arithmetic expression into operator prefix notation: ((1 + 2) * (3 + 4) + 5) * 6. This normal notation is called operator infix notation, with which we need parentheses to avoid ambiguity. Let’s us draw the derivation tree:

Reading around the tree gives the equivalent prefix notation expression: * + * + 1 2 + 3 4 5 6.

Evaluate the String

This operator prefix notation was invented by Lukasiewicz (1878 - 1956) and is often called Polish notation. There is a similar operator postfix notation (also called Polish notation), in which the operation symbols (+, -, ...) come after the operands. This can be derived by tracing around the tree of the other side, keeping our right hand on the tree and then reversing the resultant string. Both these methods of notation are useful for computer science: Compilers often convert infix to prefix and then to assembler code.

Ambiguity- example Consider the language generated by the following CFG: S → AB A → a B → b There are two derivations of the word ab: S  AB  aB  ab or S  AB  Ab  ab

However, These two derivations correspond to the same syntax tree: The word ab is therefore not ambiguous. In general, when all the possible derivation trees are the same for a given word, then the word is unambiguous.

Ambiguity - Definition A CFG is called ambiguous if for at least one word in the language that it generates, there are two possible derivations of the word that correspond to different syntax trees. If a CFG is not ambiguous, it is called unambiguous.

Example The following CFG defines the language of all non-null strings of a’s: S → aS| Sa |a The word a3 can be generated by 4 different trees:

Example 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: