A Simple SyntaxDirected Translator - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

A Simple SyntaxDirected Translator

Description:

... Translator. From: Chapter 2, The Dragon Book, 2nd ed. A Simple Translator. 2 ... A Simple Translator. 4. 2.2 Syntax Definition. Definition of grammars (2.2.1) ... – PowerPoint PPT presentation

Number of Views:190
Avg rating:3.0/5.0
Slides: 21
Provided by: cseTt
Category:

less

Transcript and Presenter's Notes

Title: A Simple SyntaxDirected Translator


1
A Simple Syntax-Directed Translator
  • From Chapter 2, The Dragon Book, 2nd ed.

2
Overview
  • This chapter is an introduction to the compiling
    techniques in Chapters 3 through 6 of the book.

3
2.1 Introduction
Abstract syntax tree
Three address code
4
2.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
5
2.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)
6
2.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

7
2.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
8
2.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
9
2.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 )
10
2.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

11
2.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

12
2.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.

13
2.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

14
2.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.

15
2.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

16
2.4 Parsing
17
2.5 A Translator for Simple Expressions
18
2.6 Lexical Analysis
19
2.7 Symbol Tables
20
2.8 Intermediate Code Generation
Write a Comment
User Comments (0)
About PowerShow.com