cime-toplevel.mly 3.28 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
/***************************************************************************

Parser for toplevel input

CiME Project - Dmons research team - LRI - Universit Paris XI

$Id: toplevel_parser.mly,v 1.17 2002/12/05 10:43:43 contejea Exp $

***************************************************************************/

%{

open Abstract_syntax;;

%}



%token <string> IDENT 
%token <Numbers.t> INTEGER
%token <string> STRING
%token LET FUN ARROW
%token IF THEN ELSE
%token AND OR NOT TRUE FALSE
%token PLUS STAR MINUS 
%token CONCAT
%token GE GT LE LT NEQ
%token LEFTPAR RIGHTPAR SEMICOLON COMMA LEFTBRACE RIGHTBRACE
%token EQUAL
%token DIRECTIVE
%token EOF

%start command
%type  <Abstract_syntax.abstract_command> command

%nonassoc ARROW IF
%left OR
%left AND
%left NOT
%left GE GT LE LT NEQ EQUAL
%left PLUS MINUS
%left STAR
%nonassoc UMINUS
%nonassoc IDENT INTEGER STRING FUN LEFTPAR LEFTBRACE CONCAT TRUE FALSE
%left APPLY


%%

command: 
  EOF                   { raise End_of_file }
| command_aux SEMICOLON { $1 }
;

command_aux:
| LET IDENT EQUAL expr          { Def($2,$4) }
| LET LEFTPAR identlist RIGHTPAR EQUAL expr          { Deftuple($3,$6) }
| LET FUN IDENT args EQUAL expr { Deffun($3,$4,$6) }
| expr                          { Eval($1) }
| DIRECTIVE IDENT               { Directive($2,"") }
| DIRECTIVE IDENT STRING        { Directive($2,$3) }
| DIRECTIVE IDENT IDENT         { Directive($2,$3) }
| DIRECTIVE IDENT INTEGER       { Directive($2,Numbers.to_string $3) }
;

identlist:
| IDENT                           { [$1] }
| IDENT COMMA identlist           { $1::$3 }
; 

args:
  IDENT                     { [$1] }
| IDENT args                { $1::$2 }
;

expr:
| IF expr THEN expr ELSE expr %prec IF
    { If($2,$4,$6) }
| expr simple_expr %prec APPLY 
    { Apply($1,$2) }
| FUN IDENT ARROW expr    
    { Fun($2,$4) }
| expr PLUS expr          
    { Apply(Apply(Var("+"),$1),$3) }
| expr MINUS expr  
    { Apply(Apply(Var("-"),$1),$3) }
| MINUS expr %prec UMINUS
    { Apply(Var("_minus"),$2) }
| expr STAR expr        
    { Apply(Apply(Var("*"),$1),$3) }
| expr EQUAL expr         
    { Apply(Apply(Var("="),$1),$3) }
| expr NEQ expr           { Apply(Apply(Var("<>"),$1),$3) }
| expr GE expr            { Apply(Apply(Var(">="),$1),$3) }
| expr GT expr            { Apply(Apply(Var(">"),$1),$3) }
| expr LE expr            { Apply(Apply(Var("<="),$1),$3) }
| expr LT expr            { Apply(Apply(Var("<"),$1),$3) }
| expr AND expr           { Apply(Apply(Var("and"),$1),$3) }
| expr OR expr            { Apply(Apply(Var("or"),$1),$3) }
| expr CONCAT expr            { Apply(Apply(Var("^"),$1),$3) }
| NOT expr                { Apply(Var("not"),$2) }
| simple_expr     
      { $1 }
;

simple_expr:
| IDENT                      
    { Var($1) }
| INTEGER                    
    { Integer($1) }
| TRUE                       
    { Bool(true) }
| FALSE                      
    { Bool(false) } 
| STRING                     
    { String($1) }
| LEFTPAR expr RIGHTPAR      
    { $2 }
| LEFTPAR expr COMMA commalist RIGHTPAR 
      { Tuple($2::$4) }
| LEFTBRACE RIGHTBRACE
	  { Set([]) }
| LEFTBRACE semicolonlist RIGHTBRACE
	  { Set($2) }
;

commalist:
| expr  
    { [$1] }
| expr COMMA commalist
    { $1::$3 }
;

semicolonlist:
| expr  
    { [$1] }
| expr SEMICOLON
    { [$1] }
| expr SEMICOLON semicolonlist
    { $1::$3 }
;