Date post: | 16-Apr-2017 |
Category: |
Education |
Upload: | jayashri-kolekar |
View: | 101 times |
Download: | 0 times |
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 #noeg : id#10ExampleStatement a := b + I;
a := b + I ;id#2op#5 id#3op#3 id#1op#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 as1. Type is added to tree2. 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$ shiftE+T* num3$ shiftE+T*num3$ reducE+T*F $ reducE+T $ reducE $ accept
E -> E+T | T | E-TT -> T*F | F | T/FF -> (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