[antlr-interest] Duplicate members when generating code with 2 import levels in Java with ANTLR-3.1.1

Terence Parr parrt at cs.usfca.edu
Mon Oct 27 22:49:16 PDT 2008


On Oct 27, 2008, at 9:50 PM, Loring Craymer wrote:

> Ter
>
> Interesting.  I thought that you just imported the production  
> methods, but if you truly support polymorphism, then rules are  
> imported not just on the basis of their names but on their  
> signatures as well.  Somehow I missed that in the code.
>
> Are you sure that grammar composition does not look more like module  
> importation?

Well, not sure what you call it but manual explains it as:
> Rules in delegate grammars that reference rules overridden in a  
> delegator reference the overridden rule. This is identical to the  
> rules for overridden methods in an object oriented programming  
> language.
>
Here is a unit test.  Now that i look at it, it sucks but it does  
indicate that it matches "c" via S.a then M.b from M.  S.b does not  
match 'c' (or anything actually).

     public void testDelegatorRuleOverridesDelegate() throws Exception {
         String slave =
             "parser grammar S;\n" +
             "a : b {System.out.println(\"S.a\");} ;\n" +
             "b : B ;\n" ;
         mkdir(tmpdir);
         writeFile(tmpdir, "S.g", slave);
         String master =
             "grammar M;\n" +
             "import S;\n" +
             "b : 'b'|'c' ;\n" +
             "WS : (' '|'\\n') {skip();} ;\n" ;
         String found = execParser("M.g", master, "MParser", "MLexer",
                                   "a", "c", debug);
         assertEquals("S.a\n", found);
     }

So if you have an S parser, a invokes S.b.  BUT, if you import S into  
M and then invoke 'a' from M, you get S.a calling S.b.  It's using  
delegate pointers.  Basically M defines

a() { gS.a(); }

and S defines:

b() { gParent.b(); }

if I remember.  So, it behaves as you'd expect: "the receiver, M,  
decides how to answer message b".  If I'm an S, a calls S.b.  If I'm  
an M, a calls M.b.  the gS and gParent act sort of like a vtable I  
think.

Ter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081027/43aeca00/attachment.html 


More information about the antlr-interest mailing list