0% found this document useful (0 votes)
5 views

Lesson 18 Print

Uploaded by

arazzaq7789
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lesson 18 Print

Uploaded by

arazzaq7789
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Lecture # 18

 Trees(Derivation tree/syntax tree/


parse tree) (with Examples)
 Ambiguous Trees (with Examples)
 Polish notation(o-o-o) (with
Examples)
 Ambiguous CFG (with Examples)
 Total Language Tree (with Examples)
 Regular Grammar
 Word, Semiword
 TG for Regular CFG
Lecture # 18

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,
 Now let the left A be replaced by bA and the right one by Ab
then the tree will be

 Replacing both A’s by a, the above tree will be

 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.
Polish Notation
 To write the arithmetic expression, it is required to traverse
from the left side of S and going onward around the tree.
 The above notation is called operator prefix notation.
Example
 To calculate the arithmetic
expression of this tree
 It can be written as
*+*+1 2+3 4 5 6
 The above arithmetic
expression in (o-o-o) form
can
be calculated as
 *+*+1 2+3 4 5 6 = *+*3+3 4 5 6
= *+*3 7 5 6 = *+21 5 6 = *26 6 = 156.
Note

 The previous prefix arithmetic expression


can be converted into the following infix
arithmetic expression as
 *+*+1 2+3 4 5 6
= *+*+1 2 (3+4) 5 6
= *+*(1+2) (3+4) 5 6
= *(((1+2)*(3+4)) + 5) 6
= (((1+2)*(3+4)) + 5)*6
Total Language Tree

 For a given CFG, a tree with the start


symbol S as its root and whose nodes are
working strings of terminals and non-
terminals.
 The descendants of each node are all
possible results of applying every
production to the working string.
 This tree is called total language tree.
Example

 Consider the following CFG


S → aa|bX|aXX X → ab|b
then the total language tree for the given CFG may
be
 It may be observed
from the this total
language tree that
dropping the repeated
words, the language
generated by the
given CFG is
{aa, bab, bb, aabab,
aabb, abab, abb}
Example
 Consider the following CFG
S → X|b, X → aX then following will be
the total language tree of the above CFG

NOTE
It is to be noted that the only
word in this language is b.
Regular Grammar

 All regular languages can be generated by


CFGs.
 Some nonregular languages can be
generated by CFGs but not all possible
languages can be generated by CFG, e.g.
the CFG S → aSb|ab generates the
language {anbn:n=1,2,3, …}, which is
nonregular.
 It is to be noted that for every FA, there
exists a CFG that generates the language
accepted by this FA.
Example
 Consider the language L expressed by (a+b)*aa(a+b)* i.e.
the language of strings, defined over Σ ={a,b}, containing
aa.
 To construct the CFG corresponding to L, consider the FA
accepting L, as follows

 CFG corresponding to the above FA may be


S → bS|aA A → aB|bS B → aB|bB|Λ
 It may be noted that the number of terminals in above CFG is
equal to the number of states of corresponding FA where the
nonterminal S corresponds to the initial state and each
transition defines a production.
Semiword and Word

Semiword
A semiword is a string of terminals (may
be none) concatenated with exactly one
nonterminal on the right i.e. a semiword,
in general, is of the following form
(terminal)(terminal)… (terminal)
(nonterminal)
Word
A word is a string of terminals. Λ is also a
word.
Theorem
 If every production in a CFG is one of the following
forms
Nonterminal → semiword
Nonterminal → word
then the language generated by that CFG is
regular.
 A CFG is said to be a regular grammar if it
generates the regular language i.e. a CFG is said to
be a regular grammar in which each production is
one of the two forms
Nonterminal → semiword
Nonterminal → word
Example

 The CFG S → aaS|bbS|Λ is a regular


grammar.
 It may be observed that the above CFG
generates the language of strings
expressed by the RE (aa+bb)*.
 The CFG S → aA|bB, A → aS|a, B → bS|b is
a regular grammar.
 It may be observed that the above CFG
generates the language of strings
expressed by RE (aa+bb)+.
TG for Regular Grammar
 For every regular grammar there exists a
TG corresponding to the regular grammar.
 Following is the method to build a TG from
the given regular grammar
 Define the states, of the required TG, equal
in number to that of nonterminals of the
given regular grammar.
 An additional state is also defined to be the
final state.
 The initial state should correspond to the
nonterminal S.
TG for Regular Grammar continues…

 For every production of the given regular grammar,


there are two possibilities for the transitions of the
required TG.
 If the production is of the form nonterminal →
semiword, then transition of the required TG would
start from the state corresponding to the
nonterminal on the left side of the production and
would end in the state corresponding to the
nonterminal on the right side of the production,
labeled by string of terminals in semiword.
 If the production is of the form nonterminal → word,
then transition of the TG would start from the state
corresponding to nonterminal on the left side of the
production and would end on the final state of the
Example
 Consider the following CFG
S → aaS|bbS| Λ
 The TG accepting the language generated
by the above CFG is given below

 The corresponding RE may be (aa+bb)*.


Example

 Consider the following CFG


S → aA|bBA → aS|a B → bS|b
then the corresponding TG will be

 The corresponding RE may be (aa+bb)+.


Example
 Consider the following CFG
S → aaS|bbS|abX|baX|Λ
X → aaX|bbX|abS|baS,
 then the corresponding TG will be

 The corresponding language is EVEN-


EVEN.

You might also like