Reputation: 61
I have the following grammar that I need to translate into SML datatypes:
Integer ranges over SML integer constants.
Boolean ::= 'true' | 'false'
Operator ::= 'ADD' | 'IF' | 'LESS_THAN'
Arguments ::= ( ',' Expression ) *
Expression ::= 'INT' '(' Integer ')'
| 'BOOL' '(' Boolean ')'
| 'OPERATION' '(' Operator ',' '[' Expression ( ',' Expression ) * ']' ')'
I have managed the following:
datatype BOOL = true | false;
datatype OPERATOR = ADD | IF | LESS_THAN;
datatype INT = INT of int;
However I am struggling with datatypes Arguments
and Expression
. Any help would be appreciated.
Upvotes: 1
Views: 485
Reputation: 46882
for ARGUMENTS, you can use a sequence of EXPRESSIONs, so something like a list of EXPRESSION would work fine (the parentheses need to be parsed, but you don't need to store them in your type, since they are always there).
for EXPRESSION, you need to combine the approach you've used in OPERATOR (where you have alternatives) with what you've done in INT (where you have of ...
). in otherwords it's going to be of the form A of B | C of D | ...
.
also, you don't really need INT of int
- you could just use an simple int
(ie an integer) for INT - and i suspect ML has a boolean type that could use instead of defining a datatype for BOOL (in other words, you probably don't need to define a datatype at all for either of those - just use what is already present in the language).
ps it's also normal to add the "homework" tag for homework.
[edit for OPERATOR you have multiple types, but that's ok - just stick them in a tuple, like (A,B)
whose type is written a * b
. for the sequence of expressions, use a list, like for ARGUMENTS.]
Upvotes: 1