Grammars
Definition Definition (I UE)
*
.
18 A
context-free grammar or CFG is
given by the following : 19 : A string e is a string generated by grammar G if there is a
also called the object alphabet Sequence of strings
· an alphabet I of terminal symbol ,
.
S =
Xo = X, =... = Xn X
non-terminal symbol the elements
=
·
an
alphabet""of ,
of which are also referred to as
auxiliary characters , placeholders in some books variables, with"" called the start
of G
or
,
step the application of
,
is obtained by
such that each Xi = Vit1 one
symbol .
rules to
production a non-terminal occurring in Xi as
follows
·
a
special non-terminal Symbol Set called the start symbol
that there production rule R Y
.
Let RE
is a <
: : occur in Xi and assume
production rules of the form R where RE , non-terminal
· a
finite get of > X is a
the
Then is obtained from Zi by replacing one occurrence of R in Xi by
(EUE)
ite
and non-terminal
*
Symbol and Xe is an arbitrary string of terminal
String Y
Symbols which ,
can be read as 'R can be replaced by X:
The language generated by grammar G is the set of all strings over which are
generated by G .
I
L xample :
asbe
5 :
Example : DFA to Grammar
S xaSb xaaSbbxaaabb
i in
· = aabb
b
· S E >
anabbb
· SxaSb -aaSbbxaaaSbbbxaaaEbbb =
S W
L =
LahbIneIN] non-regular language S - aS bT
T > aT bUE
Recap W >
E
Grammars consist of a number of production rules telling us how to rewrite non-terminals into
strings of terminals and non-terminals .
A Context Free Language is one defined by a
grammar.
strict subset of the set of Context Free
The set of Regular Languages over an alphabet is a
Languages.
Parsing
Practical Example 1: Practical Example 2:
S - aBC S S > aT Ta
B > bB/E T >
aT bTlE
C > CIE a B C
S
s
· S XaBC XabBCxabaC xabacC abca = abc
b
B C
· S X aBC XaBc(XabBcCX abECa abc
=
a T A
E E A
-
I a T
E E
Practical Example 3
=ht
Se S S aas = an aea = ga
S + 55x S
S >
S + S
S -
5 xs
A SxS St S A
S > A
1 A A A A 2
A > ⑧
A = 1
0210 Context Free Languages
A >
2
Recap
A Parse Tree shows how string
a
may be generated from a
grammar .
An ambiguous where there strings that have multiple
parse trees
is
grammar one are