nitcc: introduce nitcc
[nit.git] / contrib / nitcc / examples / minilang.sablecc
1 /* Grammar of a mini procedural programming language */
2 Grammar minilang;
3
4 Lexer
5 l = 'a'..'z'|'A'..'Z'|'_';
6 d = '0'..'9';
7 id = l (l|d)*;
8 int = d+;
9 str = '"' (' '..'~' - '"')* '"';
10 blank = ' ' | '\n' | '\t';
11
12 Parser
13 Ignored blank;
14
15 prog = s*;
16
17 s =
18         {assign:} id '=' e ';' |
19         {print:} 'print' '(' e ')' ';' |
20         {print_str:} 'print' '(' str ')' ';' |
21         {println:} 'println' '(' ')' ';' |
22         {while:} 'while' '(' c ')' stmts |
23         {if:} 'if' '(' c ')' [then:]stmts else? ;
24
25 stmts = '{' s* '}' ;
26
27 else = 'else' stmts;
28
29 e = 
30         {add:} [left:]e '+' [right:]e2 |
31         {sub:} [left:]e '-' [right:]e2 |
32         e2 {->e2};
33 e2 {->e} =
34         {mul:} [left:]e2 '*' [right:]e3 |
35         {div:} [left:]e2 '/' [right:]e3 |
36         e3 {->e3};
37 e3 {->e} =
38         {neg:} '-' e3 |
39         {lit:} int |
40         {par:} '(' e ')' |
41         {var:} id |
42         {read:} 'read' '(' ')' ;
43
44 c =
45         {or:} [left:]c '||' [right:]c2 |
46         c2 {->c2};
47 c2 {->c} =
48         {and:} [left:]c2 '&&' [right:]c3 |
49         c3 {->c3};
50 c3 {->c} =
51         {not:} 'not' [c:]c3 |
52         {eq:} [left:]e '=' [right:]e |
53         {ne:} [left:]e '!=' [right:]e |
54         {lt:} [left:]e '<' [right:]e |
55         {le:} [left:]e '<=' [right:]e |
56         {gt:} [left:]e '>' [right:]e |
57         {ge:} [left:]e '>=' [right:]e ;