[antlr-interest] ANTLR Examples StdCParser.g for C++

David Wigg wiggjd at sbu.ac.uk
Fri Nov 23 07:36:04 PST 2001


Hello,

I am trying to convert the StdCParser.g example to produce C++ code.

I'm sorry to have to admit defeat but after a month without any
significant progress I have to ask for help again.

I have, of course, had a look round for any documentation that might
help me but all I can find is documentation about how to use the system,
but not about how it works.

This problem concerns the makeToken(int t) function in the lexer part of
StdCParser.g.

I cannot find out how to create a RefToken which points to my CToken
(which is a subclass of CommonToken which is a subclass of Token).

I attach copies of my attempt at CToken.cpp and CToken.hpp.

The compiler error on my latest version of the start of the lexer (shown
below) is;

d:\java\antlr-2.7.0\examples\cpp\c\stdclexer.hpp(77) : error C2440:
'type cast' : cannot convert from 'class antlr::RefCount<class
antlr::Token>' to 'class antlr::CToken'

I cannot work out the significance of "typedef RefToken
(*factory_type)();" in CharScanner.hpp and various references to
factory_type, tokenFactory, factory etc. etc.

Having no comments at all in code makes it difficult for us, mere
mortals, to work out what could be going on!

I've no doubt there is any easy answer.

For instance, is there a way to to use CharScanner::makeToken() create a
RefToken pointing to a CToken?

Ric Klaren suggested I would need a factory in CToken but I regret I
don't really know how to use it.

This is latest version of the start of my StdCParser lexer;

class StdCLexer extends Lexer;

options
        {
        k = 3;
        exportVocab = STDC;
        testLiterals = false;
		
		// DW 24/8/01 Included to try to clear warnings
        charVocabulary = '\3'..'\377';
        }

{
  ANTLR_USE_NAMESPACE(antlr)LineObject lineObject; 
  ANTLR_USE_NAMESPACE(std)string originalSource; 
  // DW 12/11/01 Not required initially - see correspondence
  //PreprocessorInfoChannel preprocessorInfoChannel = new
PreprocessorInfoChannel();
  int tokenNumber;
  bool countingTokens; 
  int deferredLineCount; 

  public:
  
  void setCountingTokens(bool ct) 
  {
    countingTokens = ct;
    if ( countingTokens ) {
      tokenNumber = 0;
    }
    else {
      tokenNumber = 1;
    }
  }

  void setOriginalSource(ANTLR_USE_NAMESPACE(std)string src) 
  {
    originalSource = src;
    lineObject.setSource(src);
  }
  void setSource(ANTLR_USE_NAMESPACE(std)string src) 
  {
    lineObject.setSource(src);
  }
  /* Not required initially
  public PreprocessorInfoChannel getPreprocessorInfoChannel() 
  {
    return preprocessorInfoChannel;
  }

  public void setPreprocessingDirective(ANTLR_USE_NAMESPACE(std)string
pre)
  {
    preprocessorInfoChannel.addLineForTokenNumber( pre, new
Integer(tokenNumber) );
  }
  */
protected: 
  ANTLR_USE_NAMESPACE(antlr)RefToken makeToken(int t)
  {
    if ( t != ANTLR_USE_NAMESPACE(antlr)Token.SKIP && countingTokens) {
        tokenNumber++;
    }

    //ANTLR_USE_NAMESPACE(antlr)RefToken tok =
CharScanner::makeToken(t);
    ANTLR_USE_NAMESPACE(antlr)CToken tok =
(ANTLR_USE_NAMESPACE(antlr)CToken)ANTLR_USE_NAMESPACE(antlr)CToken::factory();
	tok->setLine(lineObject.line);
    tok->setSource(lineObject.source);
    tok->setTokenNumber(tokenNumber);

    lineObject.line += deferredLineCount;
    deferredLineCount = 0;
    return tok;
  }

public:
	void deferredNewline() { 
        deferredLineCount++;
    }

    void newline() { 
        lineObject.newline();
    }
}
 

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: CToken.hpp
Type: application/x-unknown-content-type-hppfile
Size: 979 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20011123/a84583e5/CToken.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CToken.cpp
Type: application/x-unknown-content-type-cppfile
Size: 1051 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20011123/a84583e5/CToken-0001.bin


More information about the antlr-interest mailing list