[antlr-interest] newbie: are selective lexer rules (depending on where in grammar parser is) possible?

Vladimir Konrad vladimir at ok2home.net
Wed Mar 25 05:39:51 PDT 2009


Hello!

(complete newbie here)

I have a text file with record based data format in the form:

/begin
time-stamp: 2001-20-20-10-20-15
/begin-uk
<anything-possible-here> = <abything-possible-here>
/end-uk
/begin-com
<anything-possible-here> = <abything-possible-here>
/end-com
/end

(there is '\n' after each line)

The problem I have is that when creating a lexer rule to match:

<anything-possible> = <anything-possible>

this rule matches "other stuff", in the file. Is it possible to switch
on/off the lexer rules depending on where in the grammar the parser
momentarily is? Or do I have to use predicates?

Kind regards,

Vlad

PS: The grammar so far (i know it is still quite broken):

content :       block* EOF ;

block   :       BEGIN TIME_STAMP? record* END ;


/* the following rule is obviously ugly as it passes all content to
parser not lexed */

record  :       BEGIN_RECORD (options {greedy=false;} : . )*
END_RECORD ;

BEGIN_RECORD
        :       BEGIN SUFFIX '\r'? '\n' ;

END_RECORD
        :       (END_RECORD) => END SUFFIX '\r'? '\n' ;

SUFFIX  :       SEPARATOR LETTER+ ;

BEGIN   :       '/begin' ;

END     :       '/end' ;

SEPARATOR
        :       '-' ;

TIME_STAMP
        :       'time-stamp:' ' '+ DIGIT+ (SEPARATOR DIGIT+)+ ;

LETTER  :       'a'..'z' | 'A'..'Z' ;

DIGIT   :       '0'..'9' ;



More information about the antlr-interest mailing list