1 Chapter 3 - 2 Construction Techniques. 2 Section 3.3 Grammars A grammar is a finite set of rules, called productions, that are used to describe the.

Slides:



Advertisements
Similar presentations
1 Chapter Parsing Techniques. 2 Section 12.3 Parsing Techniques We know (via a theorem) that the context- free languages are exactly those languages.
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.
Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
Equivalence, Order, and Inductive Proof
Transforming Context-Free Grammars to Chomsky Normal Form 1 Roger L. Costello April 12, 2014.
CS5371 Theory of Computation
1 1. Show the result of each of the following set operations in terms of set property. Write your sets as simple as possible. (a) L 0  L 4 (b) L 0  L.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
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.
Chapter 12: Context-Free Languages and Pushdown Automata
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.
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
CMSC 330: Organization of Programming Languages
1 Section 3.3 Grammars A grammar is a finite set of rules, called productions, that are used to describe the strings of a language. Notational Example.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
A sentence (S) is composed of a noun phrase (NP) and a verb phrase (VP). A noun phrase may be composed of a determiner (D/DET) and a noun (N). A noun phrase.
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
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.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Context-Free Grammars
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
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
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.
Discrete Structure Li Tak Sing( 李德成 ) Lectures
Regular Grammars Chapter 7. Regular Grammars A regular grammar G is a quadruple (V, , R, S), where: ● V is the rule alphabet, which contains nonterminals.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
Context Free Grammars.
CMSC 330: Organization of Programming Languages
Section 12.4 Context-Free Language Topics
1 Section 12.3 Context-Free Parsing We know (via a theorem) that the context-free languages are exactly those languages that are accepted by PDAs. When.
Lecture 11 Theory of AUTOMATA
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
1 Chapter 6 Simplification of CFGs and Normal Forms.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Context-Free Languages
Regular Grammars Reading: 3.3. What we know so far…  FSA = Regular Language  Regular Expression describes a Regular Language  Every Regular Language.
Mathematical Foundations of Computer Science Chapter 3: Regular Languages and Regular Grammars.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
1 Section 12.1 Context-Free Languages We know that the language {a n b n | n  N} is not regular, but it certainly has a non-regular grammar, such as S.
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.
Lecture 17: Theory of Automata:2014 Context Free Grammars.
1 Chapter Context-Free Languages. 2 Section 12.1 Context-Free Languages We know that the language {a n b n | n ∈ N} is not regular, but it certainly.
PROGRAMMING LANGUAGES
CONTEXT-FREE LANGUAGES
Formal Language & Automata Theory
Context-Free Grammars
PARSE TREES.
Context-Free Languages
Context-Free Grammars
REGULAR LANGUAGES AND REGULAR GRAMMARS
Context-Free Grammars
Context-Free Grammars 1
Finite Automata and Formal Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Relationship to Left- and Rightmost Derivations
Theory of Computation Lecture #
CFGs: Formal Definition
Context-Free Grammars
COSC 3340: Introduction to Theory of Computation
Context-Free Grammars
Presentation transcript:

1 Chapter Construction Techniques

2 Section 3.3 Grammars A grammar is a finite set of rules, called productions, that are used to describe the strings of a language. Notational Example. The productions take the form a → ß, where a and ß are strings over an alphabet of terminals and nonterminals. Read a → ß as, “a produces ß,” “a derives ß,” or “a is replaced by ß.” The following four expressions are productions for a grammar. S → aSBAlternative Short form S → Λ S → aSB | Λ B → bB B → bB | b B → b.

3 Terms Terminals: {a, b}, the alphabet of the language. Nonterminals: {S, B}, the grammar symbols (uppercase letters), disjoint from terminals. Start symbol: S, a specified nonterminal alone on the left side of some production. Sentential form: any string of terminals and/or nonterminals.

4 Derivation a transformation of sentential forms by means of productions as follows: If xay is a sentential form and a→ß is a production, then the replacement of a by ß in xay to obtain xßy is a derivation step, which we denote by xay → xßy. Example Derivation: S ➯ aSB ➯ aaSBB ➯ aaBB ➯ aabBB ➯ aabbB ➯ aabbb. This is a leftmost derivation, where each step replaces the leftmost nonterminal. The symbol ➯ + means one or more steps and ➯ * means zero or more steps. So we could write S ➯ + aabbb or S ➯ * aabbb or aSB ➯ * aSB, and so on.

5 The Language of a Grammar The language of a grammar is the set of terminal strings derived from the start symbol. Example. Can we find the language of the grammar S → aSB | Λ and B → bB | b? Solution: Examine some derivations to see if a pattern emerges. S ➯ Λ S ➯ aSB ➯ aB ➯ ab S ➯ aSB ➯ aB ➯ abB ➯ abbB ➯ abbb S ➯ aSB ➯ aaSBB ➯ aaBB ➯ aabB ➯ aabb S ➯ aSB ➯ aaSBB ➯ aaBB ➯ aabBB ➯ aabbBB ➯ aabbbB ➯ aabbbb. So we have a pretty good idea that the language of the grammar is {a n b n+k | n, k ∊ N}. Quiz (1 minute). Describe the language of the grammar S → a | bcS. Solution: {(bc) n a | n ∊ N}.

