BKS Unit II-I - Introduction To Parsing
BKS Unit II-I - Introduction To Parsing
Unit II
lexical token
source parser parse rest of interm
program analyzer get next tree front end repres
token
symbol
table • also technically part of
• uses a grammar to
parsing
check structure of tokens
• produces a parse tree • includes augmenting
• syntactic errors and info on tokens in source,
recovery type checking, semantic
• recognize correct
analysis
syntax of tokens
• report errors
Learn Compiler Design: From B. K. Sharma
To Parse Means
To parse a word or a sentence means to
break it into parts that conform to a given
grammar.
What is Parsing?
Parsing is a process to determine if a string
of tokens can be generated by a grammar.
What is Parsing?
Input String Parsing Derivation
CFG
Parsing
Derivation
Input String S
Parsing S cAd
cad S cad
CFG Production: S c A d
Production: A a
G S c A d
A ab | a
Learn Compiler Design: From B. K. Sharma
Parsing
Derivation
Stream of Tokens
E
Parsing E + E
Id + Id * Id
Id + E
CFG Id + E * E
Id + Id * E
Id + Id * Id
E E * E Production: E E + E
G | E + E Production: E Id
| Id Production: E E * E
Production: E Id
Production: E Id
Learn Compiler Design: From B. K. Sharma
Parsing
Input String
Stream of
Parsing Derivation
Tokens
CFG
Learn Compiler Design: From B. K. Sharma
Parsing
Input String
Stream of
Parsing Derivation
Tokens Tree
CFG
Learn Compiler Design: From B. K. Sharma
Parsing
Input String
Stream of
Parsing Parse Tree
Tokens
CFG
Learn Compiler Design: From B. K. Sharma
Parsing
G
S c A d Input: cad
A ab | a
S S
Production: S c A d
S c A d
Production: A a
c a d c A d
a
Learn Compiler Design: From B. K. Sharma
Derivation
Parser E
Input => E + E
E -> E + E
| E * E => 10 + E
10 + 2 * 5 => 10 + E*E
| INT
=> 10 + 2 * E
=> 10 + 2 * 5
Learn Compiler Design: From B. K. Sharma
Derivation tree
Derivation
E
E
=> E + E +
E E
=> 10 +E
=> 10 +E *E
10
=> 10 + 2 * E E E
*
=> 10 + 2 * 5
2 5
Learn Compiler Design: From B. K. Sharma
Id Id
Learn Compiler Design: From B. K. Sharma
Difference between Parse Tree and Syntax Tree