 Parsing is a process that constructs a syntactic
structure (i.e. parse tree) from the stream of
tokens.
 Breaks the data into smaller units for easy
translation
 Used to determine if input data may be derived
from the start symbol of the grammer
 Identifies lexical units in a source statement
 Classifies units into different classes e.g. id’s,
constants, reserved ids etc and enters them into
different tables
 Token contains Class code and number in class
Syntax :Code #no eg : id#10
Example
Statement a := b + I;
a := b + I ;
id#2 op#5 id#3 op#3 id#1 op#10
 Determines the statement class such as
assignment statement, if stmt etc.
 Eg: a,b : real ; and a:=b +I ;
 Determines the meaning of the expression
 Results in addition of information such as type,
length
 Determines the meaning of subtree
Stmt a:= b + I ; proceeds as
1. Type is added to tree
2. Rules of assignment indicate expression on
RHS should be evaluated first
3. Rules of addition indicate I should be converted
before addition
 A context-free grammar (CFG) G is a quadruple
(N, Σ, P, S) where
 N: a set of non-terminal symbols
 Σ: a set of terminals ( N ∩ Σ = Ǿ)
 P: a set of rules (P: N → (N U Σ)*)
 S: a start symbol.
N = {Q, F,}
Σ = {0, 1}
P = {Q → 11Q, Q→ 00F,
F → 11F, F→ ε }
S = q
to generate 110011
Q →11Q →1100F →110011F →110011ε
 They provide a precise mathematical definition
that clearly rules out certain types of language.
 The formal definition means that context free
grammars are computationally TRACTABLE--it is
possible to write a computer program which
determines whether sentences are
grammatical or not.
 They provide a convenient visual notation for
the structure of sentences (the tree).
 Goal of parser : build a derivation
 Top-down parser : build a derivation by working from
the start symbol towards the input.
 Builds parse tree from root to leaves
 Builds leftmost derivation
 Bottom-up parser : build a derivation by working
from the input back toward the start symbol
 Builds parse tree from leaves to root
 Builds reverse rightmost derivation
 Consider the grammar:
S → c A d
A → ab | a
The input string is “cad”
Given the grammar :
E → TE’
E’ → +TE’ | λ
T → FT’
T’ → *FT’ | λ
F → (E) | id
The input: id + id * id
STACK INPUT BUFFER ACTION
$ num1+num2*num3$ shift
$num1 +num2*num3$ reduc
$F +num2*num3$ reduc
$T +num2*num3$ reduc
$E +num2*num3$ shift
$E+ num2*num3$ shift
$E+num2 *num3$ reduc
$E+F *num3$ reduc
$E+T *num3$ shift
E+T* num3$ shift
E+T*num3 $ reduc
E+T*F $ reduc
E+T $ reduc
E $ accept
E -> E+T
| T
| E-T
T -> T*F
| F
| T/F
F -> (E)
| id
| -E
num
Parsing 17
 A parse tree is created from
root to leaves
 The traversal of parse trees
is a preorder traversal
 Tracing leftmost derivation
 Two types:
 Backtracking parser
 Predictive parser
 A parse tree is created from
leaves to root
 The traversal of parse trees
is a reversal of post order
traversal
 Tracing rightmost derivation
 More powerful than top-
down parsing
 LR parserBacktracking: Try different
structures and backtrack if it
does not matched the input Predictive: Guess the
structure of the parse tree
from the next input

Parsing

  • 2.
     Parsing isa process that constructs a syntactic structure (i.e. parse tree) from the stream of tokens.  Breaks the data into smaller units for easy translation  Used to determine if input data may be derived from the start symbol of the grammer
  • 4.
     Identifies lexicalunits in a source statement  Classifies units into different classes e.g. id’s, constants, reserved ids etc and enters them into different tables  Token contains Class code and number in class Syntax :Code #no eg : id#10 Example Statement a := b + I; a := b + I ; id#2 op#5 id#3 op#3 id#1 op#10
  • 5.
     Determines thestatement class such as assignment statement, if stmt etc.  Eg: a,b : real ; and a:=b +I ;
  • 6.
     Determines themeaning of the expression  Results in addition of information such as type, length  Determines the meaning of subtree
  • 7.
    Stmt a:= b+ I ; proceeds as 1. Type is added to tree 2. Rules of assignment indicate expression on RHS should be evaluated first 3. Rules of addition indicate I should be converted before addition
  • 8.
     A context-freegrammar (CFG) G is a quadruple (N, Σ, P, S) where  N: a set of non-terminal symbols  Σ: a set of terminals ( N ∩ Σ = Ǿ)  P: a set of rules (P: N → (N U Σ)*)  S: a start symbol.
  • 9.
    N = {Q,F,} Σ = {0, 1} P = {Q → 11Q, Q→ 00F, F → 11F, F→ ε } S = q to generate 110011 Q →11Q →1100F →110011F →110011ε
  • 10.
     They providea precise mathematical definition that clearly rules out certain types of language.  The formal definition means that context free grammars are computationally TRACTABLE--it is possible to write a computer program which determines whether sentences are grammatical or not.  They provide a convenient visual notation for the structure of sentences (the tree).
  • 12.
     Goal ofparser : build a derivation  Top-down parser : build a derivation by working from the start symbol towards the input.  Builds parse tree from root to leaves  Builds leftmost derivation  Bottom-up parser : build a derivation by working from the input back toward the start symbol  Builds parse tree from leaves to root  Builds reverse rightmost derivation
  • 13.
     Consider thegrammar: S → c A d A → ab | a The input string is “cad”
  • 14.
    Given the grammar: E → TE’ E’ → +TE’ | λ T → FT’ T’ → *FT’ | λ F → (E) | id The input: id + id * id
  • 16.
    STACK INPUT BUFFERACTION $ num1+num2*num3$ shift $num1 +num2*num3$ reduc $F +num2*num3$ reduc $T +num2*num3$ reduc $E +num2*num3$ shift $E+ num2*num3$ shift $E+num2 *num3$ reduc $E+F *num3$ reduc $E+T *num3$ shift E+T* num3$ shift E+T*num3 $ reduc E+T*F $ reduc E+T $ reduc E $ accept E -> E+T | T | E-T T -> T*F | F | T/F F -> (E) | id | -E num
  • 17.
    Parsing 17  Aparse tree is created from root to leaves  The traversal of parse trees is a preorder traversal  Tracing leftmost derivation  Two types:  Backtracking parser  Predictive parser  A parse tree is created from leaves to root  The traversal of parse trees is a reversal of post order traversal  Tracing rightmost derivation  More powerful than top- down parsing  LR parserBacktracking: Try different structures and backtrack if it does not matched the input Predictive: Guess the structure of the parse tree from the next input