CC Lec 7
CC Lec 7
CONSTRUCTIO
N
Lecture 6
Source Language
Lexical Analyzer
Code Optimizer
Back
Target Code Generator End
Target Language
2
Source Language
Intermediate Code
Code Optimizer
Back
Target Code Generator End
Target Language
3
THE ROLE OF THE PARSER
4
THE ROLE OF THE PARSER
Goal: We must determine if the input token stream satisfies the syntax of
program.
The syntax of programming language is usually given by grammar rules of
CFG(Context Free Grammar).
The output is usually some time kind of tree called parse tree.
Parser
Sequence of tokens Syntax
Tree
CFG (CONTEXT FREE
GRAMMAR)
CFG consist of 4 components:
Terminal : A symbol that can’t be replaced.
Non-Terminal : A symbol that must be replaced.
Production : These are the grammatical rules.
Start Symbol
S → aB / bA
S → aS / bAA / a
B → bS / aBB / b
S → aB
→ aaBaBbba (Using A → a)
→ aaBabbba (Using B → b)
→ aaaBbabbba (Using B → b)
→ aaabbabbba (Using B → b)
AMBIGUITY
Consider we have a
CFG:
E -> E+E | E*E | -E |id
We need to accept the
string –(id+id)
But, for the same
grammar, suppose we
have a string id+id*id,
then we have
AMBIGUITY
AMBIGUITY
Hence, a grammar that produces more than one parse tree for some
sentence, it is said to be ambiguous.
For most parsers, it is desirable that the grammar be made unambiguous
for it.
If not, parser cannot uniquely determine which parse tree to select for a
sentence.
ELIMINATING AMBIGUITY
Removing Ambiguity By Precedence & Associativity Rules-
An ambiguous grammar may be converted into an unambiguous grammar by
implementing-
Precedence Constraints
The level at which the production is present defines the priority of the operator contained in it.
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.
Associativity Constraints
If the operator is left associative, induce left recursion in its production.
If the operator is right associative, induce right recursion in its production.
ELIMINATING AMBIGUITY
Example: E-> E+E |id
It can be solved as E -> E+id |id
E -> E+T |T
T -> T+F |F
F -> id
FURTHER EXAMPLE
See Compilers: Principles, Techniques and Tools by Alfred V.Aho and Ravi
Sethi Chapter 4, Section 4.3.2, Page-210