[antlr-interest] Bug in Generated Parser?

Cameron Skinner camerons at microsoft.com
Wed Jul 16 22:36:51 PDT 2008


Hello,

I'm using the 3.1 beta release, CSharp2 target grammar.

I'm seeing different behavior running my parser with a syntax error the second time run. I've tracked it down to where I believe the problem is, located in BaseRecognizer.cs. The "follow.Remove( Token.EOR_TOKEN_TYPE )" logic is being called, which modifies the state of the static readonly BitSet object found on the parser. The problem with that is that even though I am creating a completely different parser instance, the static variable has been altered for as long as the assembly is in memory. I've copied a simple program to illustrate the problem at the bottom of this mail.

Is this a known problem?

Regards,

Cameron

public bool MismatchIsMissingToken(IIntStream input, BitSet follow) {
                  if (follow == null) {
                        // we have no information about the follow; we can only consume
                        // a single token and hope for the best
                        return false;
                  }

                  // compute what can follow this grammar element reference
                  if (follow.Member(Token.EOR_TOKEN_TYPE)) {
                        if (state.followingStackPointer >= 0) { // remove EOR if we're not the start symbol
                              follow.Remove(Token.EOR_TOKEN_TYPE); <<<<<<<<< THIS IS THE PROBLEM, modifies static variable
                        }
                        BitSet viableTokensFollowingThisRule = ComputeContextSensitiveRuleFOLLOW();
                        follow = follow.Or(viableTokensFollowingThisRule);
                  }

                  // if current token is consistent with what could come after set
                  // then we know we're missing a token; error recovery is free to
                  // "insert" the missing token

                  // BitSet cannot handle negative numbers like -1 (EOF) so I leave EOR
                  // in follow set to indicate that the fall of the start symbol is
                  // in the set (EOF can follow).
                  if ( follow.Member(input.LA(1)) || follow.Member(Token.EOR_TOKEN_TYPE) ) {
                        return true;
                  }
                  return false;
            }

Simple program that exhibits the fundamental problem of trying to use a static  readonly member. The state of that member can change, just not the instance itself:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
   class Program
   {
      static void Main( string[] args )
      {
         Testing t = new Testing();
         Console.WriteLine( Testing.test.isSet );

         Testing.test.isSet = true;
         Testing x = new Testing();

         Console.WriteLine( Testing.test.isSet );
      }
   }

   public class Testing
   {
      public static readonly Test test = new Test();
   }

   public class Test
   {
      public bool isSet = false;
   }
}


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080716/14a49ac1/attachment.html 


More information about the antlr-interest mailing list