Download presentation
Presentation is loading. Please wait.
1
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli
2
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
3
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
4
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
5
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
6
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
7
Consider grammar : The grammar generates the language called the language of, denoted by. Context Free Grammar - Example 7
8
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
9
This is A Parse Tree of the word by : Context Free Grammar - Example 9
10
Each internal node of the tree is associated with a single production. Context Free Grammar - Example 10
11
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, and 4. is the start variable. CF Grammar – A Formal Definition 11,
12
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
13
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
14
Grammar : Rules: 1. 2. 3. Example2: Arithmetical EXPS 14
15
Derivation of by Grammar : input Example2: Arithmetical EXPS 15
16
Derivation of by Grammar : input rule Example2: Arithmetical EXPS 16
17
Derivation of by Grammar : input output rule Example2: Arithmetical EXPS 17
18
Derivation of by Grammar : input Example2: Arithmetical EXPS 18
19
Derivation of by Grammar : input rule Example2: Arithmetical EXPS 19
20
Derivation of by Grammar : input output rule Example2: Arithmetical EXPS 20
21
Derivation of by Grammar : input Example2: Arithmetical EXPS 21
22
Derivation of by Grammar : input rule Example2: Arithmetical EXPS 22
23
Derivation of by Grammar : input output rule Example2: Arithmetical EXPS 23
24
Derivation of by Grammar : input Example2: Arithmetical EXPS 24
25
Derivation of by Grammar : input rule Example2: Arithmetical EXPS 25
26
Derivation of by Grammar : input output rule Example2: Arithmetical EXPS 26
27
Derivation of by Grammar : input Example2: Arithmetical EXPS 27
28
Derivation of by Grammar : input rule Example2: Arithmetical EXPS 28
29
Derivation of by Grammar : input output rule Example2: Arithmetical EXPS 29
30
Derivation of by Grammar : input Example2: Arithmetical EXPS 30
31
Derivation of by Grammar : input rule Example2: Arithmetical EXPS 31
32
Derivation of by Grammar : input output rule Example2: Arithmetical EXPS 32
33
Derivation of by Grammar : input Example2: Arithmetical EXPS 33
34
Derivation of by Grammar : input rule Example2: Arithmetical EXPS 34
35
Derivation of by Grammar : input output rule Example2: Arithmetical EXPS 35
36
Derivation of by Grammar : input Example2: Arithmetical EXPS 36
37
Derivation of by Grammar : input rule Example2: Arithmetical EXPS 37
38
Derivation of by Grammar : output rule Example2: Arithmetical EXPS 38
39
Derivation of by Grammar : input Example2: Arithmetical EXPS 39
40
Derivation of by Grammar : input rule Example2: Arithmetical EXPS 40
41
Derivation of by Grammar : output rule Example2: Arithmetical EXPS 41
42
Derivation of by Grammar : input Example2: Arithmetical EXPS 42
43
Derivation of by Grammar : input rule Example2: Arithmetical EXPS 43
44
Derivation of by Grammar : Example2: Arithmetical EXPS 44
45
Derivation of by Grammar : Example2: Arithmetical EXPS 45 Note: There is more than one derivation.
46
To be Demonstrated on the blackboard Example3: The Language of WF ( ) 46
47
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
48
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
49
Grammar : Rules: Example4: Similar to Arith. EXPS 49,,
50
Example4: 1 st Parse Tree for______ 50
51
Example4: 2 nd Parse Tree for_____ 51
52
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
53
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
54
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.