toy.opp.exp 9.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
File "toy.mly", line 48, characters 7-9:
Warning: the token OF is unused.
File "toy.mly", line 58, characters 7-9:
Warning: the token TO is unused.
File "toy.mly", line 61, characters 7-11:
Warning: the token TYPE is unused.
File "toy.mly", line 64, characters 7-12:
Warning: the token VALUE is unused.
%{

open ToySyntax
open ToyParserUtil

%}
%start phrase
%token WITH
%token VALUE
%token UNIT
%token UNDERSCORE
%token TYPE
%token TRY
%token TRUE
%token TO
%token THEN
%token <string> STRINGCONST
%token SHARP
%token SETMINUS
%token SEMISEMI
%token SEMI
%token RPAREN
%token REF
%token REC
%token RBRACE
%token OF
%token MATCH
%token LPAREN
%token LET
%token LEFTARROW
%token LBRACKETBAR
%token LBRACE
%token <int> INTCONST
%token INCLUDE
%token IN
%token IF
%token <string> IDENT
%token FUNCTION
%token FUN
%token <float> FLOATCONST
%token FALSE
%token EQUAL
%token ELSE
%token DOT
%token <string> CONSTRUCTOR
%token COMMA
%token COLON
%token <char> CHARCONST
%token BARRBRACKET
%token BAR
%token BANG
%token ATAT
%token AT
%token ASSIGN
%token AS
%token ARROW
%token AND

%type <ToySyntax.phrase> phrase
%%

phrase:
71 72 73 74 75 76 77 78 79 80
| _1 = SHARP _2 = IDENT
    {                             ( PhraseDirective _2 )}
| _1 = LET _2 = binding_list _3 = SEMISEMI
    {                                         ( PhraseLet(false, _2) )}
| _1 = LET _2 = REC _3 = binding_list _4 = SEMISEMI
    {                                            ( PhraseLet(true, _3) )}
| _1 = expression _2 = SEMISEMI
    {                                    ( PhraseExpr _1 )}
| _1 = INCLUDE _2 = STRINGCONST
    {                                                                ( PhraseInclude _2 )}
81 82

expression:
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| _1 = expression3
    {                            ( _1 )}
| _1 = FUNCTION _2 = function_match
    {                                   ( EFun _2 )}
| _1 = FUN _2 = fun_match
    {                       ( EFun _2 )}
| _1 = MATCH _2 = expression _3 = WITH _4 = function_match
    {                                           ( EApp (EFun _4, _2) )}
| _1 = LET _2 = binding_list _3 = IN _4 = expression
    {                                                    ( ELet(false, _2, _4) )}
| _1 = LET _2 = REC _3 = binding_list _4 = IN _5 = expression
    {                                                        ( ELet(true, _3, _5) )}
| _1 = REC _2 = pattern _3 = IN _4 = expression
    {                                  ( EApp (EVar "_rec", EFun [[_2], _4]) )}
| _1 = TRY _2 = expression _3 = WITH _4 = try_match
    {                                                                ( ETry (_2, _4) )}
99 100

expression3:
101 102 103 104
| _1 = expression2
    {                      ( _1 )}
| _1 = expression2 _2 = SEMI _3 = expression
    {                                    ( ESeq(_1, _3) )}
105 106

expression2:
107 108 109 110 111 112 113 114
| _1 = expression1
    {                      ( _1 )}
| _1 = IF _2 = expression1 _3 = THEN _4 = expression2 _5 = ELSE _6 = expression2
    {                                                      ( EIf (_2, _4, _6) )}
| _1 = expression1 _2 = COMMA _3 = expression2
    {                                     ( EApp (EApp (EVar "_pair", _1), _3) )}
| _1 = expression1 _2 = ASSIGN _3 = expression1
    {                                      ( EApp (EApp(EVar ":=", _1), _3) )}
115 116

expression1:
117 118 119 120 121 122 123 124
| _1 = expression0
    {                     ( _1 )}
| _1 = CONSTRUCTOR _2 = expression0
    {                               ( EApp (EConstruct _1, _2) )}
| _1 = expression1 _2 = expression0
    {                                      ( EApp (_1, _2) )}
| _1 = REF _2 = expression0
    {                                                        ( EApp (EVar "ref", _2) )}
125 126

expression0:
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| _1 = IDENT
    {                ( EVar _1 )}
| _1 = CONSTRUCTOR
    {                     ( EApp (EConstruct _1, EConstant ConstUnit) )}
| _1 = constant
    {                  ( EConstant _1 )}
| _1 = LPAREN _2 = expression _3 = RPAREN
    {                                      ( _2 )}
| _1 = LBRACE _2 = label_expression_list _3 = RBRACE
    {                                          ( ERecord _2 )}
| _1 = LBRACKETBAR _2 = expr_semi_list _3 = BARRBRACKET
    {                                                        ( EVector _2 )}
| _1 = BANG _2 = expression0
    {                         ( EApp (EVar "!", _2) )}
| _1 = expression0 _2 = DOT _3 = IDENT _4 = LEFTARROW _5 = expression1
    {                                                        ( EApp (EApp (ERecordUpdate _3, _1), _5) )}
| _1 = expression0 _2 = DOT _3 = IDENT
    {                                    ( EApp (ERecordAccess _3, _1) )}
| _1 = expression0 _2 = SETMINUS _3 = IDENT
    {                                                        ( EApp (ERecordRestrict _3, _1) )}
