INFO 2950: Prof. Carla Gomes Gomes@cs - Cornell.edu
INFO 2950: Prof. Carla Gomes Gomes@cs - Cornell.edu
Module
Modeling Computation:
Languages and Grammars
Rosen, Chapter 12.1
1
Modeling Computation
Given a task:
Can it be performed by a computer?
We learned earlier the some tasks are unsolvable.
For the tasks that can be performed by a computer, how can they be
carried out?
We learned earlier the concept of an algorithm.
– A description of a computational procedure.
How can we model the computer itself, and what it is doing when it
carries out an algorithm?
Models of Computation –
we want to model the abstract process of computation itself.
We’ll cover three 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
Finite-state machines (FSM)
• FSM are characterized by a set of states, an input alphabet, and transitions that
assigns a next state to a pair of state and an input. We’ll study FSM with and
without output. They are used in language recognition (equivalent to certain
grammar)but also for other tasks such as controlling vending machines
Turing Machine – they are an abstraction of a computer; used to compute
number theoretic functions
3
Early Models of Computation
Phrase-Structure Grammars
Types of Phrase-Structure Grammars
Derivation Trees
Backus-Naur Form
Intro to Languages
Natural languages (English, French, Portguese, etc) have very complex rules of
syntax and not necessarily well-defined.
9
Formal Language
10
Grammars
predicate verb 12
article a
article the
noun boy
noun dog
verb runs
verb sleeps
13
A derivation of “the boy sleeps”:
14
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” }
16
Notation
noun boy
noun dog
Variable Terminal
or Production
rule Symbols of
Non-terminal the vocabulary
Symbols of
the vocabulary
17
Basic Terminology
► EXAMPLE:
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}
24
Language L(G)
► EXAMPLE:
Let G = (V, T, S, P), where V = {a, b, 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.
S aSb S 26
S aSb
S
Grammar:
aabb
Derivation of sentence
:
S aSb S
27
Other derivations:
S aSb
S
n n
L {a b : n 0}
29
PSG Example – English Fragment
V T
Grammar G (V , T , S , P)
a A B
B b a b B
c c
Backus-Naur Form