Syntax-Directed Translation: CS 5300 - SJAllan - Prof. Stephen J. Allan, Study notes of Computer Science

Syntax-directed translation, a method for translating languages guided by context-free grammars. It covers the concept of attaching attributes to grammar symbols and the use of semantic rules associated with productions. The document also introduces synthesized and inherited attributes, as well as examples of their usage. Likely to be useful for university students studying computer science, specifically those enrolled in a course related to compilers or programming language theory.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-3s5
koofers-user-3s5 🇺🇸

5

(1)

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Syntax-Directed Translation
CS 5300 - SJAllan 2
Introduction
Translation of languages guided by context-free grammars
Attach attributes to the grammar symbols
Values of the attributes are computed by semantic rules associated
with the grammar productions
Attribute may represent:
Number, type, string, memory location, label, etc.
Syntax–directed definitions:
Each production has a set of semantic rules associated with it
Conceptually we parse the input token stream, build the
parse tree and then traverse the tree as needed to
evaluate the semantic rules.
The rules may generate code, save information in the symbol table,
issue error messages, etc.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Syntax-Directed Translation: CS 5300 - SJAllan - Prof. Stephen J. Allan and more Study notes Computer Science in PDF only on Docsity!

Syntax-Directed Translation

CS 5300 - SJAllan 2

Introduction

ƒ Translation of languages guided by context-free grammars ƒ Attach attributes to the grammar symbols

  • Values of the attributes are computed by semantic rules associated with the grammar productions
  • Attribute may represent: ƒ Number, type, string, memory location, label, etc. ƒ Syntax–directed definitions:
  • Each production has a set of semantic rules associated with it ƒ Conceptually we parse the input token stream, build the parse tree and then traverse the tree as needed to evaluate the semantic rules.
  • The rules may generate code, save information in the symbol table, issue error messages, etc.

CS 5300 - SJAllan 3

Introduction

ƒ In some cases we don’t have to follow this outline literally

  • We may evaluate the rules during parsing ƒ Synthesized attribute:
  • The value of the attribute at a parent node can be found from the attributes of its children ƒ It can be evaluated by traversing the tree bottom-up ƒ Inherited attribute:
  • The value of the attribute at a node can be found from the attributes at its parent node and/or its sibling nodes ƒ Can be evaluated by traversing the tree top-down ƒ S-attributed definition:
  • A syntax-directed definition where all attributes are synthesized (the “S” stands for synthesized).

CS 5300 - SJAllan 4

Example: Simple Desk Calculator

Production L ÆE n E Æ E 1 + T E Æ T T Æ T 1 * F T Æ F F Æ (E) F Æ digit

Semantic Rules print(E.val) E.val := E 1 .val + T.val E.val := T.val T.val := T 1 .val * F.val T.val := F.val F.val := E.val F.val := digit .lexval

CS 5300 - SJAllan 7

Inherited Attributes

ƒ Inherited attributes are convenient for

expressing the dependence of a

programming language construct on the

context in which it appears.

CS 5300 - SJAllan 8

Example

ƒ Using inherited attributes to parse a declaration and add type information to the symbol table. L Æ id addtype (id.entry, L.in)

L 1 .in := L.in addtype (id.entry, L.in)

L Æ L 1 , id

T Æ real T.Type := real

T Æ int T.Type := integer

D Æ TL L.in := T.type

Production Semantic Rules

CS 5300 - SJAllan 9

Annotated Parse Tree for real, id1, id2, id

D T.Type = real

real

L.in = real

L.in = real

L.In = real

id

, id

.

. , (^) id

CS 5300 - SJAllan 10

Bottom-up Parsing

ƒ If all attributes are synthesized then a bottom-up parser can evaluate the attributes while it parses ƒ Add attribute fields to the nonterminals on the stack

ƒ Before each production apply the semantic rule associated with the production ƒ E.g. Assume the semantic rule associated with the A Æ XYZ production is : A.a := f (X.x, Y.y, Z.z)

CS 5300 - SJAllan 13

Bison Generated Parsers

ƒ Bison maintains a semantic stack which, in tandem with the parser stack, helps Bison manipulate and determine the meaning of the program it compiles

ƒ The values in the semantic stack represent the values or meanings of all the grammar symbols

ƒ Exactly what constitutes values for the entries in the semantic stack depends heavily on the semantics of the language being compiled

CS 5300 - SJAllan 14

Semantic Stack

ƒ Manipulation of the semantic stack is

triggered by the parser during reduce moves

ƒ Values of symbols on the rhs of the

production are popped off the semantic

stack

ƒ A value for the symbol on the lhs is

determined from these values and the

semantic rules of the language, and a new

value is pushed back on the stack

CS 5300 - SJAllan 15

Semantic Stack

ƒ All symbols in the grammar have a value

associated with them in the semantic stack

even though some of these values may be

NULL (indicating no information is needed

about this symbol)

ƒ Not all reductions require semantic actions

CS 5300 - SJAllan 16

Semantic Rules

ƒ Bison allows you to specify the semantic

action associated with the symbol on the lhs

of the production

  • These actions may return a value and may utilize the values returned by previous actions

ƒ The lexical analyzer can return values for

tokens, if desired, through the use of the

global variable yylval

CS 5300 - SJAllan 19

Union

ƒ The values on the semantic stack do not have to be of a single type

  • You can define the type of the value using the %union statement in Bison

ƒ Bison must know which type to associate with each symbol in the grammar

  • This is done using the %type statement ƒ Beware, once you starting using %type, you are forced to type all symbols even though you may not be using them
  • You are required to use %type statements in your project

CS 5300 - SJAllan 20

%union and %type

%union { int int_val; char *name_ptr; CONS *cons_ptr; ID *id_ptr; TYPE *ty_ptr; EXPR *exp_ptr; }

%type <int_val> INTCONSTSY IfHead %type <id_ptr> IdList FieldList