Lecture 8 Context-Free Grammar- Cont.

Slides:



Advertisements
Similar presentations
Lecture 11 Context-Free Grammar. Definition A context-free grammar (CFG) G is a quadruple (V, Σ, R, S) where V: a set of non-terminal symbols Σ: a set.
Advertisements

Erik Jonsson School of Engineering and Computer Science FEARLESS Engineering CS 4384 – 001 Automata Theory Thursday: Context-Free.
1 Pushdown Automata (PDA) Informally: –A PDA is an NFA-ε with a stack. –Transitions are modified to accommodate stack operations. Questions: –What is a.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
LR-Grammars LR(0), LR(1), and LR(K).
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
CFGs and PDAs Sipser 2 (pages ). Last time…
Decidable languages Sipser 4.1 (pages ). CS 311 Mount Holyoke College 2 Hierarchy of languages All languages Turing-recognizable Turing-decidable.
CS5371 Theory of Computation
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
Costas Busch - RPI1 Grammars. Costas Busch - RPI2 Grammars Grammars express languages Example: the English language.
Courtesy Costas Busch - RPI
1 Grammars. 2 Grammars express languages Example: the English language.
104 Closure Properties of Regular Languages Regular languages are closed under many set operations. Let L 1 and L 2 be regular languages. (1) L 1  L 2.
1 Regular Grammars Generate Regular Languages. 2 Theorem Regular grammars generate exactly the class of regular languages: If is a regular grammar then.
Foundations of (Theoretical) Computer Science Chapter 4 Lecture Notes (Section 4.1: Decidable Languages) David Martin With modifications.
1 Reverse of a Regular Language. 2 Theorem: The reverse of a regular language is a regular language Proof idea: Construct NFA that accepts : invert the.
79 Regular Expression Regular expressions over an alphabet  are defined recursively as follows. (1) Ø, which denotes the empty set, is a regular expression.
Transparency No. P2C1-1 Formal Language and Automata Theory Part II Pushdown Automata and Context-Free Languages.
1 Background Information for the Pumping Lemma for Context-Free Languages Definition: Let G = (V, T, P, S) be a CFL. If every production in P is of the.
Prof. Busch - LSU1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
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.
Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL.
Fall 2003Costas Busch - RPI1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
Lecture 3 Graph Representation for Regular Expressions
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2010.
Theory of Languages and Automata
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
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.
CSCI 2670 Introduction to Theory of Computing September 15, 2005.
Grammars CPSC 5135.
Lecture # 5 Pumping Lemma & Grammar
Lecture Pushdown Automata. stack stack head finite control tape head tape.
 Regular Grammar and Regular Language [Def 3.1] Regular Grammar(use to in lexical analysis) Type 3 grammar(regular grammar, RG) Type 3 grammar(regular.
Chapter 6 Properties of Regular Languages. 2 Regular Sets and Languages  Claim(1). The family of languages accepted by FSAs consists of precisely the.
Non-CF Languages The language L = { a n b n c n | n  0 } does not appear to be context-free. Informal: A PDA can compare #a’s with #b’s. But by the time.
CS 3813: Introduction to Formal Languages and Automata
Pumping Lemma for CFLs. Theorem 7.17: Let G be a CFG in CNF and w a string in L(G). Suppose we have a parse tree for w. If the length of the longest path.
Grammars A grammar is a 4-tuple G = (V, T, P, S) where 1)V is a set of nonterminal symbols (also called variables or syntactic categories) 2)T is a finite.
Regular Grammars Reading: 3.3. What we know so far…  FSA = Regular Language  Regular Expression describes a Regular Language  Every Regular Language.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2007.
CSCI 2670 Introduction to Theory of Computing October 13, 2005.
Mathematical Foundations of Computer Science Chapter 3: Regular Languages and Regular Grammars.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
Algorithms for hard problems Automata and tree automata Juris Viksna, 2015.
CS 154 Formal Languages and Computability February 11 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron.
Costas Busch - LSU1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
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.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2006.
Lecture 6: Context-Free Languages
Theory of Languages and Automata By: Mojtaba Khezrian.
 2005 SDU Lecture11 Decidability.  2005 SDU 2 Topics Discuss the power of algorithms to solve problems. Demonstrate that some problems can be solved.
Theory of Computation Automata Theory Dr. Ayman Srour.
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
Chapter 2. Formal Languages Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
Language Recognition MSU CSE 260.
Context-Free Grammars: an overview
Complexity and Computability Theory I
REGULAR LANGUAGES AND REGULAR GRAMMARS
Regular Grammar.
Definition: Let G = (V, T, P, S) be a CFL
CHAPTER 2 Context-Free Languages
فصل دوم Context-Free Languages
Properties of Context-Free Languages
Chapter 1 Regular Language
Presentation transcript:

Lecture 8 Context-Free Grammar- Cont.

Formal Definition A context-free grammar (CFG) G is a quadruple (V, Σ, R, S) where V: a set of non-terminal symbols Σ: a set of terminals (V ∩ Σ = Ǿ) R: a set of rules (R: V → (V U Σ)*) S: a start symbol.

Example V = {q, f,} Σ = {0, 1} R = {q → 11q, q → 00f, f → 11f, f → ε } S = q (R= {q → 11q | 00f, f → 11f | ε }) ** See example 2.4 P.105

