parser-menhir.mly 1.34 KB
Newer Older
1 2 3 4
%token <int> INT
%token PLUS MINUS TIMES DIV
%token LPAREN RPAREN
%token EOL
5
%token DOT COMMA
6 7 8 9 10

%left PLUS MINUS        /* lowest precedence */
%left TIMES DIV         /* medium precedence */
%nonassoc UMINUS        /* highest precedence */

11
%type<Aux.annotations> annotations
12 13 14
%start<Aux.main> main

%{ open Aux %}
15 16 17 18

%%

main:
19
| n = nothing e = expr EOL
20
    { ($startpos, $endpos), n, e, $symbolstartpos }
21 22

/* Added just to exercise productions with an empty right-hand side. */
23
%inline nothing:
24
| /* nothing */
25
    { ($startpos, $endpos) }
26 27 28

/* Added just to exercise productions with an empty right-hand side, in a choice. */
optional_dot:
29 30
| n = nothing
    { ($startpos, $endpos), Some n }
31
| DOT
32
    { ($startpos, $endpos), None }
33

34
%inline optional_comma:
35 36
| n = nothing
    { ($startpos, $endpos), Some n }
37
| COMMA
38
    { ($startpos, $endpos), None }
39

40
%inline annotations:
41
  optional_dot optional_comma
42
    { ($startpos, $endpos),
43
      $endpos($1), $startpos($2), $startofs, $symbolstartpos,
44
      $1, $2 }
45

46 47
raw_expr:
| INT
48 49
    { EInt }
| a = annotations LPAREN n = nothing e = expr RPAREN o = optional_dot
50
    { EParen(a, n, e, o, $symbolstartofs) }
51 52 53 54
| expr PLUS expr
| expr MINUS expr
| expr TIMES expr
| expr DIV expr
55
    { EBinOp ($1, $3) }
56
| MINUS expr %prec UMINUS
57
    { EUnOp $2 }
58

59
%inline expr:
60
  e = raw_expr
POTTIER Francois's avatar
POTTIER Francois committed
61
    { ($startpos, $endpos), $endpos($0), $endofs($0), e }
62