petit-1.opp.exp 6.83 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
Warning: you are using the standard library and/or the %inline keyword. We
recommend switching on --infer in order to avoid obscure type error messages.
%{

  open MIPSOps
  open LPP
  open Primitive

%}
%start program
%token WRITELN
%token WRITE
%token WHILE
%token VAR
%token SEMICOLON
%token RPAREN
%token READLN
%token RBRACKET
%token PROGRAM
%token PROCEDURE
%token OF
%token NEW
%token LPAREN
%token INTEGER
%token <int32> INTCONST
%token IF
%token <string Location.t> ID
%token FUNCTION
%token END
%token DOT
%token DO
%token COMMA
%token COLONEQ
%token COLON
%token BOOLEAN
%token <bool> BOOLCONST
%token BEGIN
%token ARRAY
%token OR
%token AND
%token NOT
%token NE
%token LT
%token LE
%token GT
%token GE
%token EQ
%token PLUS
%token MINUS
%token TIMES
%token SLASH
%token LBRACKET
%token THEN
%token ELSE
%left OR 
%left AND 
%nonassoc NOT 
%nonassoc NE LT LE GT GE EQ 
%left PLUS MINUS 
%left TIMES SLASH 
%nonassoc unary_minus 
%nonassoc LBRACKET 
%nonassoc THEN 
%nonassoc ELSE 
%type <LPP.program> program
%%

typ:
| INTEGER
    { TypInt }
| BOOLEAN
    { TypBool }
| ARRAY OF t = typ
    { TypArray t }

expression:
| e = raw_expression
    { Location.make $startpos $endpos e }
| LPAREN e = expression RPAREN
    { e }

raw_expression:
| i = INTCONST
    { EConst (ConstInt i) }
| b = BOOLCONST
    { EConst (ConstBool b) }
| id = ID
    { EGetVar id }
| MINUS e = expression %prec unary_minus
    { EUnOp (UOpNeg, e) }
| e1 = expression PLUS e2 = expression
    {let op =
  ( OpAdd )
in
 EBinOp (op, e1, e2) }
| e1 = expression MINUS e2 = expression
    {let op =
  ( OpSub )
in
 EBinOp (op, e1, e2) }
| e1 = expression TIMES e2 = expression
    {let op =
  ( OpMul )
in
 EBinOp (op, e1, e2) }
| e1 = expression SLASH e2 = expression
    {let op =
  ( OpDiv )
in
 EBinOp (op, e1, e2) }
| e1 = expression LT e2 = expression
    {let op =
  ( OpLt )
in
 EBinOp (op, e1, e2) }
| e1 = expression LE e2 = expression
    {let op =
  ( OpLe )
in
 EBinOp (op, e1, e2) }
| e1 = expression GT e2 = expression
    {let op =
  ( OpGt )
in
 EBinOp (op, e1, e2) }
| e1 = expression GE e2 = expression
    {let op =
  ( OpGe )
in
 EBinOp (op, e1, e2) }
| e1 = expression EQ e2 = expression
    {let op =
  ( OpEq )
in
 EBinOp (op, e1, e2) }
| e1 = expression NE e2 = expression
    {let op =
  ( OpNe )
in
 EBinOp (op, e1, e2) }
| c = callee LPAREN xs0 = loption_separated_nonempty_list_COMMA_expression__ RPAREN
    {let actuals =
  let xs = xs0 in
  ( xs )
in
 EFunCall (Location.make $startpos(c) $endpos(c) c, actuals) }
| a = expression LBRACKET i = expression RBRACKET
    { EArrayGet (a, i) }
| NEW ARRAY OF t = typ LBRACKET e = expression RBRACKET
    { EArrayAlloc (t, e) }

condition:
| e = expression
    { CExpression e }
| c = nontrivial_condition
    { c }

lvalue:
| id = ID
    { fun e -> ISetVar (id, e) }
| a = expression LBRACKET i = expression RBRACKET
    { fun e -> IArraySet (a, i, e) }

procedure:
| FUNCTION f = ID LPAREN formals = separated_bindings RPAREN COLON t0 = typ SEMICOLON locals = variables body = block SEMICOLON
    {let result =
  let t = t0 in
  ( Some t )
in

      f, {
        formals = formals;
        result = result;
        locals = locals;
        body = body
      }
    }
| PROCEDURE f = ID LPAREN formals = separated_bindings RPAREN SEMICOLON locals = variables body = block SEMICOLON
    {let result =
  ( None )
in

      f, {
        formals = formals;
        result = result;
        locals = locals;
        body = body
      }
    }

