How can you add member variables to the parser/lexer class definitions of ANTLR 2.x?
Created May 4, 2012
Ian Kaplan
Member variables and methods can be added by including a bracketed section after the options section that follows the class definition. For example, for the parser:
class JavaParser extends Parser; options { k = 2; // two token lookahead exportVocab=Java; // Call its vocabulary "Java" codeGenMakeSwitchThreshold = 2; // Some optimizations codeGenBitsetTestThreshold = 3; defaultErrorHandler = false; // Don't generate parser error handler s } { /* Class variables or methods go here */ private int myvar; public int getMyVar() { return myvar; } }
Included below is some C++ code to extend the scanner to keep track of columns:
class JavaLexer extends Lexer; options { exportVocab=Java; // call the vocabulary "Java" testLiterals=false; // don't automatically test for literals k=4; // four characters of lookahead } { // // Start local extensions to the scanner // private: int column; // text column number (numbered from 1) public: // Extension specific initialization. This is included // since the class constructor is automaticly generated. void init() { column = 0; } int getCol() { return column; } void consume() { CharScanner::consume(); // call consume() in the base class if (inputState->guessing == 0) { column++; } } void newline() { CharScanner::newline(); // call newline() in the base class column = 1; } RefToken makeToken( int t ) { RefToken tok = CharScanner::makeToken( t ); tok->setColumn( column ); return tok; } // // end local scanner extensions // }