examples: annotate examples
[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 = '"' (Any - '"')* '"';
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         {lit:} int |
31         {par:} '(' e ')' |
32         {var:} id |
33         {read:} 'read' '(' ')'
34 Unary
35         {neg:} '-' e
36 Left
37         {mul:} [left:]e '*' [right:]e |
38         {div:} [left:]e '/' [right:]e
39 Right
40         {add:} [left:]e '+' [right:]e |
41         {sub:} [left:]e '-' [right:]e ;
42
43 c =
44         {eq:} [left:]e '=' [right:]e |
45         {ne:} [left:]e '!=' [right:]e |
46         {lt:} [left:]e '<' [right:]e |
47         {le:} [left:]e '<=' [right:]e |
48         {gt:} [left:]e '>' [right:]e |
49         {ge:} [left:]e '>=' [right:]e
50 Unary
51         {not:} 'not' c
52 Left
53         {and:} [left:]c '&&' [right:]c
54 Left
55         {or:} [left:]c '||' [right:]c ;