separated_bindings:
| xs0 = loption_separated_nonempty_list_SEMICOLON_binding__
    {let bindings =
  let xs = xs0 in
  ( xs )
in
 List.flatten bindings }

terminated_bindings:
| bindings = nonempty_list_terminated_binding_SEMICOLON__
    { List.flatten bindings }

variables:
| vars = loption_preceded_VAR_terminated_bindings__
    { vars }

program:
| PROGRAM globals = variables defs = list_procedure_ main = block DOT
    {{
       globals = globals;
       defs = defs;
       main = main
    }}

loption_separated_nonempty_list_SEMICOLON_instruction__:
| 
    { [] }
| x = separated_nonempty_list_SEMICOLON_instruction_
    { x }

loption_separated_nonempty_list_SEMICOLON_binding__:
| 
    { [] }
| x = separated_nonempty_list_SEMICOLON_binding_
    { x }

loption_separated_nonempty_list_COMMA_expression__:
| 
    { [] }
| x = separated_nonempty_list_COMMA_expression_
    { x }

loption_preceded_VAR_terminated_bindings__:
| 
    { [] }
| VAR x0 = terminated_bindings
    {let x =
  let x = x0 in
  ( x )
in
 x }

list_procedure_:
| 
    { [] }
| x = procedure xs = list_procedure_
    { x :: xs }

nonempty_list_terminated_binding_SEMICOLON__:
| x0 = binding SEMICOLON
    {let x =
  let x = x0 in
  ( x )
in
 [ x ] }
| x0 = binding SEMICOLON xs = nonempty_list_terminated_binding_SEMICOLON__
    {let x =
  let x = x0 in
  ( x )
in
 x :: xs }

separated_nonempty_list_SEMICOLON_instruction_:
| x = instruction
    { [ x ] }
| x = instruction SEMICOLON xs = separated_nonempty_list_SEMICOLON_instruction_
    { x :: xs }

separated_nonempty_list_SEMICOLON_binding_:
| x = binding
    { [ x ] }
| x = binding SEMICOLON xs = separated_nonempty_list_SEMICOLON_binding_
    { x :: xs }

separated_nonempty_list_COMMA_expression_:
| x = expression
    { [ x ] }
| x = expression COMMA xs = separated_nonempty_list_COMMA_expression_
    { x :: xs }

separated_nonempty_list_COMMA_ID_:
| x = ID
    { [ x ] }
| x = ID COMMA xs = separated_nonempty_list_COMMA_ID_
    { x :: xs }

nontrivial_condition:
| NOT c = condition
    { CNot c }
| c1 = condition AND c2 = condition
    { CAnd (c1, c2) }
| c1 = condition OR c2 = condition
    { COr (c1, c2) }
| LPAREN c = nontrivial_condition RPAREN
    { c }

instruction:
| c = callee LPAREN xs0 = loption_separated_nonempty_list_COMMA_expression__ RPAREN
    {let actuals =
  let xs = xs0 in
  ( xs )
in
 IProcCall (Location.make $startpos(c) $endpos(c) c, actuals) }
| lvalue = lvalue COLONEQ e = expression
    { (lvalue : LPP.expression -> LPP.instruction) e }
| READLN LPAREN lvalue = lvalue RPAREN
    { let callee = Location.make $startpos($1) $endpos($1) (CPrimitiveFunction Readln) in
      let e = Location.make $startpos $endpos (EFunCall (callee, [])) in
      (lvalue : LPP.expression -> LPP.instruction) e }
| IF c = condition THEN b = instruction_or_block
    { IIf (c, b, ISeq []) }
| IF c = condition THEN b1 = instruction_or_block ELSE b2 = instruction_or_block
    { IIf (c, b1, b2) }
| WHILE c = condition DO b = instruction_or_block
    { IWhile (c, b) }

block:
| BEGIN xs0 = loption_separated_nonempty_list_SEMICOLON_instruction__ END
    {let is =
  let xs = xs0 in
  ( xs )
in
 ISeq is }

instruction_or_block:
| i = instruction
    { i }
| b = block
    { b }

callee:
| WRITE
    { CPrimitiveFunction Write }
| WRITELN
    { CPrimitiveFunction Writeln }
| id = ID
    { CUserFunction (Location.content id)}

binding:
| ids = separated_nonempty_list_COMMA_ID_ COLON t = typ
    { List.map (fun id -> (id, t)) ids }

%%