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