UnitIII Formal Languages and Grammars
UnitIII Formal Languages and Grammars
• Natural languages (English, French, Portguese, etc) have very complex rules of
syntax and not necessarily well-defined.
2
Formal Language
3
Grammars
• A formal grammar G is any compact, precise mathematical definition
of a language L.
• As opposed to just a raw listing of all of the language’s legal sentences, or just
examples of them.
• A grammar implies an algorithm that would generate all legal
sentences of the language.
• Often, it takes the form of a set of recursive definitions.
• A popular way to specify a grammar recursively is to specify it as a
phrase-structure grammar.
Grammars (Semi-formal)
• Example: A grammar that generates a subset of
the English 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”:
L = { “a boy runs”,
“a boy sleeps”,
“the boy runs”,
“the boy sleeps”,
“a dog runs”,
“a dog sleeps”,
“the dog runs”,
“the dog sleeps” }
9
Production rules example:
noun → boy
noun → dog
Variable Terminal
or Production
Symbols
Non-terminal rule
Symbols of
the vocabulary
10
Phrase-Structure Grammars
Languages Machines
Type-0 TM
(Unrestricted)
Type-1 LBA
(Context Sensitive)
Type-2 PDA
(Context Free)
Type-3 FA
(Regular)
Type-0 Grammar
α≠^
ie, erasing of α is not permitted
Examples:
Type-2 Grammar (Context Free Grammars)
A -> α
where
α ∈ (VN U ∑)*
Type-3 Grammar (Regular Grammars)
A -> a or A -> aB
where
A,B ∈ VN and a ∈ ∑
Example: Construct a Regular Grammar for the following
Finite Automata
Solution:
Example:
Recursive Enumerable Languages (RE)
And
Recursive Languages (REC)
Recursive Enumerable (RE) or Type -0 Languages
Example-1 : Example-1 :
A ⇢ a, A ⇢ a,
A ⇢ aB, A ⇢ Ba,
A ⇢ ∈ A ⇢ ∈
where, where,
A and B are Variables A and B are Variables
a is terminal a is terminal
∈ is empty string ∈ is empty string
Example-2 : Example-2 :
S ⇢ B00 | S11
S ⇢ 00B | 11S
B ⇢ B0 | B1 | 0 | 1
B ⇢ 0B | 1B | 0 | 1 Where,
where, S and B are Variables,
S and B are Variables, 0 and 1 are terminals
0 and 1 are terminals