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.

Slides:



Advertisements
Similar presentations
1 Pushdown Automata (PDA) Informally: –A PDA is an NFA-ε with a stack. –Transitions are modified to accommodate stack operations. Questions: –What is a.
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.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
CS5371 Theory of Computation
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
Discussion #31/20 Discussion #3 Grammar Formalization & Parse-Tree Construction.
Specifying Languages CS 480/680 – Comparative Languages.
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
COP4020 Programming Languages
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
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.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
CSE 3813 Introduction to Formal Languages and Automata Chapter 8 Properties of Context-free Languages These class notes are based on material from our.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
Design contex-free grammars that generate: L 1 = { u v : u ∈ {a,b}*, v ∈ {a, c}*, and |u| ≤ |v| ≤ 3 |u| }. L 2 = { a p b q c p a r b 2r : p, q, r ≥ 0 }
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.
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:
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.
Context-free Languages
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.
Context-Free Grammars Chapter 11. Languages and Machines.
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.
Regular Grammars Chapter 7 1. Regular Grammars A regular grammar G is a quadruple (V, , R, S), where: ● V is the rule alphabet, which contains nonterminals.
1 L= { w c w R : w  {a, b}* } is accepted by the PDA below. Use a construction like the one for intersection for regular languages to design a PDA that.
CPS 506 Comparative Programming Languages Syntax Specification.
Lecture 11 Theory of AUTOMATA
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.
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.
Critique this PDA for L= { u u R v v R : u ∈ {0,1}* and v ∈ {0,1}+ } u0εu0 u1εu1 uεεvε v00vε v11vε vεεfε sεεtε t0εt0 t1εt1 t00tε t11tε tεεuε After you.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Syntax Analyzer (Parser)
Context-Free Languages
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Chapter 8 Properties of Context-free Languages These class notes are based on material from our textbook, An Introduction to Formal Languages and Automata,
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
1.Draw a parse tree for the following derivation: S  C A C  C A b b  b b A b b  b b B b b  b b a A a a b b  b b a b a a b b 2. Show on your parse.
1.Draw a parse tree for the following derivation: S  C A C  C A b b  b b A b b  b b B b b  b b a A a a b b  b b a b a a b b 2. Show on your parse.
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.
Lecture 15 Ambiguous Grammars Topics: Context Free Grammars Language generated by a grammar Proofs with L(G) Ambiguous grammars October 20, 2008 CSCE 355.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Theory of Languages and Automata By: Mojtaba Khezrian.
Context Free Grammars & Parsing CPSC 388 Fall 2001 Ellen Walker Hiram College.
Computability Joke. Context-free grammars Parsing. Chomsky
Parsing & Context-Free Grammars
Formal Language & Automata Theory
L1= { w  {a,b}* : w consists of all strings that begin with an even number of a's followed by an odd number of b's. } L2= { w  {a,b}* : the number of.
Recap lecture 33 Example of trees, Polish Notation, examples, Ambiguous CFG, example,
Syntax Specification and Analysis
PARSE TREES.
Context-Free Languages
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Chapter 7 Regular Grammars
CHAPTER 2 Context-Free Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

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 a DFA first then get the grammar from the DFA. Recall: A regular context-free grammar can have at most one non-terminal on the RHS of each rule, if there is a non- terminal, it is the last symbol on the RHS of the rule.

2 Announcements: Assignment 3 has been posted. If you do the 10 point bonus question, make sure you submit your code on connex. Midterm: Wednesday June 27, in class. Old midterms are available from the course web page. Assignment #1 and #2 solutions are available in the connex resources.

3

Create a NDFA which accepts the language generated by this context-free grammar. Start symbol: S S → aa S S → ε S → M M → bbb M → ab M M → b S M → E E → aa

Side note: I am using a more general definition of a NDFA here which permits transitions on strings. That is, Δ is a relation which is a finite subset of K x Σ* x K current string next state

Theorem: If L has a regular context-free grammar, then L is a regular language. Proof: A NDFA can be constructed that accepts the language that the regular context- free grammar generates. Note: this is in the text as Exercise

Given the regular context-free grammar G=(V, Σ, R, S) construct a NDFA M = (K, Σ, Δ, s, F) where K= (V – Σ) ⋃ { f }, s= S, F = { f } For each rule T → u R with u  Σ*, R  V-Σ, add a transition (T, u, R) to Δ. For each rule T → u with u in Σ*, add a transition (T, u, f) to Δ.

L= { a n b p : n ≤ p ≤ 3n, n,p ≥0} Start symbol S. S → a S b S → ε S → a S bb S → a S bbb This works because any integer p can be expressed as: p= r + 2 (n – r) when n ≤ p ≤ 2n, and p= 2r + 3 (n - r) when 2n ≤ p ≤ 3n.

Parse Trees: Definition of parse trees. Parse trees are created when programs are compiled to aid in checking syntax and determining semantics of a program. Leftmost derivations. Example of how parse trees are used to prove the pumping theorem for context- free languages.

L= {w in {a, b}* : w has the same number of a’s and b’s} Start symbol S S→ e S → a B S → b A A → a A → a S A → b A A B → b B → b S B → a B B S- generates strings with #a’s = #b’s A- means need one “a” B- means need one “b”

Three derivations for aabbab: 1. S  aB  aaBB  aabSB  aabbAB  aabbaB  aabbab 2. S  aB  aaBB  aabB  aabbS  aabbaB  aabbab 3. S  aB  aaBB  aaBbS  aaBbaB  aaBbab  aabbab Leftmost derivation: leftmost non-terminal replaced at each step. Which ones are leftmost?

Two of the derivations are essentially the same- they correspond to the same leftmost derivation. The other one is different. A context-free grammar is ambiguous if there is a string in the language which has two or more distinct parse trees (or equivalently, two or more distinct leftmost derivations).

Parse Trees- G = (V, Σ, R S) Nodes of parse trees are each labelled with one symbol from V ⋃ {ε}. Node at top- root, labelled with start symbol S. Leaves- nodes at the bottom. Yield- concatenate together the labels on the leaves going from left to right.

Leftmost derivation: always replace the leftmost non-terminal at each step. Theorem: Every derivation in a grammar G corresponds to a unique leftmost derivation. Proof: Construct the parse tree and determine the leftmost derivation from it.

Find the leftmost derivation for this parse tree.

To pump: Look for a path from root with a repeated non-terminal.

Path: S - A - T - A Non-terminal A is repeated.

Yield from second copy downwards gives x.

Consider tree from first copy downwards. Yield before x is v. Yield after x is y.

Yield after y is z. Yield of whole tree before v is u.

Cut off part of tree from second copy downwards.

Cut off part from first copy downwards.

To pump zero times: New yield: u x z = b bc c = u v 0 x y 0 z

To pump twice:

Pumping twice: New yield is: u v v x y y z = u v 2 x y 2 z

To pump three times: New yield is: u vvv x yyy z = u v 3 x y 3 z

To pump n times: New yield is: = u v n x y n z

The Pumping Theorem for Context-Free Languages: Let G be a context-free grammar. Then there exists some constant k which depends on G such that for any string w which is generated by G with |w| ≥ k, there exists u, v, x, y, z, such that 1. w= u v x y z, 2. |v| + |y| ≥ 1, and 3. u v n x y n z is in L for all n ≥ 0.

Prove the following languages are context- free by designing context-free grammars which generate them: L 1 = {a n b n c p : n, p ≥ 0} L 2 = {a n b p c n : n, p ≥ 0} L 3 = {a n b m : n ≠ m, n, m ≥ 0} Hint: L 3 = {a n b m : n m} L 4 = { c u c v c : |u|=|v|, u, v  {a, b}* } What is L 1  L 2 ?