Loading ...
Sorry, an error occurred while loading the content.

Why ASTNULLType?

Expand Messages
  • james@eecs.ukans.edu
    I m going to re-report a problem I mentioned most recently on January 19. It has been present since at least 2.6.1 (when I started using ANTLR). Here s the
    Message 1 of 2 , May 8 2:16 PM
    • 0 Attachment
      I'm going to re-report a problem I mentioned most recently on January
      19. It has been present since at least 2.6.1 (when I started using
      ANTLR). Here's the synopsis:

      1) I am using ASTLabelType to change the default AST node type to a
      class called "SymbolAST".

      2) I have a rule in my tree walker that looks like this:

      varInitializer[int where]
      : #(ASSIGN {pr(#ASSIGN,where);} initializer[where])
      |
      ;

      3) ANTLR generates code for this rule that begins like this:

      public final void varInitializer(AST _t,
      int where
      ) throws RecognitionException {

      SymbolAST varInitializer_AST_in = (SymbolAST)_t;

      4) Due to the empty alternative in the rule, _t can be
      antlr.ASTNULLType. When that is the case, I get a
      ClassCastException at runtime.

      So, I considered how one might fix this problem. I noticed that none of
      the code generated by ANTLR seemed to use this variable declared at the
      beginning. It is emitted in antlr.JavaCodeGenerator.genRule(), lines
      2206-2209. So I snipped that out. This showed that the variable is
      used in one case, after all. It is needed to refer to #rulename in any
      initial code for the rule.

      Okay, so how about not casting it to a SymbolAST? That would be okay
      for me, but isn't a good general solution, because somebody might
      reasonably write initial code in a rule that depends on the cast.

      So now I am asking: why is _t an object of ASTNULLType instead of null
      for an empty alternative? If _t was null, the cast would succeed.
      Indeed, I see that in many places, ANTLR emits code checking whether _t
      is null, and setting it to ASTNULL if it is. What is the reason for the
      type's existence? And what is a good solution to my problem?

      Thanks,
      --
      Jerry James
    • Ric Klaren
      Hi, I also ran into the problem Jerry James reported (not quite the same but related on second glance). ... Although my impression was he was using the java
      Message 2 of 2 , May 23 10:01 AM
      • 0 Attachment
        Hi,

        I also ran into the problem Jerry James reported (not quite the same but
        related on second glance).

        > On Mon, May 08, 2000 at 04:16:53PM -0500, james@... wrote:

        Although my impression was he was using the java generated code (and I'm
        using C++).

        Some small snippets for the feel of the thing:

        with: ASTLabelType = "TFSP_ASTNode"

        void TFSP_Parser::start() {
        Tracer traceInOut(this, "start");
        returnAST = antlr::nullAST;
        antlr::ASTPair currentAST;
        TFSP_ASTNode start_AST = antlr::nullAST;
        ....

        'vanilla' ANTLR code

        void TFSP_Parser::start() {
        Tracer traceInOut(this, "start");
        returnAST = antlr::nullAST;
        antlr::ASTPair currentAST;
        antlr::RefAST start_AST = antlr::nullAST;

        As I look at this I get the impression that ASTLabelType is nice in theory
        but in practice can never work (for C++ that is, java I'm not sure... ok
        didn't look either :) )?

        One solution would be add a nullAST = "<whatever you want>" option. But
        maybe it's better to shove the nullAST thing into the AST class itself
        (static member). And get code like:

        void TFSP_Parser::start() {
        Tracer traceInOut(this, "start");
        returnAST = antlr::nullAST;
        antlr::ASTPair currentAST;
        TFSP_ASTNode start_AST = TFSP_ASTNode::nullAST;

        Since nullAST is only a picketfence kindoff thing, this seems a reasonable
        solution?

        And probably something similar for ASTNULL.. or... is it possible to make
        nullAST and ASTNULL the same thing (guess not)?

        <scribbled down hastily since I have to rush off part>
        In C++ one also might consider some template magic. I have a feeling that
        this might be a better solution that the AstLabelType thing (for C++).
        e.g. templatize the TreeParser and the Parser with the AST node thing keep
        the RefAst stuff internal to those classes. Put the ASTNULLType in the
        template.
        </scribbled down hastily since I have to rush off part>

        Any thoughts/comments?

        Regards,

        Ric

        PS The change with respect to antlr::nullAST into <ASTLabelType>::nullAST I
        could do tommorrow morning. So it would maybe possible to put that into
        2.7.1?
        --
        -----+++++*****************************************************+++++++++-------
        -- Ric Klaren - klaren@... ------------------------------------------
        -----+++++*****************************************************+++++++++-------
        'And this 'rebooting' business? Give it a good kicking, do you?' 'Oh, no,
        of course, we ... that is ... well, yes, in fact,' said Ponder. 'Adrian
        goes round the back and ... er ... prods it with his foot. But in a
        technical way,' he added. --- From: Hogfather by Terry Pratchett.
        -------------------------------------------------------------------------------
      Your message has been successfully submitted and would be delivered to recipients shortly.