[antlr-interest] Why stream name can't be printed out when error occurs(ANTLR C)?

A Z asicaddress at gmail.com
Thu May 26 11:36:50 PDT 2011


For development, I handled tree grammar error reporting with the following
code. It simply searches back in the token stream until it hits an input
token and uses that for location information. I also print out the token
index offset between the error token and the reporting token so I get an
idea of where the error occurred. Note that I stopped changing this as soon
it was working enough to allow debugging the tree grammar so don't expect
much.


  pANTLR3_COMMON_TOKEN searchToken = NULL;
  unsigned int testIndex = 0;
  if(ex->index > 0)
  {
    testIndex = ex->index - 1;
    pANTLR3_BASE_TREE searchBaseTree =
thisTreeNodeStream->get(thisTreeNodeStream,testIndex);
    searchToken = searchBaseTree->getToken(searchBaseTree);


    //If UP token
    if(errorToken->type == 3)
    {
      //Go back in the stream until we hit a token that is not UP and has a
column not -1
      while(searchToken->type == 3 || searchToken->charPosition == -1)
      {
        testIndex--; //FIXME - Maybe dangerous?
        searchBaseTree =
thisTreeNodeStream->get(thisTreeNodeStream,testIndex);
        searchToken = searchBaseTree->getToken(searchBaseTree);
      //printf("searchToken->toString
%s\n",searchToken->toString(searchToken)->chars);
      }

    }
    //If DOWN token
    else if(errorToken->type == 2)
    {
      //Go back in the stream until we hit a token that is not UP
      while(searchToken->type == 2)
      {
        testIndex--; //FIXME - Maybe dangerous?
        searchBaseTree =
thisTreeNodeStream->get(thisTreeNodeStream,testIndex);
        searchToken = searchBaseTree->getToken(searchBaseTree);
     // printf("searchToken->toString
%s\n",searchToken->toString(searchToken)->chars);
      }
    }
    //If no column info(imaginary token?)
    else if(errorToken->charPosition == -1)
    {
      //Go back in the stream until we hit a token that is not UP and has a
column not -1
      while(searchToken->type == 3 || searchToken->type == 2 ||
searchToken->charPosition == -1)
      {
        testIndex--; //FIXME - Maybe dangerous?
        searchBaseTree =
thisTreeNodeStream->get(thisTreeNodeStream,testIndex);
        searchToken = searchBaseTree->getToken(searchBaseTree);
      //printf("searchToken->user1 %d\n",searchToken->user1);
     // printf("searchToken->type %d\n",searchToken->type);
     // printf("searchToken->toString
%s\n",searchToken->toString(searchToken)->chars);
      }
    }
    else
    {
      searchToken = errorToken;
    }
  }
  //This must be the first node in the tree
  else
  {
    searchToken = errorToken;
  }


More information about the antlr-interest mailing list