parser-ocamlyacc.mly 1.41 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
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

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

%type<unit> main
%start main

%%

main:
| nothing expr EOL
    {}

/* Added just to exercise productions with an empty right-hand side. */
nothing:
| /* nothing */
    { Aux.print "nothing" (Parsing.symbol_start_pos()) (Parsing.symbol_end_pos()) }

/* Added just to exercise productions with an empty right-hand side, in a choice. */
optional_dot:
| nothing
    { Aux.print "optional_dot" (Parsing.symbol_start_pos()) (Parsing.symbol_end_pos())}
| DOT
    { Aux.print "optional_dot" (Parsing.symbol_start_pos()) (Parsing.symbol_end_pos())}

32
33
34
35
36
37
38
39
40
41
optional_comma:
| nothing
    { Aux.print "optional_comma" (Parsing.symbol_start_pos()) (Parsing.symbol_end_pos())}
| COMMA
    { Aux.print "optional_comma" (Parsing.symbol_start_pos()) (Parsing.symbol_end_pos())}

annotations:
  optional_dot optional_comma
    { Aux.print "annotations" (Parsing.symbol_start_pos()) (Parsing.symbol_end_pos())}

42
43
44
raw_expr:
| INT
    {}
45
| annotations LPAREN nothing expr RPAREN optional_dot
46
    {}
POTTIER Francois's avatar
POTTIER Francois committed
47
| expr PLUS expr
48
    {}
POTTIER Francois's avatar
POTTIER Francois committed
49
| expr MINUS expr
50
    {}
POTTIER Francois's avatar
POTTIER Francois committed
51
| expr TIMES expr
52
    {}
POTTIER Francois's avatar
POTTIER Francois committed
53
| expr DIV expr
54
55
56
57
58
59
60
61
    {}
| MINUS expr %prec UMINUS
    {}

expr:
  raw_expr
    { Aux.print "expr" (Parsing.symbol_start_pos()) (Parsing.symbol_end_pos()) }