0% found this document useful (0 votes)
55 views32 pages

Hopefully Today's Lecture: Context Free Grammar (CFG)

Context Free Grammar (CFG) defines the rules for generating strings in a formal language. A CFG consists of terminals, non-terminals, and productions. Terminals cannot be replaced while non-terminals are replaced using productions. A context free language is the set of strings generated by a CFG. Strings can be represented using trees that show the derivation steps. Examples show how different CFGs generate languages like anbn and palindromes.

Uploaded by

Ahsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views32 pages

Hopefully Today's Lecture: Context Free Grammar (CFG)

Context Free Grammar (CFG) defines the rules for generating strings in a formal language. A CFG consists of terminals, non-terminals, and productions. Terminals cannot be replaced while non-terminals are replaced using productions. A context free language is the set of strings generated by a CFG. Strings can be represented using trees that show the derivation steps. Examples show how different CFGs generate languages like anbn and palindromes.

Uploaded by

Ahsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Hopefully Today’s Lecture

Context Free Grammar (CFG)

CFG Terminologies

Context Free Language

Trees

1
Context Free Grammar (CFG)
The earliest computers accepted no
instructions other then their own assembly
language. Every procedure, no matter how
complicated , had to be encoded in the set of
instructions, LOAD, STORE, ADD the
contents of two registers and so on. The major
problem was to display mathematical formulas
as follows
- 2
+ - 2
+ -
(8 0) (7 10) (11 10) 2
S=
2
or 2
CFG continued …
1
+9
A= 2
8 5
4+ +
21 1
3+
2
So, it was necessary to develop a way of
writing such expressions in one line of
standard typewriter symbols, so that in this
way a high level language could be invented.
Before the invention of computers, no one
would ever have dreamed of writing such
complicated formula in parentheses e.g. the
right side of formula can be written as
3
((1/2)+9)/(4+(8/21)+(5/(3+(1/2))))
CFG continued …
The high level language is converted into
assembly language codes by a program called
compiler.
The compiler that takes the user’s programs as
its inputs and prints out an equivalent program
written in assembly language.
Like spoken languages, high level languages
for computer have also, certain grammar. But
in case of computers, the grammatical rules,
don’t involve the meaning of the words.
4
CFG continued …
It can be noted that the grammatical rules
which involve the meaning of words are called
Semantics, while those don’t involve the
meaning of the words are called Syntactics.
e.g. in English language, it can not be written “
Buildings sing ”, while in computer language
one number is as good as another.
e.g. X = B + 10, X = B + 999
Following is a remark
5
Remark
In general, the rules of computer language
grammar, are all syntactic and not semantic.
A law of grammar is in reality a suggestion
for possible substitutions.

6
CFG terminologies

Terminals: The symbols that can’t be


replaced by anything are called terminals.
Non-Terminals: The symbols that must be
replaced by other things are called non-
terminals.
Productions: The grammatical rules are
often called productions.

7
CFG
CFG is a collection of the followings
1. An alphabet Σ of letters called terminals from
which the strings are formed, that will be the
words of the language.
2. A set of symbols called non-terminals, one of
which is S, stands for “start here”.
3. A finite set of productions of the form
non-terminal  finite string of terminals and /or
non-terminals.
Following is a note in this regard

8
Note
The terminals are designated by small
letters, while the non-terminals are
designated by capital letters.
There is at least one production that has the
non-terminal S as its left side.

9
Context Free Language (CFL)
The language generated by CFG is called
Context Free Language (CFL).
Example:
Σ = {a}
productions:
1. S aS
2. SɅ
Applying production (1) six times and then
production (2) once, the word aaaaaa is
generated as
10
S  aS
 aaS
 aaaS
 aaaaS
 aaaaaS
 aaaaaaS
 aaaaaaɅ
= aaaaaa
11
Example continued …

It can be observed that prod (2) generates


Ʌ, a can be generated applying prod. (1)
once and then prod. (2), aa can be
generated applying prod. (1) twice and
then prod. (2) and so on. This shows that
the grammar defines the language
expressed by a*.
12
Example
Σ = {a}
productions:
1. SSS
2. Sa
3. S Ʌ
This grammar also defines the language
expressed by a*.
Note: It is to be noted that Ʌ is considered to
be non-terminal. It has a special status. If for
a certain non-terminal N, there may be a
production N Ʌ. This simply means that N
can be deleted when it comes in the working
13
string.
Example
Σ = {a,b}
productions:
1. SX
2. SY
3. X Ʌ
4. YaY
5. YbY
6. Ya
7. Yb 14
Example continued …
All words of this language are of either X-type
or of Y-type. i.e. while generating a word the
first production used is SX or SY. The
words of X-type give only Ʌ, while the words
of Y-type are words of finite strings of a’s or
b’s or both i.e. (a+b)+. Thus the language
defined is expressed by (a+b)*.