| _1 = expression0 _2 = AT _3 = expression1
    {                                                        ( EApp (EApp (EVar "@", _1), _3) )}
| _1 = expression0 _2 = ATAT _3 = expression1
    {                                                        ( EApp (EApp (EVar "@@", _1), _3) )}
| _1 = CONSTRUCTOR _2 = COLON _3 = expression0
    {                                                        ( ELabel (_1, _3) )}
153 154

constant:
155 156 157 158 159 160 161 162 163 164 165 166 167 168
| _1 = INTCONST
    {                  ( ConstInt _1 )}
| _1 = FALSE
    {               ( ConstBool false )}
| _1 = TRUE
    {              ( ConstBool true )}
| _1 = UNIT
    {              ( ConstUnit )}
| _1 = FLOATCONST
    {                                                        ( ConstFloat _1 )}
| _1 = CHARCONST
    {                                                        ( ConstChar _1 )}
| _1 = STRINGCONST
    {                                                        ( ConstString _1 )}
169 170

label_expression:
171 172
| _1 = IDENT _2 = EQUAL _3 = expression2
    {                               ( _1, _3 )}
173 174 175

label_expression_list:
| 
176 177 178 179 180
    {                      ( [] )}
| _1 = label_expression
    {                         ( [_1] )}
| _1 = label_expression _2 = SEMI _3 = label_expression_list
    {                                                 ( _1 :: _3 )}
181 182 183

expr_semi_list:
| 
184 185 186 187 188
    {                                                        ( [] )}
| _1 = expression2
    {                                                        ( [_1] )}
| _1 = expression2 _2 = SEMI _3 = expr_semi_list
    {                                                        ( _1 :: _3 )}
189 190

fun_match:
191 192 193 194
| _1 = simple_pattern_list _2 = ARROW _3 = expression _4 = BAR _5 = fun_match
    {                                                       ( (_1, _3) :: _5 )}
| _1 = simple_pattern_list _2 = ARROW _3 = expression
    {                                          ( [_1, _3] )}
195 196

function_match:
197 198 199 200
| _1 = pattern _2 = ARROW _3 = expression _4 = BAR _5 = function_match
    {                                                 ( ([_1], _3) :: _5 )}
| _1 = pattern _2 = ARROW _3 = expression
    {                                ( [[_1], _3] )}
201 202

try_match:
203 204 205 206
| _1 = pattern _2 = ARROW _3 = expression _4 = BAR _5 = try_match
    {                                            ( (_1, _3) :: _5 )}
| _1 = pattern _2 = ARROW _3 = expression
    {                                ( [_1, _3] )}
207 208

simple_pattern:
209 210 211 212 213 214 215 216 217 218 219 220
| _1 = UNDERSCORE
    {                           ( PWildcard )}
| _1 = IDENT
    {               ( PVar _1 )}
| _1 = constant
    {                  ( PConstant _1 )}
| _1 = CONSTRUCTOR
    {                     ( PConstruct (_1, PConstant ConstUnit) )}
| _1 = LPAREN _2 = pattern _3 = RPAREN
    {                             ( _2 )}
| _1 = LBRACE _2 = label_pattern_list _3 = RBRACE
    {                                       ( PRecord _2 )}
221 222

pattern:
223 224 225 226 227 228 229 230 231 232 233 234
| _1 = simple_pattern
    {                                                        ( _1 )}
| _1 = pattern _2 = AS _3 = IDENT
    {                         ( PAlias (_1, _3) )}
| _1 = pattern _2 = COMMA _3 = pattern
    {                             ( PPair (_1, _3) )}
| _1 = CONSTRUCTOR _2 = simple_pattern
    {                                  ( PConstruct (_1, _2) )}
| _1 = REF _2 = simple_pattern
    {                                                        ( PRef _2 )}
| _1 = pattern _2 = BAR _3 = pattern
    {                                                        ( POr (_1, _3) )}
235 236 237

label_pattern_list:
| 
238 239 240 241 242 243 244
    {                      ( StringMap.empty )}
| _1 = IDENT _2 = EQUAL _3 = pattern
    {                            ( StringMap.singleton _1 _3 )}
| _1 = IDENT _2 = EQUAL _3 = pattern _4 = SEMI _5 = label_pattern_list
    {                                                 ( 
                                                          let label = _1
                                                          and lpmap = _5 in
245 246 247 248
							  try
							    let _ = StringMap.lookup label lpmap in
							    raise (DuplicateLabel label)
							  with Not_found ->
249 250
							    StringMap.add label _3 lpmap
                                                        )}
251 252

simple_pattern_list:
253 254 255 256
| _1 = simple_pattern
    {                       ( [_1] )}
| _1 = simple_pattern _2 = simple_pattern_list
    {                                         ( _1 :: _2 )}
257 258

binding_list:
259 260 261 262
| _1 = binding _2 = AND _3 = binding_list
    {                                                        ( _1 :: _3 )}
| _1 = binding
    {                                                        ( [_1] )}
263 264

binding:
265 266 267 268
| _1 = pattern _2 = EQUAL _3 = expression
    {                                       ( (_1, _3) )}
| _1 = IDENT _2 = simple_pattern_list _3 = EQUAL _4 = expression
    {                                                        ( (PVar _1, EFun [_2, _4]) )}
269 270 271 272 273

%%