1 grammar t046rewrite; 2 options { 3 language=Python; 4 } 5 6 program 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 17 method 18 : m='method' ID '(' ')' body 19 {self.input.replace($m, "public void");} 20 ; 21 22 body 23 scope { 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 36 stat: ID '=' expr ';' {$body::decls.add($ID.text);} 37 ; 38 39 expr: mul ('+' mul)* 40 ; 41 42 mul : atom ('*' atom)* 43 ; 44 45 atom: ID 46 | INT 47 ; 48 49 ID : ('a'..'z'|'A'..'Z')+ ; 50 51 INT : ('0'..'9')+ ; 52 53 WS : (' '|'\t'|'\n')+ {$channel=HIDDEN;} 54 ; 55