15
Example
Σ = {a,b}
productions:
1. SaS
2. SbS
3. Sa
4. Sb
5. S Ʌ
This grammar also defines the language
expressed by (a+b)*. 16
Example
Σ = {a,b}
productions:
1. SXaaX
2. XaX
3. XbX
4. XɅ
This grammar defines the language
expressed by (a+b)*aa(a+b)*.
17
Example
Σ = {a,b}
productions:
1. S  SS
2. S  XS
3. S  Ʌ
4. S  YSY
5. X  aa
6. X  bb
7. Y  ab
8. Y  ba
This grammar generates EVEN-EVEN 18
language.
Example
Σ = {a,b}
productions:
1. S  aB
2. S  bA
3. A  a
4. A  aS
5. A  bAA
6. B  b
7. B  bS
8. B  aBB
This grammar generates the language
EQUAL(The language of strings, with 19
number of a’s equal to number of b’s).
Note
It is to be noted that if the same non-terminal
have more than one productions, it can be
written in single line e.g.
S  aS, S  bS, S  Ʌ can be written as
S  aS|bS|Ʌ
It may also be noted that the productions
S  SS|Ʌ always defines the language which
is closed w.r.t. concatenation i.e.the language
expressed by RE of type r*. It may also be
noted that the production S  SS defines the
language expressed by r+. 20
Example
Consider the following CFG Σ = {a,b}
productions:
1. S  YXY
2. Y  aY|bY|Ʌ
3. X  bbb
It can be observed that, using prod.2, Y
generates Ʌ. Y generates a. Y generates b. Y
also generates all the combinations of a and
b. thus Y generates the strings generated by
(a+b)*. It may also be observed that the
above CFG generates the language expressed
by (a+b)*bbb(a+b)*. 21
Example
• Consider the following CFG
1. S  SS|XaXaX| Ʌ
2. X  bX| Ʌ
• It can be observed that, using prod.2, X
generates Ʌ. X generates any number of b’s.
Thus X generates the strings generated by b*.
It may also be observed that the above CFG
generates the language expressed by
(b*ab*ab*)*.
22
Example
Consider the following CFG
Σ = {a,b}
productions:
S  aSa|bSb|a|b|Ʌ
The above CFG generates the language
PALINDROME. It may be noted that the
CFG
S  aSa|bSb|a|b generates the language
NON-NULLPALINDROME.

23
Example
Consider the following CFG
Σ = {a,b}
productions:
S  aSb|ab| Ʌ
It can be observed that the CFG generates the
language {anbn: n=0,1,2,3, …}. It may also be
noted that the language {anbn: n=1,2,3, …} can
be generated by the following CFG
S  aSb|ab
24
Task
Construct CFG that generates the language
L = {w є {a,b}*: length(w) >= 2 and second
letter of w from right is a}

25
Example
Consider the following CFG
(1) S  aXb|bXa (2) X  aX|bX|Ʌ
The above CFG generates the language of
strings, defined over Σ ={a,b}, beginning and
ending in different letters.

26
Task
Construct the CFG for the language of strings,
defined over Σ ={a,b}, beginning and ending
in same letters.

27
Trees
As in English language any sentence can be
expressed by parse tree, so any word generated
by the given CFG can also be expressed by the
parse tree, e.g.
consider the following CFG
S  AA
A  AAA|bA|Ab|a
Obviously, baab can be generated by the above
CFG. To express the word baab as a parse tree,
start with S. Replace S by the string AA, of
nonterminals, drawing the downward lines
from S to each character of this string as 28
follows
Trees continued …
S

A A
Now let the left A be replaced by bA and the
right one by Ab then the tree will be
S

A A

b AA b 29
Trees continued …
Replacing both A’s by a, the above tree will
be
S

A A

b AA b

a a 30
Trees continued …
Thus the word baab is generated. The above
tree to generate the word baab is called
Syntax tree or Generation tree or
Derivation tree as well.

31
Lecture Summing Up
Context Free Grammar, Terminals, Non-
terminals, Productions, CFG, Context Free
Language, Trees, examples.

32

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy