General Information

 o Eli: Translator Construction Made Easy
 o Global Index
 o Frequently Asked Questions

Tutorials

 o Quick Reference Card
 o Guide For new Eli Users
 o Release Notes of Eli
 o Tutorial on Name Analysis
 o Tutorial on Type Analysis

Reference Manuals

 o User Interface
 o Eli products and parameters
 o LIDO Reference Manual

Libraries

 o Eli library routines
 o Specification Module Library

Translation Tasks

 o Lexical analysis specification
 o Syntactic Analysis Manual
 o Computation in Trees

Tools

 o LIGA Control Language
 o Debugging Information for LIDO
 o Graphical ORder TOol

 o FunnelWeb User's Manual

 o Pattern-based Text Generator
 o Property Definition Language
 o Operator Identification Language
 o Tree Grammar Specification Language
 o Command Line Processing
 o COLA Options Reference Manual

 o Generating Unparsing Code

 o Monitoring a Processor's Execution

Administration

 o System Administration Guide

 Questions, Comments, ....

Tutorial on Type Analysis

Previous Chapter Next Chapter Table of Contents


Operator Overloading

We here extend our language by binary and unary operators in order to demonstrate type analysis for expressions with overloaded operators. The following grammar fragment introduces binary operators such that AddOpr has lower precedence than MulOpr, and unary operators, MonOpr, have highest precedence. (The necessary chain productions have been specified in a grammar fragment above.) The productions for the operators themselves are created from the operator descriptions below.

Opr.con[27]==


Expression:     Expression AddOpr Factor.
Factor:         Factor MulOpr Operand.
Operand:        MonOpr Operand.
Operand:        '(' Expression ')'.

This macro is attached to a product file.

Type analysis for operators is supported by the library module Operator:

OprType.specs[28]==


$/Type/Operator.gnrc+referto=(Operator.descr):inst

This macro is attached to a product file.

The referto parameter of the instantiation identifies the following specification fragment which contains descriptions of the operators.

Operator.descr[29]==


Source Operators[30]
Target Operators[31]

This macro is attached to a product file.

Operators are overloaded in our language, i.e. a source operator symbol like + may denote one of several target operations, e.g. integer addition or floating point addition. The distinction is made using the types of the operands. Hence, we provide two related sets of descriptions, one for source operators and one for target operators.

Each of the following lines describes one source operator by its token, the concrete grammar symbol it is derived from, the abstract grammar symbol for its representation in the tree, and a unique name:

Source Operators[30]==


SrcOpr ('+', AddOpr, BinOpr, AddOp)
SrcOpr ('-', AddOpr, BinOpr, SubOp)
SrcOpr ('*', MulOpr, BinOpr, MulOp)
SrcOpr ('/', MulOpr, BinOpr, DivOp)

SrcOpr ('+', MonOpr, UnOpr, PlusOp)
SrcOpr ('-', MonOpr, UnOpr, NegOp)
SrcOpr ('!', MonOpr, UnOpr, NotOp)

This macro is invoked in definition 29.

For each of the source operators at least one meaning is specified by one of the following target operator descriptions. The first component of a target operator description relates it to a source operator, the second component is a unique name. Here, for example the AddOp is overloaded with three target operators: iAdd, rAdd, and bAdd. The operator names are automatically introduced as names for definition table keys. They may be used explicitly in specifications to distinguish target operators, or to associate properties to them.

The third component describes the signature of the target operator expressed in terms of keys for predefined types.

Target Operators[31]==


TgtOpr (AddOp, iAdd, (intType,intType):intType, TgtStr={"+"})
TgtOpr (SubOp, iSub, (intType,intType):intType, TgtStr={"-"})
TgtOpr (MulOp, iMul, (intType,intType):intType, TgtStr={"*"})
TgtOpr (DivOp, iDiv, (intType,intType):intType, TgtStr={"/"})

TgtOpr (AddOp, rAdd, (realType,realType):realType, TgtStr={"+"})
TgtOpr (SubOp, rSub, (realType,realType):realType, TgtStr={"-"})
TgtOpr (MulOp, rMul, (realType,realType):realType, TgtStr={"*"})
TgtOpr (DivOp, rDiv, (realType,realType):realType, TgtStr={"/"})

TgtOpr (PlusOp, iPlus, (intType):intType, TgtStr={"+"})
TgtOpr (NegOp,  iNeg,  (intType):intType, TgtStr={"-"})

TgtOpr (PlusOp, rPlus, (realType):realType, TgtStr={"+"})
TgtOpr (NegOp,  rNeg,  (realType):realType, TgtStr={"-"})

TgtOpr (AddOp, bAdd, (boolType,boolType):boolType, TgtStr={"||"})
TgtOpr (MulOp, bMul, (boolType,boolType):boolType, TgtStr={"&&"})

TgtOpr (NotOp, bNot, (boolType):boolType, TgtStr={"!"})

This macro is invoked in definition 29.

The last component associates a value to a property named TgtStr of the target operator. The name of the property, its type, and its value may be chosen arbitrarily. Here they are strings that could be used for translating the operators to C. Since, translation is not specified in this text, that property is not further used. However, we have to specify our choice:

OprType.pdl[32]==


TgtStr:  CharPtr; "Strings.h"

This macro is attached to a product file.

The target operator signatures as given above would require operands to have exactly those types. E. g. a + 1 would be illegal if a were of type realType. The following specification introduces coercion from intType to realType be applicable if necessary to identify a target operator.

OprType.oil[33]==


COERCION cFloat (intType): realType;

This macro is attached to a product file.

Type analysis for binary and unary expressions needs to compute the type attribute of the whole expression (the result type of the operation) and the required operand type (the corresponding type of the signature of the identified target operator). The latter may differ from the operand type if coercion is applied. We can obtain these computations from the module roles BinTgtOpr and UnTgtOpr. We only need to pass the type information to and from the operator symbol as shown below. The module role ChkOpr issues a message if no target operator with a suitable signature could be identified. (The key of the target operator could also be obtained by BinOpr.TgtKey or UnOpr.TgtKey, if necessary e. g. for translation.)

Opr.lido[34]==


SYMBOL BinOpr INHERITS BinTgtOpr, ChkOpr END;
RULE: Expression ::= Expression BinOpr Expression COMPUTE
  BinOpr.LType = Expression[2].Type;
  BinOpr.RType = Expression[3].Type;
  Expression[1].Type = BinOpr.ResType;
  Expression[2].ReqType = BinOpr.LTType;
  Expression[3].ReqType = BinOpr.RTType;
END;

SYMBOL UnOpr INHERITS UnTgtOpr, ChkOpr END;
RULE: Expression ::= UnOpr Expression COMPUTE
  UnOpr.RType = Expression[2].Type;
  Expression[1].Type = UnOpr.ResType;
  Expression[2].ReqType = UnOpr.RTType;
END;

This macro is attached to a product file.


Previous Chapter Next Chapter Table of Contents