Context Free Grammar & Parser

You might also like

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

Context Free Grammar

& Parser
Context Free Grammar
Declaration Statement

S  D V ;
D int | float | char
V  V , id | id
Simple If Statement

S  If ( Cond) { Stat }
Cond  id Relop id
Relop  < | > | <= | >=
Stat  id = Exp
Exp  Exp + Exp | Exp – Exp | Exp * Exp | id
Simple If Statement with else block

S  If ( Cond) { Stat } else { Stat }


Cond  id Relop id
Relop  < | > | <= | >=
Stat  id = Exp
Exp  Exp + Exp | Exp – Exp | Exp * Exp | id
While Loop

S While(Cond) { Stat ; }


Cond  id Relop id
Relop  < | > | <= | >=
Stat  id = Exp
Exp  Exp + Exp | Exp – Exp | Exp * Exp | id
Parser
Top Down Parsers
Top down parser start with start symbol or root and
moves towards leaf nodes or terminal symbols, till the
valid string or goal is achieved.

Bottom up Parsers
Bottom up parser start with leaf nodes or terminal
symbols, moves upwards till the root node or start
symbol is explored.
Example of Top Down Parser
a) Recursive Descent Parser
b) Table Driven or Predictive Parser

Example of Bottom up Parser


c) SLR Parse
d) Canonical SLR Parser
e) LALR parser
f) Operator Precedence Parser
Simple Parsing Process
S  D V ;
D int | Float | Char
V  V, id | id

Top Down Parsing


=>S
=>D V
=>int V
=> int id
=>S
=>D V ;
=>int V ;
=> int id , V ;
=> int id , id , V ;
=> int id , id , id , V ;
=> int id, id, id, id ;
=> int a, b, c, d ;
Bottom up parsing

int a, b, c;
 D a, b, c ;
D id , id, id ;
D id, id, id ;
D V, id, id ;
D V , id ;
D V => S ( start symbol or root )

You might also like