[antlr-interest] Lexer for a grammar similar to a template engine

Joan Pujol joanpujol at gmail.com
Sun Oct 11 03:07:43 PDT 2009


Hi ,

I'm trying to do a grammar to parse expressions in a document in a
template engine basis.
The expressions are enclosed in ${}  (for example ${name}) and can be
in an enclosed in arbitrary document content.

Ideally what I need from the parser for input:   Hello ${name}.  Would
be similar to:

OTHER ^EXPR(ID)

I've tried that grammar but it doesn't works. If I enter an input that
contains $ or { in enters in an infinite bucle:

Grammar:
----
OTHER:	(
	    {input.LA(1)!='$' && input.LA(2)!='{'}? . 	
	)+;
	
ID  :	('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;
	
line	: (OTHER
	  | expr )*;

expr	: '${' ID '}';
---

I'm sure I'm missing some basic concept but have passed more than
three or four years from my last antlr parser and I've to refresh my
parsing abilities.
Some one can orient me of how to solve that? or what I'm doing wrong?

Can I do that in one parser or is better to use one parser to separate
arbitrary content of expressions and then use another parser for the
expressions?

A lot of thanks in advance,


-- 
Joan Jesús Pujol Espinar
http://www.joanpujol.cat
http://lujop.deviantart.com


More information about the antlr-interest mailing list