Chapter 5 Context Free Languages and Grammar PDF
Chapter 5 Context Free Languages and Grammar PDF
THEORY OF COMPUTING
Chapter 5
Context-Free Grammars and
Languages
Introduction to Grammar
• A grammar is intuitively a set of rules which are used to
construct a language contained in Σ* for some alphabet
Σ.
• These rules allow us to replace symbols or strings of
symbols with other symbols or strings of symbols until
we finally have strings of symbols contained Σ in
allowing us to form an element of the language.
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
2
9 Ogada
Introduction to Grammar
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
3
9 Ogada
Formal Languages
• A formal language is abstraction of the general
characteristics of programming languages.
• A formal language consists of a set of symbols and
some rules of formation by which those symbols can
be combined into entities called sentences.
• A formal language is the set of all strings
permitted by the rules of formation.
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
4
9 Ogada
Describing Languages
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
6
9 Ogada
Describing Languages
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
7
9 Ogada
Describing Languages
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
8
9 Ogada
Context Free Grammars
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
10
9 Ogada
Context Free Grammars
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
11
9 Ogada
Context Free Grammars
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
12
9 Ogada
Context Free Grammars
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
13
9 Ogada
Context Free Grammars
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
14
9 Ogada
Context Free Grammars
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
15
9 Ogada
Context-Free Grammars
• Context-free grammars have played a central role
in compiler technology since the 1960’s; they
turned the implementation of parsers (functions
that discover the structure of a program) from a
time-consuming, ad-hoc implementation task
into a routine job that can be done in a
afternoon.
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
16
9 Ogada
Context-Free Grammars
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
18
9 Ogada
Context-free grammars
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
20
9 Ogada
Context-free grammars
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
22
9 Ogada
Grammar of Palindromes
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
23
9 Ogada
Grammar of Palindromes
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
24
9 Ogada
Grammar of Palindromes
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
27
9 Ogada
Start Symbol
• One of the variables represents the language being
defined.
• It is called the start symbol.
• Other variables represent auxiliary classes of strings
that are used to help define the language of the start
symbol.
• In the example, P, the only variable is the start Symbol.
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
28
9 Ogada
Productions or Rules
• There is a finite set of productions or rules that
represent the recursive definition of a language.
• Each production consists of:
– A variable that is being (partially) defined by the
production. This variable is often called the head of
the production.
– The production symbol →.
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
29
9 Ogada
Productions or Rules (cont)
• Each production consists of a string of zero or more
terminals and variables.
• The string, called the body of the production,
represents one way to form strings in the language of
the variable of the head. In so doing, we leave
terminals unchanged and substitute for each variable
of the body any string that is known to be in the
language of that variable.
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
30
9 Ogada
Formal Definition of CFG’s
• A context-free grammar is a quadruple
G = (V, T, P, S)
Where
V is a finite set of variables
T is a finite set of terminals
P is a finite set of productions of the form A→, where A is a
variable and (VT)*
S V is the start variable
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
31
9 Ogada
Examples
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
33
9 Ogada
Example
• A simple grammar generates strings of 0's and 1's such
that each block of 0's is followed by at least as many 1's.
– S AS |
– A 0A1 | A1 | 01
• S AS A 0A1 0A11 00111 is a derivation of
the string 00111
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
34
9 Ogada
Derivations using a Grammar
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
40
9 Ogada
Leftmost and Rightmost Derivations
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
41
9 Ogada
Language of a CFG
{w Σ* : S * w}
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
43
9 Ogada
Meaning of Context-Free
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
44
9 Ogada
Parse Tree
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
45
9 Ogada
Parse tree
• More importantly, the tree, ( called parse tree), when
used in a compiler, is the data structure of choice to
represent the source program.
• In a compiler, the tree structure of the source program
facilitates the translation of the source program into
executable code by allowing natural, recursive
functions to perform this translation process.
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
46
9 Ogada
Parse Tree
• Certain grammars allow a terminal string to have more
than one parse tree.
• This makes the grammar unsuitable for a programming
language since a compiler could not tell the structure
of certain source programs, and therefore could not
with certainty deduce what the proper executable code
for the program was.
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
47
9 Ogada
Parse Tree
• Easier way to picture derivation: parse tree
<expr>
<expr> * <expr>
<expr> + <expr> a
a a
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
48
9 Ogada
Constructing a Parse Tree
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
50
9 Ogada
Constructing Parse Tree
• Nodes = variables, terminals, or .
– Interior nodes are variables
– Leaf nodes are variables, terminals, or
– A leaf can be only if it is the only child of its parent.
• A node and its children (from left to right) must form
the head and body of a production
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
51
9 Ogada
Example
• E→E+E
• E→ I E
E + E
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
52
9 Ogada
Exercise
• E E * E I * E a * E a*(E) a*(E + E)
a * (I + E) a * (a + E) a * (a + I) a *
(a + I0) a * (a + I00) a * (a + b00).
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
53
9 Ogada
The Yield of a Parse Tree
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
54
9 Ogada
The Yield of a Parse Tree
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
55
9 Ogada
Example
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
56
9 Ogada
Equivalence of Parse Trees and
Derivations
• The following about a grammar G = (V, T, P, S) and a
terminal string w are all equivalent:
– S * w (i.e., w is in L(G))
– S * w
lm
– S *rmw
– There is a parse tree for G with root S and yield w.
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
57
9 Ogada
Applications of CFGs
• Grammars are used to describe programming
languages.
• More importantly, there is a mechanical way of
turning the language description as a CFG into a
parser, the component of the compiler that
discovers the structure of the source program
and represents that structure by a parse tree.
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
58
9 Ogada
Applications of Parse Trees
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
59
9 Ogada
Applications of Parse Trees
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
60
9 Ogada
Applications of Parse Trees
• Tags are the familiar keywords with triangular brackets,
e.g. <head> and </head> from html.
• However, XML tags deal not with the formatting of
text, but with the meaning of text.
• E.g. one could surround a sequence of characters that
was intended to be interpreted as a phone number by
<PHONE> and </PHONE>.
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
61
9 Ogada
Ambiguity
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
62
9 Ogada
Ambiguity
<expr> <expr>
a a a a
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
64
9 Ogada
Inherent Ambiguity
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
65
9 Ogada
Inherent Ambiguity
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
66
9 Ogada
Inherent Ambiguity
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
67
9 Ogada
Assignment
• The following grammar generates the language of regular
expression 0*1(0+1)*:
• S→A1B
• A →0A| є
• B →0B|1B|є
• Give the leftmost and rightmost derivations of the following strings:
a) 00101
b) 1001
c) 00011
25/06/201 ICS 2407 Theory Of Computing: Chapter 5: Context Free Grammar. Dr Kennedy
68
9 Ogada
The End