Professional Documents
Culture Documents
P1
P 1P1
Context Free Grammar
A context free grammar is a formal
notation for expressing recursive
definitions of languages
A formal language is context free if there
is a context free grammar that generates it
CFGs are powerful enough to describe the
syntax of most programming languages
Context Free Grammar
Not all formal languages are context free
Informally, a Context Free Grammar is simply
a set of rewriting rules or productions (refer
previous example)
Context Free Grammar
A CFG G is defined by its four components
G= (V,T,P,S)
Where V: set of non-terminal variables
T: set of terminals
P: set of Production rules
S: start variable
Terminals and nonterminals
Symbols that are to be replaced are called
nonterminals,
Symbols that cannot be replaced are called
terminals
Example:
1. 01A10
2. if (expression) statement else statement
Defining CFG: an example
Example of a palindrome
G= ({P},{0,1},A, P)
Where A is defined by the set of rules
Pє
P 0
P1
P 0P0
P 1P1
Example from simple English
1. <sentence> <noun phrase> <predicate>
2. <noun phrase> <article> <noun>
3. <predicate> <verb>
4. <article> a
5. <article> the
6. <noun> cat
7. <noun> dog
8. <verb> runs
9. <verb> walks
Derivation of “the dog walks”
<sentence><noun phrase> <predicate> (P1)
<noun phrase><verb> (P3)
<article> <noun> <verb> (P2)
the <noun> <verb> (P5)
the dog <verb> (P7)
the dog walks (P9)
Language of the grammar
All strings that can be derived by applying the
production rules of a grammar belong to the
language described by that grammar
In this example
2. Derivation
Recursive Inference
Strings already known to be in the
language are taken and then use the rules
from input to start symbol
Derivations
Derivations use the production rules from
head to body, the language of the
grammar is all strings of terminals
obtained this way
Inferences
Consider the grammar
G= ( {I,E}, {0,1,a,b,+,*,(,)},P, E)
To find out the strings in the language
defined by G. We infer the following
i. Production rule 5 implies that string a is in L
ii. Production rule 6 implies that string b is in L
iii. Production rule 9 implies that string a0 is in
L (Also uses (i))
Inferring strings
String For Production Strings
inferred language of used used
i a I 5
ii b I 6
iii b0 I 9 ii
iv b00 I 9 iii
v a E 1 i
vi b00 E 1 iv
vii a+b00 E 2 v,vi
viii (a+b00) E 4 vii
ix a*(a+b00) E 3 v,viii
Leftmost Derivations
The derivation obtained by replacing the
leftmost variable by one of its production
bodies at each step, is called the leftmost
derivation
A leftmost derivation is indicated by a
lm
Derivation of string a*(a+b00)
using Leftmost derivation
E E*E I*E a*E a*(E)
lm lm lm lm lm
a*(a+I)a*(a+I0) a*(a+I00)
lm lm lm
a*(a+b00)
Rightmost Derivations
The derivation obtained by replacing the
rightmost variable by one of its production
bodies at each step, is called the rightmost
derivation
A rightmost derivation is indicated by arm
Rightmost Derivations
I*(a+b00) a*(a+b00)
rm
Language of a Grammar
If G= (V,T,P,S) is a context free grammar, the
language (CFL) of G is the set of terminal
strings that have derivations from the start
symbol
*
L(G)= {string w in T|S w}
G
Palindromes
If Lpal is the language and Gpal= ({P},{0,1},A, P)
Where A is defined by the set of rules
Pє
P 0
P1
P 0P0
P 1P1
The strings derived from these rules form a
context free language
Sentential Form of a Grammar
If G= (V,T,P,S) is a context free
grammar, then any string in (VUT)*
such that S derives is a sentential form
Class Assignment
Given production rules of a grammar are
1. S A1B
2. A 0A| є
3. B 0B|1B| є
Give leftmost derivation of strings
00101
1001
Parse trees
The derivations obtained by a CFG can
be represented using tree structure
The tree shows how the symbols of a
terminal string are grouped into sub
strings
These trees are known as “parse trees”
Parse trees
The parse trees for G are trees with following
conditions
1. Each interior node is labeled by a variable in V
2. Each leaf is labeled by a variable, or a
terminal, or an epsilon
3. If an interior node is labeled A, and its
children are labeled x1, x2, x3, ….xk
respectively, from the left, then
A x1x2x3….xk is a production in P
Example
E
E + E
I A parse tree
showing the
derivation of I+E
Example
Consider the grammar of language of
palindrome strings
Learning how to
1 P 1
1 P 1
0 P 0
0 P 0
1
Yield of a parse tree
The leaves of the parse tree, on
concatenation from left, give a string,
called the yield of the tree
This is a string derived from the root
variable
The yield is a terminal string I.e. all leaves
are labeled either with a terminal or with є
Yield of the parse tree
Example :Palindromes
Yield is 110010011
Class Assignment
Draw a parse tree for a*(a+b00)