[antlr-interest] Re: Short circuit of the lexer

xadeck <decoret at graphics.lcs.mit.edu> decoret at graphics.lcs.mit.edu
Sat Jan 18 05:06:52 PST 2003


--- In antlr-interest at yahoogroups.com, Terence Parr <parrt at j...> wrote:
> 
> 
> 
> Hi.  First, can you give us the simple rules that matched the array?  
> There may be a more efficient way to specify the language.

The grammar is a complex stuff. I extracted the part for arrays:
I guess values rule could be write as (INT)* but then I don't know how
to push the integers read into my result vector. I don't want to build
an AST unless really necessary (some files I parse have 180 000 lines
and the AST may result in high memory cost).



options {
    language="Cpp";
}
{
#include <deque>

using namespace std;

static vector<int> result;

}
class MyParser extends Parser;
options
{
    k=2;
}
file:  (decl)*
    ;

decl : Id LBRACKET values RBRACKET
    ;

values: i1:INT
        {
            result.push_back(atoi(i1->getText().c_str()));
        }
    | i2:INT
        {           
            result.push_back(atoi(i2->getText().c_str()));
        }
        values
    ;
class MyLexer extends Lexer;
options
{
    k=2;
}
{
    protected:
    bool parsingList;
}
LBRACKET : '[';
RBRACKET : ']';

WS  :   (' '
    |   '\t'
    |   '\n'
    |   '\r')
        { $setType(ANTLR_USE_NAMESPACE(antlr)Token::SKIP); }
    ;

INT :   ('0'..'9')+
    ;

Id : ('a'..'z')+
    ;



> Terence
> --
> Co-founder, http://www.jguru.com
> Creator, ANTLR Parser Generator: http://www.antlr.org
> Lecturer in Comp. Sci., University of San Francisco


 

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



More information about the antlr-interest mailing list