t046rewrite.g revision 324c4644fee44b9898524c09511bd33c3f12e2df
1grammar t046rewrite;
2options {
3    language=Python;
4}
5
6program
7@init {
8    start = self.input.LT(1)
9}
10    :   method+
11        {
12        self.input.insertBefore(start,"public class Wrapper {\n")
13        self.input.insertAfter($method.stop, "\n}\n")
14        }
15    ;
16
17method
18    :   m='method' ID '(' ')' body
19        {self.input.replace($m, "public void");}
20    ; 
21
22body
23scope {
24    decls
25}
26@init {
27    $body::decls = set()
28}
29    :   lcurly='{' stat* '}'
30        {
31        for it in $body::decls:
32            self.input.insertAfter($lcurly, "\nint "+it+";")
33        }
34    ;
35
36stat:   ID '=' expr ';' {$body::decls.add($ID.text);}
37    ;
38
39expr:   mul ('+' mul)* 
40    ;
41
42mul :   atom ('*' atom)*
43    ;
44
45atom:   ID
46    |   INT
47    ;
48
49ID  :   ('a'..'z'|'A'..'Z')+ ;
50
51INT :   ('0'..'9')+ ;
52
53WS  :   (' '|'\t'|'\n')+ {$channel=HIDDEN;}
54    ;
55