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; }
            ;

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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-interest@googlegroups.com
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to