antlr最新版本V3源代码

源代码在线查看: t.g

软件大小: 3471 K
上传用户: LotusAwhaT
关键词: antlr 版本 源代码
下载地址: 免注册下载 普通下载 VIP

相关代码

				/** Convert the simple input to be java code; wrap in a class,				 *  convert method with "public void", add decls.  This shows how to insert				 *  extra text into a stream of tokens and how to replace a token				 *  with some text.  Calling toString() on the TokenRewriteStream				 *  in Main will print out the original input stream.				 *				 *  Note that you can do the instructions in any order as the				 *  rewrite instructions just get queued up and executed upon toString().				 */				grammar T;				options {				    language=Python;				}								program				@init {				    start = self.input.LT(1)				}				    :   method+				        {				        self.input.insertBefore(start,"public class Wrapper {\n")				        # note the reference to the last token matched for method:				        self.input.insertAfter($method.stop, "\n}\n")				        }				    ;								method				    :   m='method' ID '(' ')' body				        {self.input.replace($m, "public void");}				    ; 								body				// decls is on body's local variable stack but is visible to				// any rule that body calls such as stat.  From other rules				// it is referenced as $body::decls				// From within rule body, you can use $decls shorthand				scope {				    decls				}				@init {				    $body::decls = set()				}				    :   lcurly='{' stat* '}'				        {				        # dump declarations for all identifiers seen in statement list				        for it in $body::decls:				            self.input.insertAfter($lcurly, "\nint "+it+";")				        }				    ;								stat:   ID '=' expr ';' {$body::decls.add($ID.text);} // track left-hand-sides				    ;								expr:   mul ('+' mul)* 				    ;								mul :   atom ('*' atom)*				    ;								atom:   ID				    |   INT				    ;								ID  :   ('a'..'z'|'A'..'Z')+ ;								INT :   ('0'..'9')+ ;								WS  :   (' '|'\t'|'\n')+ {$channel=HIDDEN;}				    ;							

相关资源