LanguagesandGrammars Unit 3
LanguagesandGrammars Unit 3
• CSE322
• Formal Languages and Automata Theory
1
Intro to Languages
• English grammar tells us if a given combination of words is a valid
sentence.
2
Formal Language
predicate verb 5
article a
article the
•
noun boy
noun dog
verb runs
verb sleeps
6
• A derivation of “the boy sleeps”:
7
• A derivation of “a dog runs”:
9
Notation
•
noun boy
noun dog
Variable Terminal
or Production
Symbols of
Non-terminal rule
the vocabulary
Symbols of
the vocabulary
10
Basic Terminology
► EXAMPLE:
Where V={ A, B, S}
T = {a, b},
S is a start symbol
P = {S → ABa, A → BB, B → ab, A → Bb}.
G is a Phrase-Structure Grammar.
What sentences can be generated
with this grammar?
Derivation
• Definition
• If w0, w1, …., wn are strings over V such that w0 =>w1,w1=>w2,…, wn-1 => wn,
then we say that wn is derivable from w0, and write w0=>*wn.
• L(G)= {w T* | S =>*w}
16
Language L(G)
► EXAMPLE:
•
• Let G = (V, T, S, P), where V = {, A, S}, T = {a, b}, S is a start
symbol and P = {S → aA, S → b, A → aa}.
• we can derive aA from using S → aA, and then derive aaa using
A → aa.
G=(V,T,S,P) T={a,b} P=
S aSb
S
V={S}
• Derivation of sentence :
ab
S aSb ab
S aSb S 18
• Grammar: S aSb
S
• Derivation of sentence
aabb :
S aSb aaSbb aabb
S aSb S
19
• Other derivations:
S aSb aaSbb aaaSbbb aaabbb
S aSb aaSbb aaaSbbb
aaaaSbbbb aaaabbbb
So, what’s the language of the
grammar with the productions? S aSb
S 20
• Language of the grammar with the
productions: S aSb
S
n n
L {a b : n 0}
21
Another Example
V T
31
Linear Grammars
Grammars with
at most one variable at the right side
of a production
Examples: S aSb S Ab
S A aAb
A
32
A Non-Linear Grammar
Grammar G
: S SS
S
S aSb
S bSa
L(G ) {w : na ( w) nb ( w)}
33
Another Linear Grammar
Grammar G : SA
A aB |
B Ab
n n
L(G ) {a b : n 0}
34
Left-Linear Grammars
All productions have form: A Bx
or
A x
Example: S Aab
A Aab | B
Ba
35
Right-Linear Grammars
All productions have form: A xB
or
A x
Example: S abS
S a
36
Definition: Context-Free Grammars
Grammar G (V , T , S , P )
B b a b B
c c
Generating Infinite Languages
• A simple PSG can easily generate an infinite
language.
• Example: S → 11S, S → 0 (T = {0,1}).
• The derivations are:
– S0
– S 11S 110
– S 11S 1111S 11110 L = {(11)*0} – the
– and so on… set of all strings
consisting of some
number of concaten-
ations of 11 with itself,
followed by 0.
Another example
• Construct a PSG that generates the language L =
{0n1n | nN}.
– 0 and 1 here represent symbols being concatenated n
times, not integers being raised to the nth power.
• Solution strategy: Each step of the derivation
should preserve the invariant that the number of
0’s = the number of 1’s in the template so far, and
all 0’s come before all 1’s.
• Solution: S → 0S1, S → λ.
• Context-Sensitive Languages
45
Example-01:
This grammar generates the strings having equal number of a’s and b’s.
46
Consider a grammar G = (V , T , P , S) where-
V={S,A,B,C}
T={a,b,c}
P = { S → ABC , A → a , B → b , C → c }
S={S}
L(G) = { abc }
This language consists of finite number of strings.
Therefore, language of the grammar is finite.
47
48
49
Here,
50
51
52
step by step find RLG and LLG for
regex 0∗(1(0+1))∗0∗(1(0+1))∗. At each step,
same color is used to match part of regex getting
translated into corresponding part in grammar.
53
54
55
Conversion from RG to FA
58
Solution is
59
• Conversion from regular grammar to
regular expression
Let A->aA | b
60
EX#1:
S-> 1A | 10 B
A -> 0A | 1
B -> 11B | 0
solution : A= (0)*1 , B= (11)*0
S= 1 (0)*1 +11 (11)*0
EX#2: S-> bB
B -> aB | b B | b
Solution : b.(a+b)*.b 61
Conversion from R.E to R.G
a * .b .(a+b)*
Solution is : S->aS | bA
A-> aA | b A
(Where S = q0 in FA and A is q1 in FA)
62
Ex#2: (a+ba)* b
RLG : A->aA/bB/bC
B-> aA
C->epsilon
64
Method
1. The two finite automata (FA) are said to be equivalent if both the
automata accept the same set of strings over an input set Σ.
2. When two FA’s are equivalent then, there is some string x over Σ. On
acceptance of that string, if one FA reaches to the final state, the other
FA also reaches to the final state.
• Method
• The method for comparing two FA’s is explained below −
• Let M and M1 be the two FA’s and Σ be a set of input strings.
• Step 1 − Construct a transition table that has pairwise entries (q, q 1)
where q ∈ M and q1 ∈ M1 for each input symbol.
• Step 2 − If we get in a pair as one final state and other non-final state
then we terminate the construction of transition table declaring that two
FA’s are not equivalent
• Step 3 − The construction of the transition table gets terminated when
there is no new pair appearing in the transition table.
65
66
67
68
If there is a grammar
69