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

Re: Bug with return values?

Expand Messages
  • Jerry Patterson
    Markus, ... From: Markus Pilzecker For grammar rules like ... epos returns [int[] p] ... ; ... antlr [version 2.7.0] generates something like ...
    Message 1 of 1 , Mar 1, 2000
    • 0 Attachment
      Markus,

      ---last time you said
      From: Markus Pilzecker <mp@...>

      For grammar rules like

      -------------8<----------------------
      epos
      returns [int[] p]
      : p=pos
      ;
      -------------8<----------------------

      antlr [version 2.7.0] generates something like

      -------------8<----------------------
      public final int[] epos() throws RecognitionException, TokenStreamException {
      int[] p;


      try { // for error handling
      p=pos();
      }
      catch (RecognitionException ex) {
      reportError(ex);
      consume();
      consumeUntil(_tokenSet_4);
      }
      return p;
      }
      -------------8<----------------------

      , which again is complained by ``javac'' [version 1.2.2]

      -------------8<----------------------
      CFA_to_location.java:259: Variable p may not have been initialized.
      return p;
      ^
      -------------8<----------------------
      .

      As far as I can judge, ``int[] p;'' should be replaced by
      ``int[] p = null;'', which, for the purist, also is a hack -- well, at least a
      deterministic one.

      Now, I ask:
      - did I miss anything
      - is this all a bug of antlr 2.7.0
      - is javac 1.2.2 extraordinarily fussy
      - does anybody have a workaround
      ?


      Bye,

      Markus

      [Snip Ad]
      ---

      I have found that I have to initialize the variables by hand (unless
      there is a way to automate this in antlr that I am unaware of).

      So for your example (as you have suggested) put the assignment in the
      front. I don't really think it is much of a hack, I mean you would be
      communicating that somehow pos() had failed to the caller. Anyway,
      unless I've messed up the syntax, you should be able to do the following:

      epos
      returns [int[] p]
      :
      { p = null; }
      p=pos
      ;

      I wouldn't necessarily consider this an antlr *bug* as much as it is an
      annoyance. It would be nice if antlr *did* have an option to generate
      initialization (it may and I don't know it though) to avoid this problem.

      Jerry
      --- <pre>
      Jeremiah S Patterson | It is not likely that one would die for a
      jerpat@... --+-- righteous man; for a good man someone might
      jerpat@... | dare to die. Yet while we were still sinners,
      jerpatter@... | Christ died for us.
      jerpat@... | Romans 5:7-8 </pre>
    Your message has been successfully submitted and would be delivered to recipients shortly.