Module1 1
Module1 1
Chapter 2
A Simple One – Pass Compiler
Chapter 2
CSE309N
The Entire Compilation Process
Grammars for Syntax Definition
Syntax-Directed Translation
Parsing - Top Down & Predictive
Pulling Together the Pieces
The Lexical Analysis Process
Symbol Table Considerations
A Brief Look at Code Generation
Concluding Remarks/Looking Ahead
Chapter 2
CSE309N
Overview
Chapter 2
CSE309N
Grammars for Syntax Definition
A Context-free Grammar (CFG) Is Utilized to
Describe the Syntactic Structure of a Language
A CFG Is Characterized By:
1. A Set of Tokens or Terminal Symbols
2. A Set of Non-terminals
3. A Set of Production Rules
Each Rule Has the Form
NT {T, NT}*
4. A Non-terminal Designated As
the Start Symbol
Chapter 2
CSE309N
Grammars for Syntax Definition
Example CFG
Chapter 2
CSE309N
Information
A string of tokens is a sequence of zero or more tokens.
The string containing with zero tokens, written as , is called
empty string.
A grammar derives strings by beginning with the start symbol
and repeatedly replacing the non terminal by the right side of a
production for that non terminal.
The token strings that can be derived from the start symbol form
the language defined by the grammar.
Chapter 2
CSE309N
Grammars are Used to Derive Strings:
9 - 5 + digit P4 : digit 5
9-5+2 P4 : digit 2
Chapter 2
CSE309N
Grammars are Used to Derive Strings:
Chapter 2
CSE309N
Defining a Parse Tree
Chapter 2
CSE309N
Other Important Concepts
Ambiguity
Two derivations (Parse Trees) for the same token string.
string string
-
+ string string
string string
+ string
string - string 2 9 string
9 5 5 2
Grammar:
string string + string | string – string | 0 | 1 | …| 9
Chapter 2
CSE309N
Other Important Concepts
Associativity of Operators
Left vs. Right
list right
Chapter 2
CSE309N
Other Important Concepts
Operator Precedence
What does ( )
9+5*2 Typically * / is precedence
mean? + - order
stmt id := expr
| if expr then stmt
| if expr then stmt else stmt
| while expr do stmt
| begin opt_stmts end
Ambiguous Grammar?
Chapter 2
CSE309N
Syntax-Directed Translation
Associate Attributes With Grammar Rules and Translate as Parsing
occurs
The translation will follow the parse tree structure (and as a result the
structure and form of the parse tree will affect the translation).
Rules
1. If E is a variable or constant then Postfix(E) = E
2. If E is E1 op E2 then Postfix(E)
= Postfix(E1 op E2) = Postfix(E1) Postfix(E2) op
3. If E is (E1) then Postfix(E) = Postfix(E1)
Chapter 2
CSE309N
Examples
Postfix( ( 9 – 5 ) + 2 )
= Postfix( ( 9 – 5 ) ) Postfix( 2 ) +
= Postfix( 9 – 5 ) Postfix( 2 ) +
= Postfix( 9 ) Postfix( 5 ) - Postfix( 2 ) +
=95–2+
Postfix(9 – ( 5 + 2 ) )
= Postfix( 9 ) Postfix( ( 5 + 2 ) ) -
= Postfix( 9 ) Postfix( 5 + 2 ) –
= Postfix( 9 ) Postfix( 5 ) Postfix( 2 ) + –
=952+–
Chapter 2
CSE309N
Syntax-Directed Definition
expr.t =95-2+
expr.t =9 term.t =5
term.t =9
9 - 5 + 2
It starts at the root and recursively visits the children of
each node in left-to-right order
The semantic rules at a given node are evaluated once all
descendants of that node have been visited.
A parse tree showing all the attribute values at each node
is called annotated parse tree. Chapter 2
CSE309N
Translation Schemes
Embedded Semantic Actions into the right sides of
the productions.
A translation scheme is
expr expr + term {print(„+‟)}
like a syntax-directed
definition except the
expr - term {print(„-‟)}
order of evaluation of
term the semantic rules is
term 0 {print(„0‟)} explicitly shown.
term 1 {print(„1‟)}
expr
… {print(„+‟)}
+
term 9 {print(„9‟)} expr term
- {print(„-‟)} 2 {print(„2‟)}
expr term
5 {print(„5‟)}
term
9 {print(„9‟)}
Chapter 2