[antlr-interest] Bug with C++ charVocabulary option

Ric Klaren klaren at cs.utwente.nl
Fri May 31 04:04:33 PDT 2002


On Fri, May 31, 2002 at 11:08:39AM +0200, Ric Klaren wrote:
> Hmmmm basically the complete LA(x) traject needs to be made unsigned int,
> else we'll keep on having sign extension issues. It's something I already
> noted as potential trouble. Your probably the first to really stumble on
> it. Another approach is maybe adding a & 0xFF in a few places to strip away
> sign extenstions....

Try the attached patch it seems to work on my setup. Oh yeah for MSVC dll
you have to change the vector<int> in the top to vector<unsigned int>
noticed it too late.

Cheers,

Ric
--
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- klaren at cs.utwente.nl ----- +31 53 4893722  ----
-----+++++*****************************************************+++++++++-------
     Innovation makes enemies of all those who prospered under the old
   regime, and only lukewarm support is forthcoming from those who would
               prosper under the new. --- Niccolò Machiavelli


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 

-------------- next part --------------
Change 667 by klaren at klaren.hawking.main on 2002/05/31 04:00:42

	  Fixed some sign extension issues.

Affected files ...

... //depot/code/org.antlr/main/main/lib/cpp/antlr/BitSet.hpp#7 edit
... //depot/code/org.antlr/main/main/lib/cpp/src/BitSet.cpp#7 edit
... //depot/code/org.antlr/main/main/lib/cpp/src/MismatchedCharException.cpp#5 edit
... //depot/code/org.antlr/main/main/lib/cpp/src/MismatchedTokenException.cpp#5 edit

Differences ...

==== //depot/code/org.antlr/main/main/lib/cpp/antlr/BitSet.hpp#7 (ktext) ====

@@ -13,7 +13,7 @@
 extern template class ANTLR_API ANTLR_USE_NAMESPACE(std)vector<int>;
 extern template class ANTLR_API ANTLR_USE_NAMESPACE(std)vector<bool>;
 #endif
-	
+
 /**A BitSet to replace java.util.BitSet.
  * Primary differences are that most set operators return new sets
  * as opposed to oring and anding "in place".  Further, a number of
@@ -41,15 +41,15 @@
 	ANTLR_USE_NAMESPACE(std)vector<bool> storage;
 
 public:
-	BitSet(int nbits=64);
-	BitSet(const unsigned long* bits_,int nlongs);
+	BitSet( unsigned int nbits=64 );
+	BitSet( const unsigned long* bits_, unsigned int nlongs);
 	~BitSet();
 
-	void add(int el);
+	void add( unsigned int el );
 
-	bool member(int el) const;
+	bool member( unsigned int el ) const;
 
-	ANTLR_USE_NAMESPACE(std)vector<int> toArray() const;
+	ANTLR_USE_NAMESPACE(std)vector<unsigned int> toArray() const;
 };
 
 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE

==== //depot/code/org.antlr/main/main/lib/cpp/src/BitSet.cpp#7 (ktext) ====

@@ -26,50 +26,45 @@
  * @author Terence Parr, MageLang Institute
  * @author <br><a href="mailto:pete at yamuna.demon.co.uk">Pete Wells</a>
  */
-BitSet::BitSet(int nbits)
-	: storage(nbits)
+BitSet::BitSet(unsigned int nbits)
+: storage(nbits)
 {
-	for (int i=0;i<nbits;i++) {
+	for (unsigned int i = 0; i < nbits ; i++ )
 		storage[i] = false;
-	}
 }
 
-BitSet::BitSet(const unsigned long* bits_,int nlongs)
-	: storage(nlongs*32)
+BitSet::BitSet( const unsigned long* bits_, unsigned int nlongs )
+: storage(nlongs*32)
 {
-	for ( int i = 0 ; i < nlongs*32; i++) {
+	for ( unsigned int i = 0 ; i < (nlongs * 32); i++)
 		storage[i] = (bits_[i>>5] & (1UL << (i&31))) ? true : false;
-	}
 }
 
 BitSet::~BitSet()
 {
 }
 
-void BitSet::add(int el)
+void BitSet::add(unsigned int el)
 {
-	// the string constructor here is a workaround for some crummy compiler.
-	if ( el < 0 )
-		throw ANTLR_USE_NAMESPACE(std)out_of_range(ANTLR_USE_NAMESPACE(std)string("antlr::BitSet.cpp - BitSet::add()"));
-
-	if( static_cast<unsigned int>(el) >= storage.size() )
+	if( el >= storage.size() )
 		storage.resize( el+1, false );
 
 	storage[el] = true;
 }
 
-bool BitSet::member(int el) const
+bool BitSet::member(unsigned int el) const
 {
-	if ( el < 0 || static_cast<unsigned int>(el) >= storage.size())
+	if ( el >= storage.size())
 		return false;
 
 	return storage[el];
 }
 
-ANTLR_USE_NAMESPACE(std)vector<int> BitSet::toArray() const
+ANTLR_USE_NAMESPACE(std)vector<unsigned int> BitSet::toArray() const
 {
-	ANTLR_USE_NAMESPACE(std)vector<int> elems;
-	for (unsigned int i=0;i<storage.size();i++) {
+	ANTLR_USE_NAMESPACE(std)vector<unsigned int> elems;
+	for (unsigned int i = 0; i < storage.size(); i++)
+	{
 		if (storage[i])
 			elems.push_back(i);
 	}

==== //depot/code/org.antlr/main/main/lib/cpp/src/MismatchedCharException.cpp#5 (ktext) ====

@@ -113,8 +113,9 @@
 	case NOT_SET :
 		{
 			s += ANTLR_USE_NAMESPACE(std)string("expecting ") + (mismatchType == NOT_SET ? "NOT " : "") + "one of (";
-			ANTLR_USE_NAMESPACE(std)vector<int> elems = set.toArray();
-			for (int i = 0; i < (int) elems.size(); i++) {
+			ANTLR_USE_NAMESPACE(std)vector<unsigned int> elems = set.toArray();
+			for ( unsigned int i = 0; i < elems.size(); i++ )
+			{
 				s += " '";
 				s += charName(elems[i]);
 				s += "'";

==== //depot/code/org.antlr/main/main/lib/cpp/src/MismatchedTokenException.cpp#5 (ktext) ====

@@ -181,8 +181,8 @@
 	case NOT_SET:
 		{
 			s += ANTLR_USE_NAMESPACE(std)string("expecting ") + (mismatchType == NOT_SET ? "NOT " : "") + "one of (";
-			ANTLR_USE_NAMESPACE(std)vector<int> elems = set.toArray();
-			for (int i = 0; i < (int) elems.size(); i++)
+			ANTLR_USE_NAMESPACE(std)vector<unsigned int> elems = set.toArray();
+			for ( unsigned int i = 0; i < elems.size(); i++ )
 			{
 				s += " ";
 				s += tokenName(elems[i]);





More information about the antlr-interest mailing list