You are on page 1of 23

CS 212 LECTURE 03

PROGRAMMING LANGUAGES
GRAMMAR

• It is often convenient to specify languages in terms of


grammars. The advantage in doing so arises mainly
from the usage of a small number of rules for
describing a language with a large number of
sentences. For instance, the possibility that an English
sentence consists of a subject phrase followed by a
predicate phrase can be expressed by a grammatical
rule of the form

2
GRAMMAR

<sentence>  <noun-phrase><verb-phrase>
<noun-phrase>  <article><adjective><noun>
<noun-phrase>  <article><noun>
<verb-phrase>  <verb><adverb> | <verb>
<article>  a | the
<adjective>  large | hungry
<noun>  rabbit | mathematician
<verb>  eat | hops
<adverb>  quickly | wildly 3
GRAMMAR

• From these rules we can form valid sentences using a


series of replacements until no more rules can be used.
• Example, the sentence is “the large rabbit hops quickly”
we start with the rule :
<sentence>  <noun-phrase><verb-phrase>
 <article><adjective><noun><verb-phrase>
 the<adjective><noun><verb-phrase>
4
GRAMMAR

the large<noun> <verb-phrase>


the large rabbit <verb-phrase>
the large rabbit <verb><adverb>
the large rabbit hops <adverb>
the large rabbit hops quickly
Let try this sentence :
“a hungry mathematician eats wildly” 5
GRAMMAR
Let move to the formal definition of grammar
Definition : A grammar < ,N,P,S> consists of four parts:

1. A finite set  of terminal symbols , the alphabet of the


language, that are assembled to make up the sentences in
the language.
2. A finite set N of nonterminal symbols , each of which
represents some collection of subphrases of sentences.
6
GRAMMAR
Let move to the formal definition of grammar
Definition : A grammar < ,N,P,S> consists of four parts:

3. A finite set P of productions or rules that describe how each


nonterminal is defined in terms of terminal symbols and
nonterminals. The choice of nonterminals determines the
phrases of the language to which we ascribe meaning.
4. A distinguished nonterminal S, the start symbol , that
specifies the principal category being defined—for example,
sentence or program.
7
CLASSIFICATION OF GRAMMAR

• Chomsky classified grammars according to the structure of


their productions,suggesting four forms of particular
usefulness, calling them type 0 through type 3
• Type 0 :The most general grammars, the unrestricted
grammars , require only that at least one nonterminal occur
on the left side of a rule where α and β are strings of
symbols in and α is not the empty string, and
is a specially designated start symbol.
8
TYPE 0 : EXAMPLE

Let  = { a, b }, N = { A, B, S } , S = { S }
P = S  ABa ,
A  BB ,
B  ab ,
AB  b

9
CLASSIFICATION OF GRAMMAR

• Type 1 : When we add the restriction that the right


side contains no fewer symbols than the left side, we
get the context-sensitive grammars—for example, a
rule of the form αβ → αγβ where α,β ∈ (N U Σ)+
(i.e., α and β are strings of nonterminals and
terminals) and γ ∈ (N U Σ)+ (i.e., γ is a nonempty
string of nonterminals and terminals). 10
TYPE 1 : EXAMPLE

 = { a,b,c } , N ={ S,B,C }

P= S  aSbc
S  aBC
aB  ab
bC  bc
cb  bc

This type 1 grammar is generate string a nbncn where n  1


11
CLASSIFICATION OF GRAMMAR

•Type 2 : The context-free grammars


prescribe that the left side be a single
nonterminal producing rules of the form A
→ w where A ∈ N (i.e., A is a single
nonterminal), w ∈ (N U Σ)* (i.e., w are
strings of nonterminals and terminals) 12
TYPE 2 : EXAMPLE

 = { a,b } , N ={ S,A}

P = S  bSbb
SA
A  aA
A

This type 2 grammar is generate string 13


CLASSIFICATION OF GRAMMAR

• Type 3 : The most restrictive grammars, the regular


grammars , allow only a terminal or a terminal followed
by one nonterminal on the right side—that is, rules of the
form
• A → a - where A is a non-terminal in N and a is a
terminal in Σ
• A → aB - where A and B are in N and a is in Σ
• A →  - where A is in N and  denotes the empty string
14
, i.e. the string of length 0.
TYPE 3 : EXAMPLE

 = { a,b,c } , N ={ S,A }

P= S → aS
S → bA
A→
A → cA

This type 3 grammar is generate string a*bc*


15
DERIVATIONS

• The cfg G that generates the language consisting of


strings over ={a,b} with a even number of a
G = (,N,P,S) , N={S,A} , ={a,b}
P : S AA
A  AAA | bA | Ab | a
we want to generate string ababaa, we will write its
derivation. So, we have two types of derivation which are
leftmost and rightmost derivation. 16
DERIVATIONS
S=> AA S=> AA S=> AA
=> aA => AAAA => Aa
=> aAAA => aAAA => AAAa
=> abAAA => abAAA => AAbAa
=> abaAA => abaAA => AAbaa
=> ababAA => ababAA => AbAbaa
=> ababaA => ababaA => Ababaa
=> ababaa => ababaa => ababaa

Leftmost Left most Rightmost 17


CFG EXAMPLE

The cfg G that generates the language consisting of


strings over ={0,1}
G = (,N,P,S) , N={A,B} , ={0,1} , S={A}

P : A B | 
B  0A1

we want to generate string 000111, write its leftmost or


rightmost derivation.
18
DERIVATIONS

Leftmost derivation
A => B
=> 0A1
=> 0B1
=> 00A11
=> 00B11
=> 000A111
=> 000111
19
Some Pascal production rules
<expression>  <simple expression>
<simple expression>  <term> | <sign><term> | <simple
expression><adding operator><term>
<adding operator>  + | -
<multiplying operator>  * | / | div | mod
<term>  <factor> |
<term><multiplying operator><factor>
<factor>  <identifier> | <unsigned constant> |
(<expression>) 20
<unsigned constant>  <unsigned number>
<unsigned number>  <unsigned integer> |
<unsigned real>
<unsigned integer> 
<digit><unsigned integer> | <digit >

<identifier>  <letter><identifier tail>


<identifier tail>  <letter><identifier tail>|
<digit><identifier tail> | 
<sign>  + | -
<digit> 0 | 1 | 2 |…| 9 21
<letter>  a | b | c | … | z
Example : Uses Pascal production rules to find the leftmost
derivation that given the constant 65.
Derivation is
<expression>  <simple expression>
 <term>  <factor>
 <unsigned constant>
 <unsigned number>
 <unsigned integer>
 <digit><unsigned integer>
 6 <unsigned integer>
22
 6<digit>  65
TRY THIS

• Uses Pascal production rules to find the leftmost


derivation that given -250.
• Uses Pascal production rules to find the leftmost
derivation that given 5 * ( x + 15 )
• Uses Pascal production rules to find the leftmost
derivation that given ( (x – 10) div 2 )
• Uses Pascal production rules to find the leftmost
derivation that given – 125 /( x1 div 2 ) 23

You might also like