You are on page 1of 38

CS4014: Applied Mathematics

Francisco Cantú
September 13, 2022

Grammars
Part 2
Grammar

Determines the syntactic correctness of sentences


in a language

• Sentences are strings of a language

• A sentence in Natural Language may contain


an article, noun, verb, determiner, adjective,
adverb

• Uses a rule-based approach to describe the


syntactic structure of a sentence in a language
Grammar
A Grammar is described by a set of syntactic rules
to generate strings of a language. Strings are
called sentences.

• It uses a rule-based approach to analyze sen-


tences

• A rule has the form: X → Y where X and Y


are strings

• The form and structure of both X and Y de-


termine different kinds of grammars and lan-
guages as specified by the Chomsky hierarchy.
Chomsky Hierarchy
Chomsky Hierarchy and Automata
Context-Free Grammar (CFG)
A Context-Free Grammar is a quadruple (V, Σ, P, S)
where:

• V is a finite set of V ariables

• Σ is the alphabet which contains a fine set of


T erminal symbols.

• P is a finite set of rules

• S is a distinguished element of V called the


Start symbol.

The symbols V and Σ are assumed to be disjoint.


Elements of a CFG
We define the following elements of grammars:

• A rule is written A → w where A ∈ V and


w ∈ (V ∪ Σ)∗. This is called an ”A rule”.

• λ may occur in the right-hand side, so the rule


A → λ is called the null rule or the λ rule

• T erminal symbols are represented by lower case-


letter occurring at the beginning of the alpha-
bet a, b, c, ...

• Lowercase letters p, q, u, v, w, x, y, z, ... represent


elements of (V ∪ Σ)∗

• V ariables are denoted by capital letters A, B, C, S, ...


and are called N onterminal symbols of the gram-
mar.
Rules

• A → w, where A ∈ V and w ∈ (V Σ)∗ is a A


S

rule

• The null string λ is in (V Σ)∗ so may occur


S

in the right-hand side of the rule.

• A → λ is λ-rule
Some Rules

• A → aAb

• A → aA

• A → Ab

• A → AA
Interpretation of Rules

• A → aAb
Generates any number of as followed by the
same number of bs. Example left-right paren-
theses

• A → aAbb
Generates any number of as followed by the
twice the number of bs.

• A → aA
Generates any number of as preceding the vari-
able A.

• A → Ab Generates any number of bs following


the variable A.

• A → AA
Produces an additional A each time.
Exercise

if Σ = {a, b} and

∗ ∗ S∞
Σ = {a, b} = i=0{a, b}i

What rules generate Σ∗?

What is the regular expression for the grammar?


Exercise

if Σ = {a, b} and

S∞
Σ∗ = {a, b}∗ = i=0 {a, b}i

What rules generate Σ∗?

S → aS
S → bS
S →a
S →b

What is the regular expression for the grammar?

(a + b)∗
Derivation

• A string w is derivable from v if there is a finite


sequence of rule applications that transform v
to w.

• That is: v ⇒ w1, ⇒ w2, ⇒ wn ⇒ w

• The derivability of w from v is represented by


v ⇒∗ w
Types of Recursive Derivation
The use of recursion is necessary for a finite set
of rules to generate strings of arbitrary length and
languages with infinitely many strings

• The rule A → uAv is recursive since it defines


the variable A in terms of itself

• The rule A → Av is left recursive

• The rule A → uA is right recursive


Example of Derivation Tree in CFG
A grammar G that generates the language con-
sisting of strings with a positive even number of
as

• G = (V, Σ, P, S)

• V = {S, A}

• Σ = {a, b}

• P: S → AA
A → AAA
A → bA
A → Ab
A→a
These four rules are written as:
A → AAA|bA|Ab|a
Derivations of the string ababaa
Grammars Define Languages
Let G = (V, Σ, P, S) be a context-free grammar

• A string w ∈ (V ∪ Σ)∗ is a sentential form of G


if there is a derivation S ⇒∗ w in G

• A string w ∈ Σ∗ is a sentence of G if there is


