CC Unit 3
CC Unit 3
Syntax Analysis
Introduction to parser
• The syntax analyzer (parser) basically checks for the
syntax of the language.
• It takes a token from the lexical analyzer and group
them in such a way that some programming
structure (syntax) can be recognized.
• This overall process is called syntax checking of the
language.
Difference between syntax tree and parse tree
• Syntax trees are smaller than parse trees and these are much
space and time efficient.
• Top-down parsers build parse trees from the top (root) to the
bottom (leaves).
• Bottom-up parsers build parse trees from the leaves and work
up to the root.
• In both case input to the parser is scanned from left to right,
one symbol at a time.
• The output of the parser is some representation of the parse
tree for the stream of tokens.
Tasks conducted during parsing
§ Error production
§ Global correction
Panic mode
§ It is simple.
Phrase-level recovery
§ On discovering an error; the parser may perform local correction on the
which the actual error has occurred before the point of detection.
Error productions
S→ aB | bA
A → a | aS | bAA
B → b | bS | aBB
id+id*id
Removing Ambiguity by precedence and Associativity rules-
• Rule-01:
rules-
• The higher the level of the production, the lower the priority of
operator.
• The lower the level of the production, the higher the priority of
operator.
Removing Ambiguity by precedence and Associativity rules-
• Rule-02:
rules
production.
production.
Problem-01:
• Recursive Descent Parser uses the technique of Top-Down Parsing without backtracking.
• It can be defined as a Parser that uses the various recursive procedure to process the input
• The first symbol of the string of R.H.S of production will uniquely determine the correct
alternative to choose.
procedure.
• One of major drawback or recursive-descent parsing is that it can be implemented only for
those languages which support recursive procedure calls and it suffers from the problem of
left-recursion.
Recursive Descent parser
• Example − Write down the algorithm using
Recursive procedures to implement the following
Grammar.
• E → TE′
• E′ → +TE′
• T → FT′
• T′ →∗ FT′|ε
• F → (E)|id