Context-Free Grammars
(CFGs)
01/25/17
COMP 335
1
Example
A context-free grammarG :
S aSb
S
A derivation:
S aSb aaSbb aabb
COMP 335
2
A context-free grammarG :
S aSb
S
Another derivation:
S aSb aaSbb aaaSbbb aaabbb
COMP 335
3
S aSb
S
n n
L(G ) cfw_a b : n
Properties of Regular Languages
Pumping Lemma. Every regular language
satisfies the pumping lemma. If somebody
presents you with fake regular language, use
the pumping lemma to show a contradiction.
Closure properties. Building automata from
components
Problems that computers cannot solve
Evidently, it is important to know that programs do what they are supposed to, IOW,
we would like to make sure that programs are
correct.
It is easy to see that the program
main()
cfw_
printf(hello, world\n);
indeed p
Department of Computer Science & Software Engineering
Comp335 Introduction to Theoretical Computer Science
Winter 2017
Assignment 4. Due date: April 13, by 23:59 P.M. EDT
1. (a) Construct a Turing machine for the language
cfw_xay x, y cfw_a, b , x = y.
(a
Closure Properties of CFLs
Consider a mapping
s : 2
where and are finite alphabets. Let w ,
where w = a1a2 . . . an, and define
s(w) = s(a1).s(a2). .s(an)
and, for L ,
s(L) =
[
s(w).
wL
Such a mapping s is called a substitution.
1
Example: = cfw_0, 1, = c
Equivalence and Minimization of Automata
Let A = (Q, , , q0, F ) be a DFA, and cfw_p, q Q.
We define
p q w : (p, w) F iff (q, w) F
If p q we say that p and q are equivalent
If p 6 q we say that p and q are distinguishable
IOW (in other words) p and q ar
From regexs to -NFAs
Theorem 3.7: For every regex R we can construct and -NFA A, s.t. L(A) = L(R).
Proof: By structural induction:
Basis: Automata for , , and a.
(a)
(b)
a
(c)
1
Induction: Automata for R + S, RS, and R
R
S
(a)
R
S
(b)
R
(c)
2
Example: We
Department of Computer Science & Software Engineering
Comp335 Introduction to Theoretical Computer Science
Winter 2017
Assignment 1. Solutions.
1. Construct a DFA for each of the following languages
(a) All and only those strings over cfw_a, b, c whose sy
Properties of CFLs
Simplification of CFGs. This makes life easier, since we can claim that if a language is CF,
then it has a grammar of a special form.
Pumping Lemma for CFLs. Similar to the
regular case.
Closure properties. Some, but not all, of the
Ambiguity in Grammars and Languages
In the grammar
1.
2.
3.
4.
E
E
E
E
I
E+E
EE
(E)
the sentential form E + E E has two derivations:
E E+E E+EE
and
E EE E+EE
This gives us two parse trees:
E
E
E
+
E
E
*
(a)
E
E
E
*
+
E
E
(b)
1
The mere existence of sever
Equivalence of PDAs and CFGs
A language is
generated by a CFG
if and only if it is
accepted by a PDA by empty stack
if and only if it is
accepted by a PDA by final state
Grammar
PDA by
empty stack
PDA by
final state
We already know how to go between null
Undecidability
We want to prove undecidable Lu which is the
language of pairs (M, w) such that:
1. M is a TM (encoded in binary) with input
alphabet cfw_0, 1.
2. w cfw_0, 1.
3. M accepts w.
1
The landscape of languages (problems)
Ld .
Lu .
RE Recursive
bu
CONCORDIA UNIVERSITY
Dept. of Computer Science and Software Engineering
COMP 335 Introduction to Theoretical Computer Science
Fall 2016
Assignment 3
Electronic Submission is due on Thursday November 17th at 23:59
1. [20 points] Find context-free grammars
This is a theoretical course, which studies
Introduction to
theory of computation.
Theoretical Computer Science
It discusses formal languages, automata, and
computability. It introduces abstract models of
COMP 335
computation and computers.
Why theory,
Concordia University
Dept. of Computer Science and Software Engineering
COMP 335 Introduction to Theoretical Computer Science
http:/users.encs.concordia.ca/~c335_2
Fall 2016
Instructors:
Sections:
Lectures:
Office Hours:
Phones:
Emails:
Web:
N. Shiri
G
TJ
More Applications
of
The Pumping Lemma
01/25/17
COMP 335
1
The Pumping Lemma:
For infinite context-free languageL
there exists an integer m
for any string
w L,
such that
| w |m
we can write
w uvxyz
with lengths
| vxy |m and | vy |1
and it must be:
i i
uv
- Closure Properties of Regular
Languages
- Regular Expressions
01/25/17
COMP 335
1
NFA with Single Final State
COMP 335
2
Given any NFA, we can always convert
it into an equivalent NFA which has only
ONE final state (how?)
COMP 335
3
Example
a
NFA
b
a
b
Standard Representations
of Regular Languages
Regular Languages
DFAs
NFAs
01/25/17
Regular
Grammars
Regular
Expressions
COMP 335
1
When we say: We are given
a regular language
L
it means that :L is in a standard
representation
COMP 335
2
Elementary Questi
Simplifications / Transformations
of
Context-Free Grammars
01/25/17
COMP 335
1
A Substitution Rule
S
A
A
B
B
aB
aaA
abBc
aA
b
Equivalent
grammar
S aB | ab
Substitute
B b
COMP 335
A aaA
A abBc | abbc
B aA
2
A Substitution Rule
S aB | ab
A aaA
A abBc | abbc
A Universal Turing Machine
01/25/17
COMP 335
1
A limitation of Turing Machines
we discussed so far is that:
they are hardwired
i.e., they execute
only one program
Real Computers are re-programmable
COMP 335
2
Solution: Universal Turing Machine
Attributes:
NPDA Accept
Context-Free Languages
01/25/17
COMP 335
1
Theorem:
Context-Free
Languages
(Grammars)
COMP 335
Languages
Accepted by
NPDAs
2
Proof - Step 1:
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
G
Convert any context-free grammar
to a
Recursively Enumerable
(r.e.)
and
Recursive Languages
01/25/17
COMP 335
1
The Chomsky Hierarchy
Non-recursively enumerable
Recursively-enumerable
Recursive
Context-sensitive
Context-free
Regular
COMP 335
2
Definition:
A language is recursively enumerabl
i
Turings Thesis
01/25/17
COMP 335
1
Turings thesis:
Any computation carried out
by mechanical means can be
performed by a Turing Machine
(1930)
COMP 335
2
In Computer Science:
A computation is mechanical if
it can be performed by a TM
There isnt any known
Context-Free Grammars and Languages
We have seen that many languages cannot
be regular. Thus we need to consider larger
classes of langs.
Contex-Free Languages (CFLs) played a central role natural languages since the 1950s,
and in compilers since the 19
Regular expressions
A FA (NFA or DFA) is a blueprint for contructing a machine recognizing a regular language.
A regular expression is a user-friendly, declarative way of describing a regular language.
Example: 01 + 10
Regular expressions are used in e.g.
Pushdown Automata
A pushdown automata (PDA) is essentially an
-NFA with a stack.
On a transition the PDA:
1. Consumes an input symbol.
2. Goes to a new state (or stays in the old).
3. Replaces the top of the stack by any string
(does nothing, pops the sta
Nondeterministic Finite Automata
A NFA can be in several states at once, or,
viewded another way, it can guess which
state to go to next
Example: An automaton that accepts all and
only strings ending in 01.
0, 1
Start
0
q0
1
q1
q2
Here is what happens whe
CSE 355
Spring 2012 - Colbourn
Theory of Computing
Midterm # 1
ID: _
Page 1 of 7
16 February 2012
IMA SAMPLE
NAME
ASU ID
You have one hour and 15 minutes to complete the exam.
Do not open the exam until instructed to do so.
No notes, texts, computers, cal