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

Loring Craymer lgcraymer at yahoo.com
Tue Oct 28 01:35:04 PDT 2008


Ter--

Indirect execution is a very weak form of polymorphism, and the mechanism you describe depends on compiler type awareness.

Assume that you have two versions of rule b, defined in separate but included grammars

b[int foo] : a b c ;  // version 1

b returns int :  d e f ; // version 2

These have different type signatures; in an OO system, b[int x] would translate to version 1, and z = b would imply version 2.  As you mention, interpretation depends on the message sent, and part of the message are the data type expectations.

On another note, ANTLR lacks the data abstractions that go with the OO idea of inheritance.  The mechanism implemented in ANTLR 3 looks much more like Python's module import facility than it does inheritance.

--Loring





________________________________
From: Terence Parr <parrt at cs.usfca.edu>
To: Loring Craymer <lgcraymer at yahoo.com>
Cc: antlr-interest Interest <antlr-interest at antlr.org>
Sent: Monday, October 27, 2008 10:49:16 PM
Subject: Re: [antlr-interest] Duplicate members when generating code with 2 import levels in Java with ANTLR-3.1.1



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/20081028/dd911758/attachment.html 


More information about the antlr-interest mailing list