Example.ThreeC4
Copyright Ó 1987 by Xerox Corporation. All rights reserved.
Bill Jackson (bj) April 22, 1987 2:16:40 am PDT
ExampleParser: Control Module;
ExampleGeneric: Module = Begin
Generic Tokens
{ "+" "-" "*" "/" "(" ")" "#" "!" "." } : SimpleTokens;
number: GenericToken = "tokenDECIMAL";
End;
ExampleBase: Module = Begin
Cedar Types
INT: CedarType;
BOOLEAN: CedarType;
ROPE: CedarType From Rope;
Base Functions
Op: EnumeratedBaseType = { plus, minus, mult, div };
ValueOf:
BaseFunction[ number.node ]
Returns [ INT.value ];
EqualOp:
BaseFunction[ Op.left, Op.right ]
Returns [ BOOLEAN.yes ];
Add:
BaseFunction[ INT.left, INT.right ]
Returns [ INT.result ];
Subtract:
BaseFunction[ INT.left, INT.right ]
Returns [ INT.result ];
Multipy:
BaseFunction[ INT.left, INT.right ]
Returns [ INT.result ];
Divide:
BaseFunction[ INT.left, INT.right ]
Returns [ INT.result ];
Signal:
BaseFunction
Returns [ INT.result ];
Error:
BaseFunction[ ROPE.message ]
Returns [ INT.result ];
End;
ExampleTree: Module = Begin
Recursive Functions
Evaluate:
TreeRecursiveFunction [ Tree ]
Returns [ INT ];
End;
ExampleCG: Module = Begin
maingoal: NonTerminal Builds Expression;
for maingoal ← sum "."
Buildsum;
sum: NonTerminal Builds Expression;
for sum.prod ← product
Buildproduct;
for sum.plus ← sum "+" product
BuildExpression.bin[sum, Op.plus, product];
for sum.minus ← sum "-" product
BuildExpression.bin[sum, Op.minus, product];
product: NonTerminal Builds Expression;
for product.prim ← primitive
Buildprimitive;
for product.mult ← product "*" primitive
BuildExpression.bin[product, Op.mult, primitive];
for product.div ← product "/" primitive
BuildExpression.bin[product, Op.div, primitive];
primitive: NonTerminal Builds Expression;
for primitive.number ← number
BuildExpression.number[number];
for primitive.loc ← "#"
BuildExpression.loc[];
for primitive.signal ← "!"
BuildExpression.signal[];
for primitive.paren ← "(" sum ")"
Buildsum;
End;
ExampleAT: Module = Begin
Expression: AbstractType [ Evaluate ];
End;
ExampleAG: Module = Begin
Expression: AbstractType[ Evaluate ];
Expression.bin: AbstractProduction [ Expression.left, Op.op, Expression.right ];
Expression.number: AbstractProduction [ number ];
Expression.loc: AbstractProduction [ ];
Expression.signal: AbstractProduction [ ];
End;
ExampleDoit: Module = Begin
Expression
for Expression.bin: AbstractProduction [ Expression.left, Op.op, Expression.right ]
let Evaluate[tree] ←
if ( EqualOp[Op.op, Op.plus] )
then Add[Evaluate[Expression.left], Evaluate[Expression.right]]
else if ( EqualOp[Op.op, Op.minus] )
then Subtract[Evaluate[Expression.left], Evaluate[Expression.right]]
else if ( EqualOp[Op.op, Op.mult] )
then Multipy[Evaluate[Expression.left], Evaluate[Expression.right]]
else if ( EqualOp[Op.op, Op.div] )
then Divide[Evaluate[Expression.left], Evaluate[Expression.right]]
else Error["unknown binary operator"];
for Expression.number: AbstractProduction [ number ]
let Evaluate[tree] ←
ValueOf[number];
for Expression.loc: AbstractProduction [ ]
let Evaluate[tree] ←
SourcePosition[tree];
for Expression.signal: AbstractProduction [ ]
let Evaluate[tree] ←
Signal[];
End.
eof...