16 March 2016
Below is a fragment of the grammar for the language Standard ML.. We’ve done a lot with standard arithmetic expressions, so this is a bit of a departure — we’re representing types.
grammar LittleML;
top : type EOF;
type : type ID
| '(' type (',' type)+ ')' ID
| type '*' type
| <assoc=right> type '->' type
| '{' record? '}'
| '(' type ')'
| TVAR
| ID
;
record : ID ':' type (',' record)?;
ID : [A-Za-z] [A-Za-z0-9]*;
TVAR : '\'' [A-Za-z0-9]+;
WS : (' '|'\t'|'\n')+ -> skip;
Which of the following expressions is valid according to this grammar? If it’s valid, draw the parse tree.
int32
unsigned_int
'a
'a list
int list tree
int * float + char
int * char tree
{}
{x:int, y:float}
()
(int, char) tree
('a, 'b) (int * float)
'a -> 'b
'a list * int -> int * int
Which of these methods are part of the generated LittleMLBaseVisitor
class?
visitTVAR(TVARContext cxt)
visitType(TypeContext cxt)
visitRecord(RecordContext cxt)
visitWS(WSContext cxt)
visitOp(OpContext cxt)
Now suppose we label the different alternatives of the type
rule using the hash notation:
type : type ID #TypeApp
| '(' type (',' type)+ ')' ID #TypeApp
| type '*' type #TypePair
| <assoc=right> type '->' type #TypeArrow
| '{' record? '}' #TypeRecord
| '(' type ')' #TypeParen
| TVAR #TypeVar
| ID #TypeName
;
Now which of these methods are part of the generated LittleMLBaseVisitor
class?
visitTypeRecord(TypeRecordContext cxt)
visitType(TypeContext cxt)
visitRecord(RecordContext cxt)
visitTypeArrow(TypeArrowContext cxt)
When the following type expressions are parsed, what is the order in which the (hash-tagged) visitor methods are called in a standard pre-order traversal? Hint: draw the tree first.
int list tree
'a list * int -> int * int
{x:int, y:float}
{x:int * float, y: 'a list} * d