[antlr-interest] Composing tree grammars

andreas.meyer at smartshift.de andreas.meyer at smartshift.de
Fri Feb 6 12:19:54 PST 2009


>On Feb 6, 2009, at 11:01 AM, Andreas Meyer wrote:
>
>> Hi!
>>
>> I am using ANTLR 3.1 and I want to compose tree grammars in a way,  
>> that I have one “base” grammar that contains all rules, in order to  
>> define a traversal, but without actions/rewrites. Then, a second  
>> grammar imports the “base” grammar, and overrides only some rules.  
>> Now, if I try to use the second grammar on the input,
>
>hi. what does that mean?
>Ter
>

Ok, I have 

* a base grammar SimpleC.g
* a tree grammar SimpleCWalker.g with a set of rules, mirroring the base grammar's result tree
* a tree grammar SimpleCWalker2.g that imports SimpleCWalker and overrides one rule (variable), 
* in Main.java, I call the start rule from SimpleCWalker, namely "program", but using the SimpleCWalker2:

SimpleCWalker2 walker = new SimpleCWalker2(nodes);
walker.program();

Btw, when generating SimpleCWalker2, ANTLR complains:

warning(138): SimpleCWalker2.g:0:0: grammar SimpleCWalker2: no start rule (no rule can obviously be followed by EOF)

When using the input file "input" from the example, eventually, the rule "program" calls the overridden rule "variable", where the ANTLR runtimes complain about some mismatched input:

$ java Main input
tree=(VAR_DEF char c) (VAR_DEF int x) (FUNC_DECL (FUNC_HDR void bar (ARG_DEF int x))) (FUNC_DEF (FUNC_HDR int foo (ARG_DEF int y) (ARG_DEF c
DEF int i) (for (= i 0) (< i 3) (= i (+ i 1)) (BLOCK (= x 3) (= y 5)))))
SimpleCWalker.g: node from line 1:0 mismatched input 'char' expecting set null

Debugging showed that the ANTLR runtimes expected an identifier, whereas the input stream (containing the generated tree nodes from the parser) still has a LT(1) of VAR_DEF.

I attach the (modified) simpleCTreewalker example in case you want to test it.

Thanks a lot for any hint!
A.Meyer
-------------- next part --------------
A non-text attachment was scrubbed...
Name: simplecTreeParser2.tgz
Type: application/octet-stream
Size: 1486 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20090206/6d8f5c6b/attachment.obj 


More information about the antlr-interest mailing list