t.g
来自「antlr最新版本V3源代码」· G 代码 · 共 51 行
G
51 行
grammar T;options { language=Python;}program : method ;method/* name is visible to any rule called by method directly or indirectly. * There is also a stack of these names, one slot for each nested * invocation of method. If you have a method nested within another * method then you have name strings on the stack. Referencing * $method.name access the topmost always. I have no way at the moment * to access earlier elements on the stack. */scope {name} : 'method' ID '(' ')' {$method::name=$ID.text;} body ; body: '{' stat* '}' ;stat: ID '=' expr ';' | method // allow nested methods to demo stack nature of dynamic attributes ;expr: mul ('+' mul)* ;mul : atom ('*' atom)* ;/** Demonstrate that 'name' is a dynamically-scoped attribute defined * within rule method. With lexical-scoping (variables go away at * the end of the '}'), you'd have to pass the current method name * down through all rules as a parameter. Ick. This is much much better. */atom: ID {print "ref "+$ID.text+" from method "+$method::name} | INT {print "int "+$INT.text+" in method "+$method::name} ;ID : ('a'..'z'|'A'..'Z')+ ;INT : '0'..'9'+ ;WS : (' '|'\t'|'\n')+ {$channel=HIDDEN} ;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?