Entrepreneurship Process
Entrepreneurship Process
Chapter 2
2
Overview
• This chapter contains introductory material
• Building a simple compiler
– Syntax definition
– Syntax directed translation
– Predictive parsing
– Etc.
Overview
Programming Language can be defined by describing
1. The syntax of the language
1. What its program looks like
2. We use CFG or BNF (Backus Naur Form)
2. The semantics of the language
1. What its program mean
2. Difficult to describe
3. Use informal descriptions and suggestive
examples
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
5
Develop
parser and code
generator for translator
Syntax definition
specification
(grammar)
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
7
Parse Trees
• The root of the tree is labeled by the start symbol
• Each leaf of the tree is labeled by a terminal
(=token) or
• Each interior node is labeled by a non-terminal
• If A X1 X2 … Xn is a production, then node A has
children X1, X2, …, Xn where Xi is a (non)terminal or
( denotes the empty string)
Grammars for Syntax Definition
Example CFG
9 - 5 + digit P4 : digit 5
9-5+2 P4 : digit 2
Grammars are Used to Derive Strings:
This derivation could also be represented via a Parse Tree
(parents on left, children on right)
list list + digit list
list - digit + digit
list + digit
digit - digit + digit
- 2
9 - digit + digit list digit
9 - 5 + digit 5
digit
9-5+2
9
12
Ambiguity
Consider the following context-free grammar:
with production P =
Ambiguity (cont’d)
string string
9 - 5 + 2 9 - 5 + 2
14
Associativity of Operators
Left-associative operators have left-recursive productions
Precedence of Operators
Operators with higher precedence “bind more tightly”
expr expr + term | term
term term * factor | factor
factor number | ( expr )
String 2+3*5 has the same meaning as 2+(3*5)
expr
expr term
term term factor
factor factor number
number number
2 + 3 * 5
16
Syntax of Statements
stmt id := expr
| if expr then stmt
| if expr then stmt else stmt
| while expr do stmt
| begin opt_stmts end
opt_stmts stmt ; opt_stmts
|
Syntax-Directed Translation
• Uses a CF grammar to specify the syntactic
structure of the language
• AND associates a set of attributes with
(non)terminals
• AND associates with each production a set of
semantic rules for computing values for the
attributes
• The attributes contain the translated form of the
input after the computations are completed
Syntax for Statements
stmt id := expr
| if expr then stmt
| if expr then stmt else stmt
| while expr do stmt
| begin opt_stmts end
Ambiguous Grammar?
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)
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+–