+ All Categories

Parsing

Date post: 16-Apr-2017
Category:
Upload: jayashri-kolekar
View: 101 times
Download: 0 times
Share this document with a friend
17
Transcript
Page 1: Parsing
Page 2: Parsing

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

Page 3: Parsing
Page 4: Parsing

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

Page 5: Parsing

Determines the statement class such as assignment statement, if stmt etc.

Eg: a,b : real ; and a:=b +I ;

Page 6: Parsing

Determines the meaning of the expression

Results in addition of information such as type, length

Determines the meaning of subtree

Page 7: Parsing

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

Page 8: Parsing

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.

Page 9: Parsing

N = {Q, F,}Σ = {0, 1}P = {Q → 11Q, Q→ 00F, F → 11F, F→ ε }S = q to generate 110011

Q →11Q →1100F →110011F →110011ε

Page 10: Parsing

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).

Page 11: Parsing
Page 12: Parsing

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

Page 13: Parsing

Consider the grammar: S → c A d A → ab | a

The input string is “cad”

Page 14: Parsing

Given the grammar :E → TE’E’ → +TE’ | λT → FT’T’ → *FT’ | λF → (E) | id

The input: id + id * id

Page 15: Parsing
Page 16: Parsing

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

Page 17: Parsing

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


Recommended