Skip to content

Instantly share code, notes, and snippets.

@yamaguchi1024
Last active May 28, 2017 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamaguchi1024/510cf534e63bb3565f62e5da721f9b39 to your computer and use it in GitHub Desktop.
Save yamaguchi1024/510cf534e63bb3565f62e5da721f9b39 to your computer and use it in GitHub Desktop.
%{
open Syntax
%}
%token <int> INT
%token <bool> BOOL
%token <string> ID
%token LET IN
%token PLUS TIMES MINUS DIV
%token AND OR
%token EQ LT
%token IF THEN ELSE
%token LPAR RPAR
%token SEMISEMI
%start toplevel
%type <Syntax.command> toplevel
%%
toplevel:
| expr SEMISEMI { CExp $1 }
| LET var EQ expr SEMISEMI { CDecl ($2, $4) }
;
expr:
| LET var EQ expr IN expr { ELet($2,$4,$6) }
| IF expr THEN expr ELSE expr { EIf($2,$4,$6) }
| arith_expr EQ arith_expr { EEq($1,$3) }
| arith_expr LT arith_expr { ELt($1,$3) }
| arith_expr { $1 }
| bool_expr { $1 }
;
arith_expr:
| arith_expr PLUS factor_expr { EAdd($1,$3) }
| arith_expr MINUS factor_expr { ESub($1,$3) }
| factor_expr { $1 }
;
bool_expr:
| bool_expr AND atomic_expr { EAnd($1,$3) }
| bool_expr OR atomic_expr { EOr($1,$3) }
| atomic_expr { $1 }
;
factor_expr:
| factor_expr TIMES atomic_expr { EMul($1,$3) }
| factor_expr DIV atomic_expr { EDiv($1,$3) }
| atomic_expr { $1 }
;
atomic_expr:
| INT { EConstInt($1) }
| BOOL { EConstBool($1) }
| ID { EVar($1) }
| LPAR expr RPAR { $2 }
;
var:
| ID { $1 }
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment