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