a derivation S ⇒∗ w in G

• The language of G, denoted L(G), is the set


{w ∈ Σ∗|S ⇒∗ w}
Example 1 of CFG

What is the language recognized by the follow-


ing grammar:

• S → aSa | aBa

• B → bB | b
It recognizes elements of the language

L(G) = {anbman|n > 0, m > 0}


Example 2 of CFG
What is the language recognized by the following
grammar:

• S → aSdd | A

• A → bAc | bc
It recognizes elements of the language

L(G) = {anbmcmd(2n)|n ≥ 0, m > 0}


Example 3 of CFG

What is the language recognized by the follow-


ing grammar:

• S → aSb | aSbb

• S→λ
It recognizes elements of the language

L(G) = {anbm|0 ≤ n ≤ m ≤ 2n}


Example 4 of CFG
What is the language recognized by the following
grammar:

• S → abScB | λ

• B → bB | b
The S rule generates equal number of abs and
cBs. The B Rule generates b+

The language of the grammar is the set

L(G) = {(ab)n(cbmn )n|n ≥ 0, mn > 0}


Example 5 of CFG
What is the language recognized by the following
grammar:

• S → AbAbA

• A → aA | λ
The grammar generates strings over {a, b} that
contain exactly two bs

The language of the grammar is:


L(G) = a∗ba∗ba∗
Example 6 of CFG
What is the language recognized by the following
grammar:

• S → AbAbA

• A → aA | bA | λ
The grammar generates strings over {a, b} that
contain at least two bs

The language of the grammar is:


L(G) = a∗bb∗a∗bb ∗ a∗
Example 7 of CFG
What is the language recognized by the following
grammar:

• S → aB |bA | λ

• A → aC | bS

• B → aS | bC

• C → aA | bB
The grammar generates strings over {a, b} that
contain an even number of as and an even number
of bs
More on Derivation Trees
Derivation Trees
Let G = (V, Σ, P, S) be a context-free grammar
and let S ⇒ ∗ w be a derivation of in G. The
derivation tree DT of S ⇒ ∗ w is an ordered tree
that can be built interatively as follows:

• Initialize DT with root S

• if A → x1x2...xn with x ∈ (V Σ) is the rule in


S

the derivation applied to the string uAv, then


add x1, x2, ..., xn as the children of A in the tree

• If A → λ is the rule in the derivation applied to


the string uAv, the add λ as the only child of
A in the tree
Recursive Definition of Derivation
Let G = (V, Σ, P, S) be a context-free grammar
and v ∈ (V Σ)∗. The set of strings derivable
S

from v is defined recursively as follows:

• Basis: v is derivable from v

• Recursive step: If u = xAy is derivable from v


and A → w ∈ P , then xwy is derivable from v.

• Closure: A string is derivable from v only if it


can be generated from v by a finite number of
applications of the recursive step

• Notice that rule definition uses → whereas deriv-


ability uses ⇒
How to recognize CFG
Let G be a grammar. Then,

If the rules of a grammar are of the form A → w


where A ∈ V and w ∈ (V ∪ Σ)∗, then G is said to
be a Context − f ree Grammar.
Context

• The application of the rule A → w to the vari-


able A in yAv produces the string uwv

• This is denoted uAv ⇒ uwv

• The prefix u and the suffix v defines the con-


text in which the variable A occurs
Types of Grammars: CSG
Let G be a grammar. Then, If the rules of a gram-
mar are of the form u → v where u, v ∈ (V ∪ Σ)+
and length(u) ≤ length(v) then G is said to be a
Context − sensitive Grammar.

The application of A → w to the variable A in


uAv produces the string uwv. The prefix u and
the postfix v define the context in which the vari-
able A occurs.

If the restriction on the length of u and v is


not imposed, then G is said to be an U nrestricted
Grammar.
References

• Thomas A. Sudkamp (2008). Languages and


Machines: An Introduction to the Theory of
Computer Science. Third edition. Pearson
Education, Inc., Chapter 3

You might also like