3 Types of Structures Used in Modeling Computation
3 Types of Structures Used in Modeling Computation
Grammars
• Used to generate sentences of a language and to determine if a given sentence is in a language
• Formal languages, generated by grammars, provide models for programming languages (Java, C,
etc) as well as natural language --- important for constructing compilers
• Natural languages (English, French, Portguese, etc) have very complex rules of syntax and not
necessarily well-defined.
Formal Language
• Formal language – is specified by well-defined set of rules of syntax
predicate verb
article a
article the
noun boy
noun dog
verb runs
verb sleeps
• A derivation of “the boy sleeps”:
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” }
Notation
noun boy
noun dog
Variable Terminal
or Production
Symbols of
Non-terminal rule
the vocabulary
Symbols of
the vocabulary
Basic Terminology
► A vocabulary/alphabet, V is a finite nonempty set of elements
called symbols.
• Example: V = {a, b, c, A, B, C, S}
where V = {a, b, 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}
Language L(G)
► EXAMPLE:
1. we can derive aA from using S → aA, and then derive aaa using A →
aa.
• Derivation of sentence : ab
S aSb ab
S aSb S
S aSb
• Grammar: S
aabb
• Derivation of sentence :
S aSb aaSbb aabb
S aSb S
• 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
• Language of the grammar with the productions:
S aSb
S
n n
L {a b : n 0}
G={(S,A),(a,b),S,P}
Automata
Types of Grammars - Chomsky hierarchy of languages