Handbook of Computer Science(cs) and IT

Syntax Directed Translation

Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent. Values of these attributes are evaluated by the semantic rules associated with the

production rules.

Evalution of the semantic rules are as follows

  1. May generate intermediate codes
  2. May put information into the symbol table
  • May perform type checking
  1. May issue error messages
  2. May perform some other activities
  • An attribute may hold a string, a number, a memory location, a complex record etc.
  • Evaluation of a semantic rule defines the value of an attribute, but a semantic rule may also have some side effects such as printing a value. g.,

 

 

 

  • Symbols E, T and F are associated with an attribute value.
  • The token digit has an attribute lexval (it is assumed that it is evaluated by the lexical analyzer).
  • The program fragment above represents the implementation of the semantic rule for a bottom-up Parser.
  • At each shift of digit, we also push digit. Lexval into val_stack.
  • At all other shills, we do not put anything into val_stack because other terminals donot have attributes (but we increment the stack pointer val stack).
  • The above model is suited for a desk calculator, where the purpose Iv; evaluate and to generate code.

Intermediate Code Generation

Intermediate codes are machine independent codes, but they are close to machine instructions. The given program in a source language is converted to an equivalent program in an intermediate language by the intermediate code generator.

The designer of the compiler decides the intermediate language.

  • Syntax trees can he used as an intermediate language.
  • Postfix notations, three address code (quadruples) can be used as an intermediate

Syntax Tree

Syntax tree is a variant of the parse tree, where each leaf represents an operand and each interior node represent an operator.

 

 

 

 

 

A sentence a * (b + d) would have the following syntax tree

 

Three-Address Code

When each statement contains three addresses (two for operands and one for result), Most general kind of three-address code is

x= y op z

Where x, y and z  are names, constants or compoler generated tempararies and op is any operator.

But we can also use the following notation for quadruples (much better notation because it looks like a machine code instruction)

op y, z, x

Apply operator op to y and z and store the result in x.

Representation of Three-Address Codes

Tree-address code can be represented in various forms r e  quadruples Triples and indirect triples. These forms are demonstrated by way of examples below.

e.g.,                                   A = — B* (C D)

Three address code is as follow

T1=— B

T2 =C +D

T3 = T1* T2

A= T3

 

 

 

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

Leave a Reply

Your email address will not be published. Required fields are marked *