-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.mly
54 lines (47 loc) · 1.88 KB
/
parser.mly
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
/**************************************************************************/
/* */
/* Menhir */
/* */
/* François Pottier, INRIA Rocquencourt */
/* Yann Régis-Gianas, PPS, Université Paris Diderot */
/* */
/* Copyright 2005-2008 Institut National de Recherche en Informatique */
/* et en Automatique. All rights reserved. This file is distributed */
/* under the terms of the Q Public License version 1.0, with the change */
/* described in file LICENSE. */
/* */
/**************************************************************************/
%token <int> INT
%token PLUS MINUS TIMES DIV
%token LPAREN RPAREN
%token SEMICOLON
%token EOF
%left PLUS MINUS /* lowest precedence */
%left TIMES DIV /* medium precedence */
%nonassoc UMINUS /* highest precedence */
/* changed the type, because the script does not return one value, but all
* results which are calculated in the file */
%start <int list> main
%%
/* the calculated results are accumalted in an OCaml int list */
main:
| stmt = statement EOF { [stmt] }
| stmt = statement m = main { stmt :: m}
/* expressions end with a semicolon, not with a newline character */
statement:
| e = expr SEMICOLON { e }
expr:
| i = INT
{ i }
| LPAREN e = expr RPAREN
{ e }
| e1 = expr PLUS e2 = expr
{ e1 + e2 }
| e1 = expr MINUS e2 = expr
{ e1 - e2 }
| e1 = expr TIMES e2 = expr
{ e1 * e2 }
| e1 = expr DIV e2 = expr
{ e1 / e2 }
| MINUS e = expr %prec UMINUS
{ - e }