17 Context Free Languages With Examples
17 Context Free Languages With Examples
V→w
where V is a single nonterminal symbol, and w is a string of terminals
and/or nonterminals (possibly empty). The term "context-free" expresses
the fact that nonterminals can be rewritten without regard to the context
in which they occur. A formal language is context-free if some context-
free grammar generates it.
(Ref. http://en.wikipedia.org/wiki/Content_free_grammar )
2
context-sensitive grammar (CSG)
is a grammar where each production has the form
wAx → wyx,
where w and x are strings of terminals and nonterminals
and y is also a string of terminals.
In other words, the productions give rules saying "if you
see A in a given context, you may replace A by the
string y." It's an unfortunate that these grammars are
called "context-sensitive grammars" because it means
that "context-free" and "context-sensitive" are not
opposites, and it means that there are certain classes of
grammars that arguably take a lot of contextual
information into account but aren't formally considered
to be context-sensitive.
3
context-sensitive grammar (CSG)
A context-sensitive grammar (CSG) is a formal grammar in which the left-
hand sides and right-hand sides of any production rules may be
surrounded by a context of terminal and nonterminal symbols. Context-
sensitive grammars are more general than context-free grammars but still
orderly enough to be parsed by a linear bounded automaton.
(Ref. http://en.wikipedia.org/wiki/Context-sensitive_grammar )
4
Recursively enumerable language
In mathematics, logic and computer science, a formal language is
called recursively enumerable (also recognizable, partially
decidable, semidecidable, Turing-acceptable or Turing-
recognizable) if it is a recursively enumerable subset in the set of all
possible words over the alphabet of the language, i.e., if there exists a
Turing machine which will enumerate all valid strings of the language.
(Ref. https://en.wikipedia.org/wiki/Recursively_enumerable_language )
5
Some Application Area of Context
Free Languages
6
Context-Free Languages
n n R
{a b : n 0} {ww }
Regular Languages
a *b * ( a b) *
7
Context-Free Languages
Context-Free Pushdown
Grammars Automata
stack
automaton
8
Context-Free Grammars
9
Context-Free Grammar
(CFG) is a generator of the
Context-free language
(CFL)
&
A recognizer [Push down
automata (PDA)] will be an
acceptor
10
Grammars
Grammars express languages
predicate verb
11
article a
article the
noun cat
noun dog
verb runs
verb sleeps
12
Derivation of string “the dog sleeps”:
13
Derivation of string “a cat runs”:
14
Language of the grammar:
L = { “a cat runs”,
“a cat sleeps”,
“the cat runs”,
“the cat sleeps”,
“a dog runs”,
“a dog sleeps”,
“the dog runs”,
“the dog sleeps” }
15
Productions Sequence of
Terminals (symbols)
noun cat
16
Another Example
Sequence of
terminals and variables
Grammar: S aSb
S
17
Grammar: S aSb
S
Derivation of string ab :
S aSb ab
S aSb S
18
Grammar: S aSb
S
S aSb S
19
Grammar: S aSb
S
Other derivations:
n n
L {a b : n 0}
21
A Convenient Notation
*
We write: S aaabbb
for zero or more derivation steps
Instead of:
22
*
In general we write: w1 wn
If: w1 w2 w3 wn
*
Trivially: w w
23
Example Grammar Possible Derivations
S aSb *
S
S *
S ab
*
S aaabbb
S aaSbb aaaaaSbbbb
b
24
Another convenient notation:
S aSb
S aSb |
S
article a
article a | the
article the
25
Formal Definitions
Grammar: G V , T , S , P
Set of
variables
Set of Start Set of
terminals variable productions
26
Context-Free Grammar: G (V , T , S , P )
A s
Variable String of
variables and
terminals
27
Context-Free Grammar: G (V , T , S , P )
Consider the following example grammar
with 5 productions:
1. S AB 2. A aaA 4. B Bb
3. A 5. B
28
Example of Context-Free Grammar
S aSb |
productions
P {S aSb, S }
G V , T , S , P
V {S }
T {a, b} start variable
variables
terminals
29
Language of a Grammar:
For a grammarG S
with start variable
*
L(G ) {w : S w, w T *}
String of terminals or
30
Example:
n n
L(G ) {a b : n 0}
32
Example:
n n
L {a b : n 0}
is a context-free language
since context-free grammarG :
S aSb |
generates L(G ) L
33
Another Example
Context-free grammar G :
S aSa | bSb |
Example derivations:
S aSa abSba abba
S aSa abSba abaSaba abaaba
R
L(G ) {ww : w {a, b}*}
Palindromes of even length
34
Another Example
Context-free grammar G :
S aSb | SS |
Example derivations:
S SS aSbS abS ab
S SS aSbS abS abaSb abab
36
Derivation Order
1. S AB 2. A aaA 4. B Bb
3. A 5. B
37
1. S AB 2. A aaA 4. B Bb
3. A 5. B
1 2 3 4 5
S AB aaAB aaB aaBb aab
1 4 5 2 3
S AB ABb Ab aaAb aab
At each step, we substitute the
rightmost variable
39
1. S AB 2. A aaA 4. B Bb
3. A 5. B
S AB A aaA | B Bb |
41
S AB A aaA | B Bb |
S AB
S
A B
yield AB
42
S AB A aaA | B Bb |
S AB aaAB
S
A B
yield aaAB
a a A
43
S AB A aaA | B Bb |
S AB aaAB aaABb
S
A B
a a A B b
yield aaABb
44
S AB A aaA | B Bb |
S AB aaAB aaABb aaBb
S
A B
a a A B b
yield
aaBb aaBb
45
S AB A aaA | B Bb |
S AB aaAB aaABb aaBb aab
Derivation Tree S
(parse tree)
A B
a a A B b
yield
aab aab
46
Sometimes, derivation order doesn’t matter
Leftmost derivation:
S AB aaAB aaB aaBb aab
Rightmost derivation:
S AB ABb Ab aaAb aab
S
Give same A B
derivation tree
a a A B b
47
Ambiguity
48
Grammar for mathematical expressions
E E E | E E | (E) | a
Example strings:
(a a) a (a a (a a))
49
E E E | E E | (E) | a
E E E aE aEE
E
a a E a a*a
E E
A leftmost derivation
for a a a
a E E
a a
50
E E E | E E | (E) | a
a a
51
E E E | E E | (E) | a
E E E E
a E E E E a
a a a a
52
take a 2
a a a 2 2 2
E E
E E E E
2 E E E E 2
2 2 2 2
53
Good Tree Bad Tree
2 2 2 6 2 2 2 8
6 Compute expression result 8
E using the tree E
2 4 4 2
E E E E
2 2 2 2
2 E E E E 2
2 2 2 2
54
Two different derivation trees
may cause problems in applications which
use the derivation trees:
• Evaluating expressions
• In general, in compilers
for programming languages
55
Ambiguous Grammar:
A context-free grammar G is ambiguous
if there is a string w L(G ) which has:
E E E E
a E E E E a
a a a a
57
E E E | E E | (E) | a
his grammar is ambiguous also because
string a a a has two leftmost derivations
E E E aE aEE
a a E a a*a
Variables Terminals
61
A successful example:
Equivalent
Ambiguous
Non-Ambiguous
Grammar
Grammar
E E E
E E T |T
E E E
T T F | F
E (E )
E a F (E ) | a
F F a
Unique
derivation tree
for a a a a a
63
An un-successful example:
n n m n m m
L {a b c } {a b c }
n, m 0
L is inherently ambiguous:
every grammar that generates this
language is ambiguous
64
Example (ambiguous) grammar forL :
n n m n m m
L {a b c } {a b c }
S S1 | S 2 S1 S1c | A S 2 aS2 | B
A aAb | B bBc |
65
The string a nb nc n L
has always two different derivation trees
(for any grammar)
For example
S S
S1 S2
S1 c a S2
66
References
• http://matt.might.net/articles/gr
ammars-bnf-ebnf
/
• https://
en.wikipedia.org/wiki/Chomsky_hierar
chy
Thank You!
74