0% found this document useful (0 votes)
17 views16 pages

CC Lec 7

The document discusses the structure and components of a compiler, focusing on the role of the parser in determining if an input token stream adheres to the syntax of a programming language defined by context-free grammar (CFG). It explains the concepts of parse trees, derivation methods, ambiguity in grammars, and techniques for eliminating ambiguity through precedence and associativity rules. Examples illustrate the derivation process and the impact of ambiguity on parsing.

Uploaded by

waseem khosa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views16 pages

CC Lec 7

The document discusses the structure and components of a compiler, focusing on the role of the parser in determining if an input token stream adheres to the syntax of a programming language defined by context-free grammar (CFG). It explains the concepts of parse trees, derivation methods, ambiguity in grammars, and techniques for eliminating ambiguity through precedence and associativity rules. Examples illustrate the derivation process and the impact of ambiguity on parsing.

Uploaded by

waseem khosa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

COMPILER

CONSTRUCTIO
N
Lecture 6
Source Language

Lexical Analyzer

Syntax Analyzer Front


STRUCTUR Semantic Analyzer
End

E OF A Int. Code Generator

COMPILER Intermediate Code

Code Optimizer
Back
Target Code Generator End

Target Language
2
Source Language

NOW! Lexical Analyzer

Syntax Analyzer Front


End
Semantic Analyzer

Int. Code Generator

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

 Note: Also called Backus Naur Form


 The language generated by CFG is called CFL (Context Free Language).
PARSE TREE
 Graphical representation od a derivation that filter out the order in which
productions are applied to replace non-terminal.
DERIVATION
 Derivation of strings can be done in two manner:
 Left most derivation
 The process of deriving a string by expanding the leftmost non-terminal at each step.

 Right most derivation


 The process of deriving a string by expanding the rightmost non-terminal at each step.
EXAMPLE LMD
 Consider the following grammar-
S → aB / bA
S → aS / bAA / a
B → bS / aBB / b
 Let us consider a string w = aaabbabbba
 S → aB
 → aaBB (Using B → aBB)
 → aaaBBB (Using B → aBB)
 → aaabBB (Using B → b)
 → aaabbB (Using B → b)
 → aaabbaBB (Using B → aBB)
 → aaabbabB (Using B → b)
 → aaabbabbS (Using B → bS)
 → aaabbabbbA (Using S → bA)
 → aaabbabbba (Using A → a)
EXAMPLE RMD
 Consider the following grammar-

 S → aB / bA

 S → aS / bAA / a

 B → bS / aBB / b

 Let us consider a string w = aaabbabbba

 S → aB

 → aaBB (Using B → aBB)

 → aaBaBB (Using B → aBB)

 → aaBaBbS (Using B → bS)

 → aaBaBbbA (Using S → bA)

 → aaBaBbba (Using A → a)

 → aaBabbba (Using B → b)

 → aaaBBabbba (Using B → aBB)

 → 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

 Example: E -> E+E | E*E | id


 It can be written as

 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

You might also like