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.

Slides:



Advertisements
Similar presentations
CS2303-THEORY OF COMPUTATION Push Down Automata (PDA)
Advertisements

6.2 Closure Properties of CFL's
Lecture 10: Context-Free Languages Contextually David Evans
Regular operations Sipser 1.1 (pages 44 – 47). CS 311 Fall Building languages If L is a language, then its complement is L’ = {w | w ∉ L} Let A.
David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 11: Parsimonious Parsing.
Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –
Non-Deterministic Finite Automata
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
Lecture 3 Universal TM. Code of a DTM Consider a one-tape DTM M = (Q, Σ, Γ, δ, s). It can be encoded as follows: First, encode each state, each direction,
Finite State Machines Finite state machines with output
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
The Pumping Lemma for CFL’s
Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
4.5 Inherently Ambiguous Context-free Language For some context-free languages, such as arithmetic expressions, may have many different CFG’s to generate.
Lecture 16 Deterministic Turing Machine (DTM) Finite Control tape head.
Erik Jonsson School of Engineering and Computer Science FEARLESS Engineering CS 4384 – 001 Automata Theory Thursday: Context-Free.
Lecture 6 Nondeterministic Finite Automata (NFA)
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.
LR-Grammars LR(0), LR(1), and LR(K).
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
CS5371 Theory of Computation
Fall 2006Costas Busch - RPI1 Regular Expressions.
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
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
Fall 2004COMP 3351 Single Final State for NFA. Fall 2004COMP 3352 Any NFA can be converted to an equivalent NFA with a single final state.
1 Single Final State for NFAs and DFAs. 2 Observation Any Finite Automaton (NFA or DFA) can be converted to an equivalent NFA with a single final state.
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.
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:
Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language.
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 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
Pushdown Accepters & Context-Free Grammars Sipser, Theorem 2.12 Denning, Chapter 8.
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.
Lecture 8 Context-Free Grammar- Cont.
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.
Context Free Grammars and Regular Grammars Needs for CFG Grammars and Production Rules Context Free Grammars (CFG) Regular Grammars (RG)
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.
 2005 SDU Lecture11 Decidability.  2005 SDU 2 Topics Discuss the power of algorithms to solve problems. Demonstrate that some problems can be solved.
Standard Representations of Regular Languages
NPDAs Accept Context-Free Languages
CS314 – Section 5 Recitation 3
PUSHDOWN AUTOMATA. PUSHDOWN AUTOMATA Hierarchy of languages Regular Languages  Finite State Machines, Regular Expression Context Free Languages 
Jaya Krishna, M.Tech, Assistant Professor
Context-Free Languages
Context-Free Grammars and Languages
CSE322 LEFT & RIGHT LINEAR REGULAR GRAMMAR
DPDA Deterministic PDA
Regular Grammar.
Definition: Let G = (V, T, P, S) be a CFL
Finite Automata Reading: Chapter 2.
Properties of Context-Free Languages
Theory of Computation Lecture #
Chapter 2 Context-Free Language - 02
DPDA Deterministic PDA
CSE 105 theory of computation
Chapter 1 Regular Language
Teori Bahasa dan Automata Lecture 10: Push Down Automata
CSE 105 theory of computation
CSE 105 theory of computation
CSE 105 theory of computation
Presentation transcript:

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 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 | ε })

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 0S in L(G) because S 0S1 00S in L(G) because S * 0 1. L(G) = {0 1 | n > 0} nn nn nn

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. sq1q1 f=q n x1x1 xnxn S x 1 q 1 x 1 x 2 q 2 ··· x 1 …x n f x 1 …x n

x in L(M) There is a path associated with x from initial state to a final state. 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 G = ({S, A}, {0, 1}, {S →1A, A → 00}, S) 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 For each rule A → x, x in Σ*, draw an edge A → f x

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

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 RR R R R R R

Example 1 G = ({S,A}, {0, 1}, {S→A01, A→10}, S) G =({S,A}, {0, 1}, {S →10A, A →01}, S) NFA accepts L(G ) L(G )={1001} 1001 AS R R 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) 0 1 SA R R