![]() |
![]() |
![]() |
General Information
Tutorials
Reference Manuals
Libraries
Translation Tasks
Tools
Administration
![]() |
![]() |
Type analysis tasksOperator Identification
Operator identification is a subtask of type analysis for any language
that has overloaded operators, i.e. a source operator symbol like
The
This module replaces the
The $/Type/Operator.gnrc +referto=(FILENAME):instIf this instantiation is contained in a .specs file and
if the description file, say Oper.d is contained in the
same directory, it may read
$/Type/Operator.gnrc +referto=(Oper.d):instIf the .specs and Oper.d are contained in a .fw
specification, the same line can be used.
The file contains a sequence of macro calls that describe source
operators and target operators.
The module provides computational roles for binary and unary operators
( The macros that describe the operators have the following forms:
The following restrictions must be obeyed:
Every
The computational role
The same holds for the computational role
The computational role The use of this module is demonstrated by adding expressions with binary and unary operators to the language of our running example. In the concrete grammar an expression hierarchy defines precedences and associativity of operator sets: Expression: Expression AddOpr Factor / Factor. Factor: Factor MulOpr Operand / Operand. Operand: MonOpr Operand. Operand: '(' Expression ')'.Here UnOpr s have highest precedence and AddOpr have lowest.
The operators of the three sets are not specified here.
They are introduced by SrcOpr descriptions contained in the
file the module is instantiated with, e.g.
SrcOpr ('+', AddOpr, BinOpr, AddKey) SrcOpr ('*', MulOpr, BinOpr, MulKey) SrcOpr ('-', MonOpr, UnOpr, NegKey) The module adds a production for every source operator to the concrete grammar, e.g. AddOpr: '+'.It states that AddOpr and MulOpr are to be represented
by a BinOpr node in the tree, and it adds an attribute
computations for each operator context to the tree, e.g.
RULE: BinOpr ::= '+' COMPUTE BinOpr.OprSym = AddKey; END; The following target operator descriptions (also contained in the file the module is instantiated with) overload those operators with operations on integral numbers and boolean values: TgtOpr (AddKey, iAddKey, (intType,intType):intType, TgtStr={"+"}) TgtOpr (AddKey, bOrKey, (boolType,boolType):boolType, TgtStr={"||"}) TgtOpr (MulKey, iMulKey, (intType,intType):intType, TgtStr={"*"}) TgtOpr (MulKey, bAndKey, (boolType,boolType):boolType, TgtStr={"&&"}) TgtOpr (NegKey, iNegKey, (intType):intType, TgtStr={"-"}) TgtOpr (NegKey, bNotKey, (boolType):boolType, TgtStr={"!"})From these descriptions the module generates the necessary .oil specifications, e.g INDICATION AddKey: iAddKey; OPER iAddKey (intType,intType):intType;and the necessary .pdl specifications, e.g iAddKey -> TgtStr={"+"};The properties that are initialized in the target operator description, here TgtStr have to be defined in the user's specification.
We then can use the computational roles SYMBOL Expression: Type, ReqType: DefTableKey; 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;
The operator descriptions of the target operators in our example
associate a property RULE: Expression ::= Expression BinOpr Expression COMPUTE Expression[1].CCode = PTGBinOpr (Expression[2].CCode, GetTgtStr (BinOpr.TgtKey, "??"), Expression[3].CCode); END; RULE: Expression ::= UnOpr Expression COMPUTE Expression[1].CCode = PTGUnOpr (GetTgtStr (UnOpr.TgtKey, "??"), Expression[2].CCode); END;
To complete the above example we need a .pdl specification of the
TgtStr: CharPtr; "Strings.h"
and suitable BinOpr: "(" $1 ")" $2 string "(" $3 ")" UnOpr: $1 string "(" $2 ")"
|