6 Construction of Grammars Example. Find a grammar for {a n b | n ∊ N}. Solution: We need to derive any string of a’s followed by b. The production S → aS can be used to derive strings of a’s. The production S → b will stop the derivation and produce the desired string ending with b. So a grammar for the language is S → aS | b.

7 Quizzes Quiz (1 minute). Find a grammar for {ba n | n ∊ N}. Solution: S → Sa | b. Quiz (1 minute). Find a grammar for {(ab) n | n ∊ N}. Solution: S → Sab | Λ or S → abS | Λ.

8 Rules for Combining Grammars Let L and M be two languages with grammars that have start symbols A and B, respectively, and with disjoint sets of nonterminals. Then the following rules apply. L ∪ M has a grammar starting with S → A | B. LM has a grammar starting with S → AB. L* has a grammar starting with S → AS | Λ.

9 Example Find a grammar for {a m b m c n | m, n ∊ N}. Solution: The language is the product LM, where L = {a m b m | m ∊ N} and M = {c n | n ∊ N}. So a grammar for LM can be written in terms of grammars for L and M as follows. S → AB A → aAb | Λ B → cB | Λ.

10 Example Find a grammar for the set Odd, of odd decimal numerals with no leading zeros, where, for example, 305 ∊ Odd, but 0305 ∉ Odd. Solution: Notice that Odd can be written in the form Odd = (PD*)*O, where O = {1, 3, 5, 7, 9}, P = {1, 2, 3, 4, 5, 6, 7, 8, 9}, and D = {0} ∪ P. Grammars for O, P, and D can be written with start symbols A, B, and C as: A → 1 | 3 | 5 | 7 | 9, B → A | 2 | 4 | 6 | 8, and C → B | 0. Grammars for D* and PD* and (PD*)* can be written with start symbols E, F, and G as: E → CE | Λ, F → BE, and G → FG | Λ. So a grammar for Odd with start symbol S is S → GA.

11 Example Find a grammar for the language L defined inductively by, –Basis: a, b, c ∊ L. –Induction: If x, y ∊ L then ƒ(x), g(x, y) ∊ L. Solution: We can get some idea about L by listing some of its strings. a, b, c, ƒ(a), ƒ(b), …, g(a, a), …, g(ƒ(a), ƒ(a)), …, ƒ(g(b, c)), …, g(ƒ(a), g(b, ƒ(c))), … So L is the set of all algebraic expressions made up from the letters a, b, c, and the function symbols ƒ and g of arities 1 and 2, respectively. A grammar for L can be written as S → a | b | c | ƒ(S) | g(S, S). For example, a leftmost derivation of g(ƒ(a), g(b, ƒ(c))) can be written as S ➯ g(S, S) ➯ g(ƒ(S), S) ➯ g(ƒ(a), S) ➯ g(ƒ(a), g(S, S)) ➯ g(ƒ(a), g(b, S)) ➯ g(ƒ(a), g(b, ƒ(S))) ➯ g(ƒ(a), g(b, ƒ(c))).

12 Parse Tree A Parse Tree is a tree that represents a derivation. The root is the start symbol and the children of a nonterminal node are the symbols(terminals, nonterminals, or Λ ) on the right side of the production used in the derivation step that replaces that node. Example. The tree shown in the picture is the parse tree for the following derivation: S ➯ g(S, S) ➯ g(ƒ(S), S) ➯ g(ƒ(a), S) ➯ g(ƒ(a), b). S g( S, S) f (S)b a

13 Ambiguous Grammar Means there is at least one string with two distinct parse trees, or equivalently, two distinct leftmost derivations or two distinct rightmost derivations. Example. Is the grammar S → SaS | b ambiguous? Solution: Yes. For example, the string babab has two distinct leftmost derivations: S ⇒ SaS ⇒ SaSaS ⇒ baSaS ⇒ babaS ⇒ babab. S ⇒ SaS ⇒ baS ⇒ baSaS ⇒ babaS ⇒ babab. The parse trees for the derivations are pictured in the next slide.

14 parse trees S S SSSS SS SS a a a a b bb b bb

15 Quiz (2 minutes) Show that the grammar S → abS | Sab | c is ambiguous. Solution: The string abcab has two distinct leftmost derivations: S ⇒ abS ⇒ abSab ⇒ abcab S ⇒ Sab ⇒ abSab ⇒ abcab.

16 Unambiguous Grammar Sometimes one can find a grammar that is not ambiguous for the language of an ambiguous grammar. Example. The previous example showed S → SaS | b is ambiguous. The language of the grammar is {b, bab, babab, …}. Another grammar for the language is S → baS | b. It is unambiguous because S produces either baS or b, which can’t derive the same string.

17 Example The previous quiz showed S → c | abS | Sab is ambiguous. Its language is {(ab) m c(ab) n | m, n ∈ N}. Another grammar for the language is S → abS | cT and T → abT | ٨. It is unambiguous because S produces either abS or cT, which can’t derive the same string. Similarly, T produces either abT or ٨, which can’t derive the same string.

18 The End of Chapter 3 - 2