Title: A Simple SyntaxDirected Translator
1A Simple Syntax-Directed Translator
- From Chapter 2, The Dragon Book, 2nd ed.
2Overview
- This chapter is an introduction to the compiling
techniques in Chapters 3 through 6 of the book.
32.1 Introduction
Abstract syntax tree
Three address code
42.2 Syntax Definition
- Definition of grammars (2.2.1)
- A context-free grammar has four components
- a set of terminal symbol,
- a set of nonterminal symbols
- a set of productions
- a designated nonterminal as the start symbol
- Example 2.1
list ? list digit list ? list - digit list ?
digit digit ? 0 1 2 3 4 5 6 7 8
9
52.2 Syntax Definition
- Derivations(2.2.2)
- A grammar derives strings by beginning with the
start symbol and repeatedly replacing a
nonterminal by the body of a production for that
nonterminal. - Example 2.2
- Derivation of 9-52 using the grammar of Example
2.1. - Example 2.3
call ? id ( optparams ) optparams ? params
e params ? params , params param max(x, y)
62.2 Syntax Definition
- Parse tree (2.2.3)
- A parse tree pictorially shows how the start
symbol of a grammar derives a string in the
language. - Example 2.4
72.2 Syntax Definition
- Ambiguity (2.2.4)
- A grammar can have more than one parse tree
generating a given string of terminals. Such a
grammar is said to be ambiguous. - Example 2.5
string ? string string string string
0123456789
82.2 Syntax Definition
- Associativity of operators (2.2.5)
- left-associative
- , -, , /
- Right-associative
- (in C), e.g., abc
right ? letter right letter letter ? a b
... z
92.2 Syntax Definition
- Precedence of operators (2.2.6)
- has higher precedence than
- Two nontermainals expr and term for the two
levels of precedence, and an extra one factor for
generating basic units in expressions
expr ? expr term expr term term term ?
term factor term / factor factor factor ?
digit ( expr )
102.3 Syntax-Directed Translation
- Syntax-directed translation is done by attaching
rules or program fragments to productions in a
grammar. - The example here
- Translation of infix expressions into postfix
expression - Introducing two concepts related to
syntax-directed translation - Attributes
- Any quantity associated with a program entity,
- Examples data types of expression, the number of
instructions in the generated code, or the
location of the first instruction in the
generated code for a construct, ... - Translation schemes
- A notation for attaching program fragment to the
productions of a grammar
112.3 Syntax-Directed Translation
- Postfix notation (2.3.1)
- Definition of postfix notation
- If E is a variable or constant, then the postfix
notation for E is E itself. - If E is an expression of the form E1 op E2, where
op is any binary operator, then the postfix
notation for E is E1 E2 op , where E1 and E2
are the postfix notations for E1 and E2,
respectively. - If E is a parenthesized expression of the form
(E1), then the postfix notation for E is the same
as the postfix notation for E1. - Example 2.8
- (9-5)2 ? 95-2
- 9-(52) ? 952-
- Example 2.
- 9523
- Scanning from left to right
122.3 Syntax-Directed Translation
- Synthesized attributes (2.3.2)
- A syntax-directed definition associates
- With each grammar symbol, a set of attributes,
and - With each production, a set of semantic rules for
computing the values of the attributes associated
with the symbols appearing in the production.
132.3 Syntax-Directed Translation
- Simple syntax-directed definitions (2.3.3)
- A syntax-directed definition with the following
property is termed simple. - the string representing the translation of the
nonterminal at the head of each production is the
concatenation of the translations of the
nonterminals in the production body, in the same
order as in the production, with some optional
additional string interleaved - For example, 2.10
142.3 Syntax-Directed Translation
- Tree traversal (2.3.4)
- Tree traversal is used for describing attribute
evaluation and for specifying the execution of
code fragments in a translation scheme.
152.3 Syntax-Directed Translation
- Translation schemes (2.3.5)
- A syntax-directed translation scheme is a
notation for specifying a translation by
attaching program fragments (called semantic
actions) to productions in a grammar. - A translation scheme is like a syntax-directed
definition, except that the order of evaluation
of the semantic rules is explicitly specified. - Example 2.12
162.4 Parsing
172.5 A Translator for Simple Expressions
182.6 Lexical Analysis
192.7 Symbol Tables
202.8 Intermediate Code Generation