Cits2211lectures Grammars PDF
Cits2211lectures Grammars PDF
Cits2211lectures Grammars PDF
Formal Grammar
Chomsky hierarchy
October 6, 2014
Introduction
Formal Grammar
Chomsky hierarchy
Introduction
Formal Grammar
Chomsky hierarchy
For example, English, German and Java are all languages some
strings are legal , and hence in the language, while some strings
are not in the language.
The walrus talks loudly
Loudly walrus talks the
for (int i=0; i<10; i++) { j=j+i; }
for System.out.(int i)<10; ++ }
I have arrived
Ich bin angekommen (literally, I am in come-d)
Introduction
Formal Grammar
Chomsky hierarchy
Introduction
Formal Grammar
Chomsky hierarchy
Grammars (informal)
A grammar is a set of rules that describe how to form legal strings
in the language.
For English we have the following loose rule
sentence noun-phrase verb-phrase
which we interpret as saying
A valid sentence consists of a noun-phrase followed by a
verb-phrase
To complete the grammar, we then need to define noun-phrase,
verb-phrase and so on, which are defined in the same way
noun-phrase article noun
verb-phrase verb adverb
Introduction
Formal Grammar
Chomsky hierarchy
Terminals
Introduction
Formal Grammar
Chomsky hierarchy
Introduction
Formal Grammar
Chomsky hierarchy
Formal Grammars
V is some alphabet
Introduction
Formal Grammar
Example
Suppose that
V = {I , L, D, a, b, . . . , z, 0, 1, . . . 9}
VT = {a, b, . . . , z, 0, 1, . . . 9}
S =I
and the productions are as follows
I L
I IL
I ID
L a...L z
D 0...D 9
Example words: a42, doctor , r 2d2
Chomsky hierarchy
Introduction
Formal Grammar
Chomsky hierarchy
Derivations in a grammar
Let G be a grammar with alphabet V , and let w1 and w2 be two
words over V .
Then we say that w1 directly generates w2 if there is a production
such that
1 w contains an instance of
1
2 w is obtained from w by replacing this instance of with
2
1
If w1 directly generates w2 , then we denote this by
w1 w2
If there is a sequence,
w1 w2 w3 wn
then we say that
w1 wn
Introduction
Formal Grammar
Chomsky hierarchy
Example derivations
I za0
As the word za0 consists entirely of terminals, there are no further
productions that can be applied to it.
We say that za0 is one of the words in the language generated by
the grammar.
Introduction
Formal Grammar
Chomsky hierarchy
L(G ) = {w VT | S w }
We now have two different approaches to formal languages:
Recognition is the act of determining whether or not a word is
contained in a given language.
Generation is the process of applying rules to generate words
guaranteed to be contained in a language.
Introduction
Formal Grammar
Chomsky hierarchy
The alphabet is
V = {0, A, B, E , F , W , X , Y , Z , S}
0Z Z 0
WBZ EB
F
A
Introduction
Formal Grammar
Chomsky hierarchy
Introduction
Formal Grammar
Chomsky hierarchy
Types of Grammar
Context-Sensitive (Type 1)
A grammar is context-sensitive if it obeys the erasing convention
and for every non-erasing production , the word is at least
as long as .
Almost any language one can think of is context-sensitive
Example: S aSBc | abc, cB Bc, bB bb
Introduction
Formal Grammar
Chomsky hierarchy
Types of Grammar
Context-Sensitive (Type 1)
A grammar is context-sensitive if it obeys the erasing convention
and for every non-erasing production , the word is at least
as long as .
Almost any language one can think of is context-sensitive
Example: S aSBc | abc, cB Bc, bB bb
One derivation is: S aSBc aabcBc aabBcc aabbcc and
more generally, any string an b n c n for n 1
Introduction
Formal Grammar
Chomsky hierarchy
Context-Free (Type 2)
A grammar is context-free if it obeys the erasing convention and
for every production , is a single non-terminal symbol.
Example: S SS | (S) | () and S aSb, S ab
Introduction
Formal Grammar
Chomsky hierarchy
Context-Free (Type 2)
A grammar is context-free if it obeys the erasing convention and
for every production , is a single non-terminal symbol.
Example: S SS | (S) | () and S aSb, S ab
One derivation is: S SS SSS (S)SS (())()() That is,
expressions with balanced parentheses. While the second grammar
generates an b n .
Introduction
Formal Grammar
Chomsky hierarchy
Regular (Type 3)
A grammar is regular if it obeys the erasing convention and for
every production , is a single nonterminal and is of the
form t or tW where t is a single terminal symbol and W is a
non-terminal symbol.
Example: S 0S, S 1S and S
Introduction
Formal Grammar
Chomsky hierarchy
Regular (Type 3)
A grammar is regular if it obeys the erasing convention and for
every production , is a single nonterminal and is of the
form t or tW where t is a single terminal symbol and W is a
non-terminal symbol.
Example: S 0S, S 1S and S
Some derivations are: 00, 1101, 0010101111, 111111, or more
generally, (0 + 1)
Introduction
Formal Grammar
Chomsky hierarchy
Introduction
Formal Grammar
Chomsky hierarchy
Introduction
Formal Grammar
Chomsky hierarchy
Introduction
Formal Grammar
Chomsky hierarchy
Introduction
Formal Grammar
Chomsky hierarchy
|vxy | p,
|vy | 1, and
n : N. uv n xy n z L
Introduction
Formal Grammar
Chomsky hierarchy
A context-free grammar
Introduction
Formal Grammar
Chomsky hierarchy
Introduction
Formal Grammar
Chomsky hierarchy
Backus-Naur form