1 introduction to computability theory lecture5: context free languages prof. amos israeli
Post on 19-Dec-2015
215 views
TRANSCRIPT
On the last lecture we completed our study of regular languages. (There is still a lot to learn but our time is limited…).
Introduction and Motivation
2
In our study of RL-s we Covered:
1. Motivation and definition of regular languages.
2. DFA-s, NFA-s, RE-s and their equivalence.
3. Non Regular Languages and the Pumping Lemma.
Introduction and Motivation
3
In this lecture, we turn to Context Free Grammars and Context Free Languages.
The class of Context Free Languages is an intermediate class between the class of regular languages and the class of Decidable Languages (To be defined).
Introduction and Motivation
4
A Context Free Grammar is a “machine” that creates a language.
A language created by a CF grammar is called A Context Free Language.
(We will show that) The class of Context Free Languages Properly Contains the class of Regular Languages.
Introduction and Motivation
5
Consider grammar :
A CFL consists of substitution rules called Productions.
The capital letters are the Variables.
The other symbols are the Terminals.
Context Free Grammar - Example
6
1G
I
aIbI
Consider grammar :
The grammar generates the language called the language of , denoted by .
Context Free Grammar - Example
7
0| nbaB nn
1G
I
aIbI
1G
1G 1GL
Consider grammar :
This is a Derivation of the word by :
On each step, a single rule is activated. This mechanism is nondeterministic.
Context Free Grammar - Example
8
aaabbbaaaIbbbaaIbbaIbI
1G
I
aIbI
1Gaaabbb
Each internal node of the tree is associated with a single production.
Context Free Grammar - Example
10
bbbaaa
I
I
I
I
aIbI
I
A Context Free Grammar is a 4-tupple where:
1. is a finite set called the variables.2. is a finite set, disjoint from V called the
terminals.3. is a set of rules, where a rule is a variable
and a string of variables and terminals, and4. is the start variable .
CF Grammar – A Formal Definition
11
,
SRV ,,,V
R
VS
A word is a string of terminals.A derivation of a word w from a context Free
Grammar is a sequence of strings ,over , where:
1. is the start variable of G.2. For each , is obtained by
activating a single production (rule) of G on one of the variables of .
A Derivation – A Formal Definition
12
SRVG ,,,
li 1Ss 0
wsssS l ...10
V
is
1is
A word w is in the Language of grammar G, denoted by , if there exists a derivation whose rightmost string is w .
Thus,
CF Grammar – A Formal Definition
13
GLw
GwwGL from derived be can |
FACTORFACTORTERMTERM |
Grammar :
Rules:1. 2. 3.
Example2: Arithmetical EXPS
14
TERMTERMEXPREXPR |
EXPRS
FACTORTERMEXPV ,,
baEXPRFACTOR ||
)(,,,,, ba
2G
TERMEXPR
Derivation of by Grammar : input output rule
Example2: Arithmetical EXPS
17
2G aba
TERMEXPR
Derivation of by Grammar : input output rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
20
2G aba
FACTORTERMTERM
Derivation of by Grammar : input
rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
22
2G aba
aFACTOR
Derivation of by Grammar : input output rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
23
2G aba
aFACTOR
aTERM
Derivation of by Grammar :
input rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
25
2G aba
aTERM
FACTORTERM
aFACTORaTERM
Derivation of by Grammar :
input output rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
26
2G aba
FACTORTERM
aFACTORaTERM
Derivation of by Grammar :
input
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
27
2G aba
aFACTORaTERM
Derivation of by Grammar :
input rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
28
2G aba
EXPRFACTOR
aFACTORaTERM
Derivation of by Grammar :
input output rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
29
2G aba
EXPRFACTOR
aEXPR
aFACTORaTERM
Derivation of by Grammar :
input
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
30
2G aba
aEXPR
aFACTORaTERM
Derivation of by Grammar :
input rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
31
2G aba
aEXPR
TERMEXPREXPR
aTERMEXPRaEXPR aFACTORaTERM
Derivation of by Grammar :
input output rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
32
2G aba
TERMEXPREXPR
aTERMEXPRaEXPR aFACTORaTERM
Derivation of by Grammar :
input
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
33
2G aba
aTERMEXPRaEXPR aFACTORaTERM
Derivation of by Grammar :
input rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
34
2G aba
TERMEXPR
aTERMEXPRaEXPR aFACTORaTERM
Derivation of by Grammar :
input output rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
35
2G aba
TERMEXPR
aTERMTERM
aTERMEXPRaEXPR aFACTORaTERM
Derivation of by Grammar :
input
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
36
2G aba
aTERMTERM
aTERMEXPRaEXPR aFACTORaTERM
Derivation of by Grammar :
input rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
37
2G aba
aTERMTERM
FACTORTERM
aTERMEXPRaEXPR aFACTORaTERM
Derivation of by Grammar :
output rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
38
2G aba
aTERMTERM
FACTORTERM
aFACTORFACTOR
aTERMEXPRaEXPR aFACTORaTERM
Derivation of by Grammar :
input
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
39
2G aba
aTERMTERM aFACTORFACTOR
aTERMEXPRaEXPR aFACTORaTERM
Derivation of by Grammar :
input
rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
40
2G aba
aTERMTERM
aFACTOR
aFACTORFACTOR
aTERMEXPRaEXPR aFACTORaTERM
Derivation of by Grammar :
output rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
41
2G aba
aTERMTERM
aFACTOR
aFACTORFACTOR
aFACTORa
aTERMEXPRaEXPR aFACTORaTERM
Derivation of by Grammar :
input
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
42
2G aba
aTERMTERM aFACTORFACTOR
aFACTORa
aTERMEXPRaEXPR aFACTORaTERM
Derivation of by Grammar :
input rule
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
43
2G aba
aTERMTERM
bFACTOR
aFACTORFACTOR
aFACTORa
aTERMEXPRaEXPR aFACTORaTERM
Derivation of by Grammar :
FACTORTERMTERMEXPR
Example2: Arithmetical EXPS
44
2G aba
aTERMTERM aFACTORFACTOR
abaaFACTORa
Derivation of by Grammar :
Example2: Arithmetical EXPS
45
TERMEXPREXPR
2G
TERMFACTORTERMTERM
FACTORTERMaTERMa
FACTORFACTORa
FACTORba
aba
aba
Note: There is more than one derivation.
We already saw that a word may have more then a single derivation from the same grammar.
A Leftmost Derivation is a derivation in which rules are applied in order left to right.
A grammar is ambiguous if it has Two parse trees.
Ambiguity
47
Reminder: Two parse trees are equal if they are equal as trees and if all productions corresponding to inner nodes are also equal .
Ambiguity
48
Grammar :
Rules:
Example4: Similar to Arith. EXPS
49
,
,
EXPREXPREXPR
EXPRS
FACTORTERMEXPV ,,
)(,,,,, ba
4G
EXPREXPREXPRaEXPR
Note: Some ambiguous grammars may have an unambiguous equivalent grammar.
But: There exist Inherently Ambiguous Grammars , i.e. an ambiguous grammar that does not have an equivalent unambiguous one.
Ambiguity
52
Q: From a computational point of view, how strong are context free languages?
A: Since the language is not regular and it is CF, we conclude that .
Q: Can one prove ?A: Yes.
Discussion
53
0| nbaB nn
RLCFL RLCFL
Q: A language is regular if it is recognized by a DFA (or NFA). Does there exist a type of machine that characterizes CFL?
A: Yes, those are the Push-Down Automata (Next Lecture). .
Q: Can one prove a language not to be CFL ?A: Yes, by the Pumping Lemma for CFL-s . For
example: is not CFL.
Discussion
54
0| ncbaL nnn