Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Laxmi Institute of Technology

Scanning and Parsing

CSE Department
5th Sem

Presented by : Guided BY:


Riddhi prajapati (130860131044) Mrs.Rinkal prajapati
Sonali patel (130860131038)
Contents :
 Programming language grammars
 classification of grammars
 ambiguity in grammatic specification
Scanning and Parsing
 A scanning is that part of the language
processor that performs lexical
analysis(breaks syntaxes into a series of
tokens) of the input in accordance with a
grammar
 parser is that part that performs syntax

analysis(string of symbols).
Programming language grammars
Grammar is a collection of four tuples, that are
NT(or V),T,P,S.
 NT(or V):A set of symbol are called non-
terminals or variables.

 T:A set of alphabet of letters called


terminals from which we are going to make
the statement that will be the sentences of
language.
Programming language grammars
 P:It is the finite set of production rules. A
finite set of production in LHS --> RHS from.

 S:From set of non-terminals, one of which is


the start symbol conventionally it is S. Syntax
checking process of parser either starts from
this non-terminal S, or stop when this symbol
is found.
classification of grammars
Chomsky Hierarchy:
 Chomsky had suggested four different

classes of phrase structure grammar as


follows:
1) Type 0 grammar
2) Type 1 grammar
3) Type 2 grammar
4) Type 3 grammar
Type 0 (unrestricted grammar)
 This grammar permits production of the form
α→β with α≠ϵ where α and β are sentential
form i.e. any combination of any number of
terminals and non terminals.
 For example,

Grammar G=(V, T, P, S ) where


V={S, B, C},T={A, B, C} and
P = S SB
SB  BC
Ba
Type 1 (context sensitive grammar)
 Every production rule of this grammar α→β
and length of β is greater than or equal to α
except rule S → ϵ. Starting non terminal S
should not appear on right hand side of
production rule.
 For Example,

G=(V, T, P, S ) ,V={S, B, C},T={A, B, C}


P : A AB
AB  AC
C  ab // A=Starting non terminal
Type 2 (context free grammar)
 Every production rule of this grammar is in
form A α means left hand side of
production rule contain only one non
terminal. And RHS contains terminal as well
as non terminal i.e. sentential form.
 Example,

G = ({A, B, C},{a, b}, P, A)


P : A  AB | a
B  AC | b
C  ab
Type 3 (Regular grammar)
 This grammar accepts all rules of CFG but
this grammar is divided into two types. There
are as follows:
1) Left linear grammar : Left most symbols of
RHS of production rule is non terminal.
Example,
G = ({A, B, C},{a, b}, P, A)
P : A  Ba | a
B  Cb | b
Ca|b
Context free grammar(CFG)
 Grammar is a set of formal rules, which
checks the correctness of the sentences or we
can construct the correct sentence using
these rules.
 The grammar is normally consisting of two

types of basic elements namely,


1) Terminal symbols and
2) Non-terminal symbol or auxiliary symbols or
variables.
Context free grammar(CFG)
 Terminal symbols are those which are directly
used in the statements of source language.
 When non-terminals are not used in the

statement but it is used to tell syntax of


statements using set of production rules.
 There are two aspects of the grammar,

1) Generative capacity : Using grammar


2) Grammatical constituents : The formal
definition of grammar is as follows.
Context free grammar(CFG)
Formal definition
 Context free grammar G is the collection of

four tuples.It is shown in below.


G = ( V, T, P, S )
Where,
V : It is finite set of non terminals,
T : It is finite set of terminals of input alphabet
i.e. summation
P: : It is finite set of production rule of the
form,
Non terminals  terminals + non terminals
Example
A context-free grammar G : S  aSb
S  SS
S 
A derivation:

S  SS  aSbS  abS  ab

14
Ambiguity in grammatic specification
 A string x in L(G) may have two or more parse
tree witness S * x. The grammar G is said to
be ambiguous.
 A CFG is called ambiguous if for at least one

word in the language that it generates, there


are two possible derivations :
1) Left most derivation tre
2) Right most derivation tree
Example:
E  E  E | E  E | (E) | a
a  aa

E E  E  E  a E  a EE
 a  a  E  a  a*a
E  E leftmost derivation

a E  E

a a
16
Example:
E  E  E | E  E | (E) | a
a  aa
E  EE  E  EE  a EE
 a  aE  a  aa E

E  E
rightmost derivation

E  E a

a a
17
E  E  E | E  E | (E) | a
a  aa
Two derivation trees
E E

E  E E  E

a E  E E  E a

a a a a
18
The grammar E  E  E | E  E | (E) | a
is ambiguous because both parse
trees are not same.
string a  aa has two derivation trees

E E

E  E E  E

a E  E E  E a

a a a a
19
Solution to remove Ambiguity
Let two grammar G1 and G2
G1 : A  aα | aβ (contains ambiguity)

G2 : A  aA’ ( ambiguity removed)


A’  α | β

So, applying this rule to our grammar then

S  aB | aA
A  aAB | a | b
B  Abb | b
Removing Ambiguity From Grammars
:
 To remove Ambiguity,grammar required to be
rewrite
 Example :

Grammar :S  aB | aA
A  aAB | a | b
B  Abb | b
This grammar is Ambiguous.
So, finally we have grammar with production:

S  aS’
S’  B | A
A  aA’ | b
A’  AB | Є
B  Abb | b

Now, this grammar is an ambiguous.


THANK YOU…

You might also like