[antlr-interest] Embedded expressions

Jayesh Patel Jayesh.Patel at sandstone-tech.com
Sat Aug 1 09:38:52 PDT 2009


Hello everybody

 I'm very new to ANTLR and so far I have been able to achieve what I want.  Before I go a head with the problem, a little background to what I'm trying to do.  I have a number of text based document files, into which I have inserted expressions into the text to insert dates, names and figures.  I'm using ANTLR to tranform the text  file into a populated text file.  An typical example of the expressions would be:
 
[. insert.date.today/]
 
The lexer and the parser correctly finds and evaluates any expression that is on its own.  My problem is this, I can have expressions embedded within text.  I want to retain the text and to evaluate the expression.  An example of what I'm using is:
 
'Your next appointment date is [. insert.date.nextAppointmentDate/] and you will be seen
by [. insert avaliable.consultant.fullName/].  Please, bring along your [. insert careerDocumentList/]
to help with you career choice.'
 
However, what is happening is that expressions embedded in the text are being ignored and all you get is:
 
'Your next appointment date is   and you will be seen
by  .  Please, bring along your  
to help with you career choice.'
 
I have successfully got everything working the way I want it, and the Lexer and tree diagram clearly indentifies the embedded expressions.  To cater for the text I have used the following rule:
 
everything_else
  :(IDENTIFIER | SYMBOLS) (options {greedy=false;} :.*) ;
 
I believe that the problem is caused by the (options {greedy=false;} :.*).  The wildcard '.*' is accepting all of the text, as text, and ignoring the embedded expressions.
 
The lexer rules I have used are:
 
EXPRESSION_ALPHA: ('a'..'z' | 'A'..'Z' | '.');
 
NUMBER: ('0'..'9');
 
LEFT_PAREN: '[.';
 
RIGHT_PAREN:  '\\]'| ']';
 
WS:  (' ' | '\t' | '\u000C' | '\r' | '\n' )* {$channel = HIDDEN;};
 
IDENTIFIER: (EXPRESSION_ALPHA|NUMBER)+;
 
Many thanks
 
Jay


More information about the antlr-interest mailing list