[antlr-interest] Selective ignoring of whitespace

Ratul Bhadury hola.ratul at gmail.com
Thu May 14 10:02:56 PDT 2009


Hello all,

I've been trying to achieve something using Antlr v3, but have been
struggling for a while now, and think its now time to ask the help of more
experienced users, hence this mail.

What I basically want to achieve is a simple parser which will accept lines
of the nature:

sec = <keyword>

I want any whitespace (spaces and tabs) before the '=' sign to be ignored,
but any spaces after '=' to be included in what will be regarded as the
keyword. Therefore:
sec= hello    -> is valid
sec      =hello     people  -> is valid
         sec     =     hello     -> is valid
se=    hello   -> is rejected

However, I cannot get the grammar to ignore the spaces before the '=' sign,
and when I try parsing the input line, I get an error.

I would be extremely grateful if someone could point me in the right
direction, or even better, identify the error in the grammar. Below is my
attempt.

Many thanks in advance,

Ratul



/////////////////////////////////////////////////////////////////////////////////////////

grammar MopedSearch;

options
{
    language=CSharp2;
}

@lexer::members {

private bool myGotEqualsOp = false;

}



@header {
using System.Collections.Generic;
}

@members {

private string[] myValidDateFormats = new string[1] { "yyyyMMdd" };

}

@rulecatch {
    catch (RecognitionException re) {
        throw re;
    }
}

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/

clause        returns [ SearchExpression searchExpr ]
scope    {
    SearchField field;
    SearchOperator op;
    List<string> terms;
}
@init { $clause::terms = new List<string>(); }
    :    keywordClause EOF
         { SearchExpression expr = new SearchExpression($clause::field,
$clause::op, $clause::terms); $searchExpr = expr; }
    ;

keywordClause
    : WS keywordField WS E { $clause::op = 1; } k=keyTerm
{$clause::terms.Add($k.text); } (','k=keyTerm {$clause::terms.Add($k.text);
})*
    ;
keywordField    : SECURITY        { $clause::field = 1; }
                ;
keyTerm         : KEYWORD
                | NUMBER
                ;

/*------------------------------------------------------------------
 * LEXER/TOKEN RULES
 *------------------------------------------------------------------*/
SECURITY    : ('S'|'s')('E'|'e')('C'|'c');

E            :    '=' { myGotEqualsOp = true; };
PLUS        :    '+';


NUMBER        : DIGIT+;
KEYWORD        :
('A'..'Z'|'a'..'z'|DIGIT|'$'|'£'|'&'|'^'|'%'|'('|')'|'['|']'|':'|';'|'*'|'<'|'>'|'/'|'\\'|'@'|'#'|'?'|'!'|'.'|'
')+;
fragment DIGIT    : '0'..'9';


WS            :    (' ' | '\t')+ { if (!myGotEqualsOp) $channel = HIDDEN; }
            ;

/////////////////////////////////////////////////////////////////////////////////////////
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090514/a1265023/attachment.html 


More information about the antlr-interest mailing list