0% found this document useful (0 votes)
24 views16 pages

Lesson 10 - Formal Language S

The document discusses formal languages and grammars, including defining phrase structure grammars, context-sensitive grammars, context-free grammars, and regular grammars. It provides examples of each type of grammar and how to derive languages from grammars.

Uploaded by

2022455636
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views16 pages

Lesson 10 - Formal Language S

The document discusses formal languages and grammars, including defining phrase structure grammars, context-sensitive grammars, context-free grammars, and regular grammars. It provides examples of each type of grammar and how to derive languages from grammars.

Uploaded by

2022455636
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

FORMAL LANGUAGE

NATURAL VS FORMAL LANGUAGE

 Natural language – spoken language such as Bahasa Melayu,


English, Spanish etc..
 Very complicated to specify all the rules of syntax for all
natural languages.
 Very difficult to use natural language to communicate with
the computer
 Formal language – model the natural language to enable
human to communicate with the computer
 Has well-defined set of rules of syntax
GRAMMAR
 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
FORMAL GRAMMAR

 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 (PSG)
G = {V, T, S, P}
Phrase Structure Grammar (PSG)

G = {V, T, S, P}
V = vocabulary
T = terminal
S = start symbol
P = production
Vocabulary

A vocabulary (or alphabet) V is a finite,


nonempty set of elements called symbols. A
word (or sentence) over V is a string of
finite length of elements of V.
 Contains terminals and non terminals
elements.
 Terminals are represented in small letters
whereas non terminals are represented by
capital letters
Terminals

Terminals are some of the elements


of the vocabulary which cannot be
replaced by other symbols.
Represented in small letters
Production

The rules that specify when we can replace a


string from V∗, the set of all strings of
elements in the vocabulary, with another
string are called the productions of the
grammar.
Order of Production
V T S P
Let G = ({S, A, B, a, b}, {a, b}, S, {S → AB, A → aAa | Ɛ, B → Bb | Ɛ}) T is a subset of V
 In each step the leftmost variable in the string is replaced
 e.g. S ⇒ AB ⇒ aAaB ⇒ aaB ⇒ aaBb ⇒ aab
 In each step the rightmost variable in the string is replaced
 e.g. S ⇒ AB ⇒ ABb ⇒ Ab ⇒ aAab ⇒ aab
 A grammar is ambiguous if there exist two or more distinct left-most (or
right-most) derivations for a string w (i.e., two distinct derivation trees for w)
 e.g. Grammar with productions {S → aSb | SS | Ɛ}
 S ⇒ aSb ⇒ aaSbb ⇒ aabb
 S ⇒ SS ⇒ S ⇒ aSb ⇒ aaSbb ⇒ aabb
Phrase Structure Grammar (PSG)

Let G be the grammar with vocabulary V = {S, A, a, b}, set of


terminals T = {a, b}, starting symbol S, and productions
P = {S → aA, S → b, A → aa}. What is L(G), the language of
this grammar?
S ⇒ aA ⇒ aaa
S⇒b
type: 2
L(G) = {b, aaa}
Order of Production
 Another way to show derivation is by using a derivation tree
Let G be the grammar with vocabulary V = {S, A, a, b}, set of terminals T = {a,
b}, starting symbol S, and productions P = {S → aA, S → b, A → aa}. What is L(G),
the language of this grammar?

L(G) = {b, aaa}


Type 1 Context Sensitive / Type 1
 A type 1 grammar can have productions of the form αAβ → αγβ, where;
 A∈V
 α, β ∈ (V + T)* The left-hand sides and right-hand
 γ ∈ (V + T)+ sides of any production rules may
be surrounded by a context of
 |αAβ| <= |αγβ| terminal and nonterminal symbols
(cardinality)
 Eg:
aAb → abbb
aA → abb
 context-sensitive because the derivation must be surrounded by the
strings α and β.
 A language generated by a type 1 grammar is called a context-sensitive
language.
Type 2 Context Free / Type 2

 A type 2 grammar can have productions only of the form A → α, where


 A∈V lhs rhs
 Α ∈ (V + T)*
 Type 2 grammars are called context-free grammars because a
nonterminal symbol that is the left side of a production can be
replaced in a string whenever it occurs, no matter what else is in the
string.
 A language generated by a type 2 grammar is called a context-free
language Steps:
 Eg: Generate a string by applying rules
S → AB –Start with the initial symbol
A→a –Repeat:
B→b •Pick any non-terminal in the string
•Replace that non-terminal with the right-hand side of some rule that
has that non-terminal as a left-hand side
•Repeat until all elements in the string are terminals
Type 3 Regular
 A type 3 grammar can have productions only of the form
 A → aB
 A→a
 A→ε
for A, B ∈ V, a ∈ Σ*, and ε the empty string
 Eg:
S → aS|b
S→ ε
Types of Grammar
Let G be the grammar with V = {S, a, b, c}; T = {a, b, c}; starting symbol S; and
productions S → abS, S → bcS, S → bbS, S → a, and S → cb.
 Construct derivation trees for
a) bcbba.
b) bbbcbba.
c) bcabbbbbcb.

You might also like