Designing CFG Just like designing FA, it requires creativity FIRST: Some CFLs are union of simpler CFLs, use Divide and Conquer: Ex: In order to design the grammar for the language {0n1n | n>=0} U {1n0n | n>=0} First: construct grammar for {0n1n | n>=0} = S → 0S11 | ε And for {1n0n | n>=0} S → 1S20 | ε Add the rule S → S1 | S2 to give the grammar: S → S1 | S2 S → 0S11 | ε S → 0S21 | ε

SECOND: Constructing CFG is easy for languages that happen to be regular. Therefore, first design DFA, the convert the DFA to CFG as follows: - Make a variable Ri for each state qi - Add the rule Ri → aRj to the CFG if δ(qi,a)=qj is a transition in DFA. - Add the rule Ri →ε if qi is an accept state of DFA. - Make R0 the start variable of the grammar, where q0 is the start state of the machine. - Verify on your own that the resulting CFG generates the same language that the DFA recognizes.

THIRD: remember when the machine – in order to accept a language – it had to memorize an unbounded amount of info. For example, {0n1n | n>=0} where the machine needs to memorize an infinite no. of 0s and 1s! In order to construct a CFG to handle such situation, the CFG should have the rule R → vRu

Finally: Strings may contain certain structures that appear recursively as part of other structures, such as example 2.4

How do we use rules? If A → B, then xAy xBy and we say that xAy derivates xBy. If s ··· t, then we write s * t. A string x in Σ* is generated by G=(V,Σ,R,S) if S * x. L(G) = { x in Σ* | S * x}.

Example G = ({S}, {0,1}. {S → 0S1 | ε }, S) ε in L(G) because S ε . 01 in L(G) because S 0S1 01. 0011 in L(G) because S 0S1 00S11 0011. 0 1 in L(G) because S * 0 1 . L(G) = {0 1 | n > 0} n n n n n n

Context-Free Language (CFL) A language L is context-free if there exists a CFG G such that L = L(G).

Theorem For every regular set, there exists a CFG G such that L=L(G). Proof. Let L=L(M) for a DFA M=(Q, Σ, δ, s, F). Construct a CFG G=(V, Σ, R, S) as follows. V = Q, Σ = Σ, R = { q → ap | δ(q,a) = p } U { f → ε | f in F}, S = s. x1 xn S x1q1 x1x2q2 ··· x1…xnf x1…xn f=qn s q1

There is a path associated with x from initial state to a final state. x in L(M) S * x Therefore, L(M) = L(G).

Corollary Every regular language is a CFL. The class of regular languages is a proper subclass of CFLs. CFL Regular Why, proper?

Regular Grammar Regular grammar is a CFG (V, Σ, R, S) such that every rule is in form V→ Σ*(V+ε) Example S → aA A → Sb S → ε G =? (right it in a formal way) Remark: Every regular language can be generated by a regular grammar.

Theorem Every regular grammar generates a regular language. Proof. Consider a regular grammar G=(V, Σ, R, S). Construct a string-labeled digraph with vertex set V U {f} as follows: For each rule A → xB, x in Σ* and B in V, draw an edge A → B. x x For each rule A → x, x in Σ*, draw an edge A → f

G = ({S,A}, {0,1}, {S→0S | 10A, A→00}, S) Example G = ({S,A}, {0,1}, {S→0S | 10A, A→00}, S) 10 00 f S A This string-labeled digraph with initial state S and a final state f is a state diagram of an NFA M. There is a path associated with x from S to f in M. S * x in Σ* Therefore, L(G) = L(M).

Corollary A language L is regular if and only if L can be generated by a regular grammar.

Right-Linear and Left-Linear A right regular grammar (also called right linear grammar) is a formal grammar (V, Σ, R, S) such that all the production rules in R are of one of the following forms: B → a - where B is a non-terminal in N and a is a terminal in Σ B → aC - where B and C are in N and a is in Σ B → ε - where B is in N and ε denotes the empty string, i.e. the string of length 0. In a left regular grammar (also called left linear grammar), all rules obey the forms A → a - where A is a non-terminal in N and a is a terminal in Σ A → Ba - where A and B are in N and a is in Σ A → ε - where A is in N and ε is the empty string.

Example As an example from the area of programming languages, the set of all strings denoting a floating point number can be described by a right regular grammar G with N = {S, A,B,C,D,E,F}, Σ = {0,1,2,3,4,5,6,7,8,9,+,-,.,e}, where S is the start symbol, and R consists of the following rules:

Right-Linear and Left-Linear The regular grammar is also called a right- linear grammar. A grammar G=(V, Σ, R, S) is left-linear if every rule is in form V → (V+ε)Σ*. (e.g., ({S,A}, {0, 1}, {S→A01, A→10}, S) Remark: Every language generated from a left-linear grammar is regular. Why?

Why? For left-linear grammar G = (V, Σ, R, S), construct G = (V, Σ, R , S) where R = {A→W | A→W in R}. G is right-linear. Hence, L(G ) is regular. Therefore, L(G) = L(G ) is regular. R R R R R R R R

Example 1 G = ({S,A}, {0, 1}, {S→A01, A→10}, S) NFA accepts L(G ) L(G )={1001} L(G)={1001} R R 10 01 S A R

Example 2 L(G) = 0*1 L(G ) = 10* NFA accepts 10* G = ({S,A}, {0,1}, {S →1A, A →0A|ε}, S) G = ({S,A}, {0,1}, {S →A1, A →A0|ε}, S) R 1 S A R