cime-toplevel.opp.exp 4.18 KB
Newer Older
1 2 3 4 5 6
%{

open Abstract_syntax;;

%}
%start command
7 8
%token AND
%token ARROW
9
%token COMMA
10 11 12 13 14 15 16 17 18 19
%token CONCAT
%token DIRECTIVE
%token ELSE
%token EOF
%token EQUAL
%token FALSE
%token FUN
%token GE
%token GT
%token <string> IDENT
20
%token IF
21
%token <Numbers.t> INTEGER
22
%token LE
23 24 25 26
%token LEFTBRACE
%token LEFTPAR
%token LET
%token LT
27
%token MINUS
28 29 30 31 32 33 34
%token NEQ
%token NOT
%token OR
%token PLUS
%token RIGHTBRACE
%token RIGHTPAR
%token SEMICOLON
35 36
%token STAR
%token <string> STRING
37 38
%token THEN
%token TRUE
39 40 41 42 43 44 45 46 47 48
%nonassoc ARROW IF
%left OR
%left AND
%left NOT
%left EQUAL GE GT LE LT NEQ
%left MINUS PLUS
%left STAR
%nonassoc UMINUS
%nonassoc CONCAT FALSE FUN IDENT INTEGER LEFTBRACE LEFTPAR STRING TRUE
%left APPLY
49 50 51 52
%type <Abstract_syntax.abstract_command> command
%%

command:
53
  _1 = EOF
54 55 56
    {                        ( raise End_of_file )}
| _1 = command_aux _2 = SEMICOLON
    {                        ( _1 )}
57 58

command_aux:
59
  _1 = LET _2 = IDENT _3 = EQUAL _4 = expr
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    {                                ( Def(_2,_4) )}
| _1 = LET _2 = LEFTPAR _3 = identlist _4 = RIGHTPAR _5 = EQUAL _6 = expr
    {                                                     ( Deftuple(_3,_6) )}
| _1 = LET _2 = FUN _3 = IDENT _4 = args _5 = EQUAL _6 = expr
    {                                ( Deffun(_3,_4,_6) )}
| _1 = expr
    {                                ( Eval(_1) )}
| _1 = DIRECTIVE _2 = IDENT
    {                                ( Directive(_2,"") )}
| _1 = DIRECTIVE _2 = IDENT _3 = STRING
    {                                ( Directive(_2,_3) )}
| _1 = DIRECTIVE _2 = IDENT _3 = IDENT
    {                                ( Directive(_2,_3) )}
| _1 = DIRECTIVE _2 = IDENT _3 = INTEGER
    {                                ( Directive(_2,Numbers.to_string _3) )}
75 76

identlist:
77
  _1 = IDENT
78 79 80
    {                                  ( [_1] )}
| _1 = IDENT _2 = COMMA _3 = identlist
    {                                  ( _1::_3 )}
81 82

args:
83
  _1 = IDENT
84 85 86
    {                            ( [_1] )}
| _1 = IDENT _2 = args
    {                            ( _1::_2 )}
87 88

expr:
89
  _1 = IF _2 = expr _3 = THEN _4 = expr _5 = ELSE _6 = expr %prec IF
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
    {    ( If(_2,_4,_6) )}
| _1 = expr _2 = simple_expr %prec APPLY
    {    ( Apply(_1,_2) )}
| _1 = FUN _2 = IDENT _3 = ARROW _4 = expr
    {    ( Fun(_2,_4) )}
| _1 = expr _2 = PLUS _3 = expr
    {    ( Apply(Apply(Var("+"),_1),_3) )}
| _1 = expr _2 = MINUS _3 = expr
    {    ( Apply(Apply(Var("-"),_1),_3) )}
| _1 = MINUS _2 = expr %prec UMINUS
    {    ( Apply(Var("_minus"),_2) )}
| _1 = expr _2 = STAR _3 = expr
    {    ( Apply(Apply(Var("*"),_1),_3) )}
| _1 = expr _2 = EQUAL _3 = expr
    {    ( Apply(Apply(Var("="),_1),_3) )}
| _1 = expr _2 = NEQ _3 = expr
    {                          ( Apply(Apply(Var("<>"),_1),_3) )}
| _1 = expr _2 = GE _3 = expr
    {                          ( Apply(Apply(Var(">="),_1),_3) )}
| _1 = expr _2 = GT _3 = expr
    {                          ( Apply(Apply(Var(">"),_1),_3) )}
| _1 = expr _2 = LE _3 = expr
    {                          ( Apply(Apply(Var("<="),_1),_3) )}
| _1 = expr _2 = LT _3 = expr
    {                          ( Apply(Apply(Var("<"),_1),_3) )}
| _1 = expr _2 = AND _3 = expr
    {                          ( Apply(Apply(Var("and"),_1),_3) )}
| _1 = expr _2 = OR _3 = expr
    {                          ( Apply(Apply(Var("or"),_1),_3) )}
| _1 = expr _2 = CONCAT _3 = expr
    {                              ( Apply(Apply(Var("^"),_1),_3) )}
| _1 = NOT _2 = expr
    {                          ( Apply(Var("not"),_2) )}
| _1 = simple_expr
    {      ( _1 )}
125 126

simple_expr:
127
  _1 = IDENT
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    {    ( Var(_1) )}
| _1 = INTEGER
    {    ( Integer(_1) )}
| _1 = TRUE
    {    ( Bool(true) )}
| _1 = FALSE
    {    ( Bool(false) )}
| _1 = STRING
    {    ( String(_1) )}
| _1 = LEFTPAR _2 = expr _3 = RIGHTPAR
    {    ( _2 )}
| _1 = LEFTPAR _2 = expr _3 = COMMA _4 = commalist _5 = RIGHTPAR
    {      ( Tuple(_2::_4) )}
| _1 = LEFTBRACE _2 = RIGHTBRACE
    {   ( Set([]) )}
| _1 = LEFTBRACE _2 = semicolonlist _3 = RIGHTBRACE
    {   ( Set(_2) )}
145 146

commalist:
147
  _1 = expr
148 149 150
    {    ( [_1] )}
| _1 = expr _2 = COMMA _3 = commalist
    {    ( _1::_3 )}
151 152

semicolonlist:
153
  _1 = expr
154 155 156 157 158
    {    ( [_1] )}
| _1 = expr _2 = SEMICOLON
    {    ( [_1] )}
| _1 = expr _2 = SEMICOLON _3 = semicolonlist
    {    ( _1::_3 )}
159 160 161 162

%%