0% found this document useful (0 votes)
8 views

Grammars

The document discusses grammars and their role in language modeling, categorizing them into operational and generative models. It explains the structure of grammars, including their components and the Chomsky hierarchy, which classifies grammars into four types based on production forms. Additionally, it explores the relationship between general grammars and Turing machines, illustrating how each can be used to recognize or generate the languages defined by the other.

Uploaded by

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

Grammars

The document discusses grammars and their role in language modeling, categorizing them into operational and generative models. It explains the structure of grammars, including their components and the Chomsky hierarchy, which classifies grammars into four types based on production forms. Additionally, it explores the relationship between general grammars and Turing machines, illustrating how each can be used to recognize or generate the languages defined by the other.

Uploaded by

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

Grammars

Model for languages


Models suitable to Models suitable to
recognize/accept, describe how to
translate, compute generate a
languages language
• They “receive” an • Sets of rules to
input string and build phrases of a
process it language
®Operational
models ®Generative models
(Automata) (Grammars)
Recap (1)
• Generative models produce strings
• grammar (or syntax)
• A grammar is a set of rules to build the
phrases of a language
• It applies to any notion of language (in the
widest possible sense)
• A formal grammar generates strings of a
language through a process of rewriting
Recap (2)
• A grammar is a linguistic rule
• It is composed by
• a main object: initial symbol
• composing objects: nonterminal symbols
• base elements: terminal symbols
• refinement rules: productions
• Formally?
Recap (3)
• A grammar is a tuple <VN, VT, P, S>
where
• VN is the nonterminal alphabet (or
vocabulary)
• VT is the terminal alphabet (or vocabulary)
• V=VNVT
• SVN is a particular element of VN called
axiom or initial symbol
• PVN+V* is the set of rewriting rules or
productions
• A grammar G=<VN, VT, P, S> generates a
language on the alphabet V
Recap (4)
• A production is an element of V +V*
N
• This is usually denoted as <, >, where
VN+ and V*
• We generally indicates (easing of formal
notation) a production as 
•  is a sequence of non terminal symbols (at
least one)
•  is a sequence of symbols, that can be
terminal or not (potentially empty)
Recap (5)
 ( is obtained by immediate
derivation by )
• V+ and V*
if and only if
=123, =123  22P

 2 is rewritten as 2 in the context <1,


3>
Recap (6)
• Given a grammar G=<VN, VT, P, S>,
L(G)={x|xVT*  S*x}
• Informally the language generated by a
grammar G is the set of all strings
• Consisting only of terminal strings
that can be derived from S
• In any number of steps
Chomsky hierarchy (1)
• Grammars are classified according to the
form of their productions
• Chomsky classified grammars in four
types
type 0
type 1
type 2
type 3
Chomsky hierarchy (2)
• Type 3 grammars restrict productions to a
single nonterminal on the left-hand side
and a right-hand side consisting of a
single terminal, possibly followed (or
preceded, but not both in the same
grammar) by a single nonterminal
• The rule S is also allowed here if S does
not appear on the right side of any rule
• Type-2 grammars are defined by rules of
the form A where A is a nonterminal
and  is a string of terminals and
nonterminals
Chomsky hierarchy (3)
• Type-1 grammars have rules of the form
A  , where A is a nonterminal and
,  and  are strings of terminals and
nonterminals.
•  must be non empty
• The rule S is allowed if S does not appear
on the right side of any rule
• Type-0 grammars include all formal
grammars
Some natural question
• What is the practical use of grammars?
• What languages can be obtained through
grammars?
• What relation exists among automata
and grammars?
• Among languages generated by grammars
and languages accepted by automata?
• And the Chomsky hierarchy?
Some answer
• Chomsky hierarchy can be “renamed”
• Type 3 grammars: regular
• Type 2 grammars: context-free
• Type 1 grammars: context-sensitive
• Type 0 grammars: non-restricted
• Correlations
• Regular grammars – regular languages - FSA
• Context-free grammars – context-free
languages -NDPDA
• Non-restricted grammars – recursively
enumerable languages - MT
Definition
• General (also called non-restricted) grammars
are grammar without any limitation on
productions
• They correspond to type 0 in the Chomsky hierarchy
• Both context-free grammars and regular
grammars are non-restricted
general G

Context-free G

Regular G
General grammars and TM
• General grammars (GG) and TM are
equivalent formalism
• Given a GG it is possible to build a TM that
recognizes the language generated by the
grammar
• Given a TM it is possible to define a GG that
generates the language accepted by the TM
• How?
From a GG to a TM (1)
Given a general grammar G=<VN, VT, P,
S>, let us construct a NDTM M, accepting
L(G):
• M has one memory tape
• The input string x is on the input tape
• The memory tape is initialized with S (better:
Z0S)
• The memory tape in general will contain a
string a ( V*)
• It is scanned searching the left part of some
production of P
• When one is found, (not necessarily the first one)
M operates a ND choice and the chosen is
substituted by the corresponding right part (if
From a GG to a TM (2)
• Following this procedure
cs=<qs, Z0> |-*- <qs,Z0>
• If and when the tape holds a string yVT*,
it is compared with x
• If they coincide, x is accepted
• otherwise this particular move sequence
does not lead to acceptance
Remarks
• Using a NDTM facilitates the construction but it
is not necessary
• It is necessary that, if x  L(G), M might “try an
infinite number of ways”
• some of these might never terminate, without being
able (rightly) to conclude that x  L(G), but not even
the opposite
Indeed the definition of acceptance requires
that M reaches an accepting configuration if and
only if x  L, but does not requires that M
terminates its computation without accepting
(i.e., in a “rejecting state”) if x  L
• Again the complement-problem and the asymmetry
between solving a problem in the positive or negative
sense.
From TM to GG (1)
Given a single tape TM M let us build a
general grammar G, that generates L(M):
• First, G generates all strings of the type
x$X, xVT*, X being a “copy of x” composed of
nonterminal symbols (e.g., for x = aba, x$X =
aba$ABA)
• G simulates the successive configurations of M
using the string on the right of $
• G is defined in a way such that it has a derivation
x$X * x if and only if x is accepted by M.
• The base idea is to simulate each move of M by
an immediate derivation of G
From TM to GG (2)
a B A C b

is represented
through the string
q $BqAC

• G has therefore derivations of the kind x$Xx$qoX (initial


configuration of M)
• If, for M it is defined :
• d(q,A) = <q’, A’, R> G includes the production qA  A’q’
• d(q,A) = <q’, A’, S> G includes the production qA  q’ A’
• d(q,A) = <q’, A’, L> G includes the production BqA  q’ BA’
 B in the alphabet of M (recall that M is single tape, hence
it has a unique alphabet for input, memory, and output)
From TM to GG (3)
a B A C b a B A’ C b

q q’

If and only if: x$aBqACb  x$aBA’q’Cb, ecc.

• We finally add productions allowing G to derive from


x$aBqFACb a unique x if –and only if– M reaches an
accepting configuration (aBqFACb), by deleting
whatever is at the right of $, $ included.

You might also like