[antlr-interest] Understanding ANTLR's 'multiple alternatives'

Gavin Lambert antlr at mirality.co.nz
Tue Oct 14 00:20:02 PDT 2008


At 09:20 14/10/2008, `VL wrote:
 >I need to parse a file with quite simple syntax, but it has one
 >caveat: file can include environment variables in a form of 
$(VAR)
 >at any place inside. This is not a problem for software that 
uses
 >it since it just evaluates values and parses results.
[...]
 >DIR=/some/path
 >DOCS=/home/$(USER)/docs
 >
 >Say, i have such simple rule for absolute path:
 >
 >fragment PATH:   ( '/' (ID|INT)* )+;
 >
 >I thought that i can match path with $(VAR) embedded somewhere
 >inside it with something like this:
 >
 >fragment ENVAR:  '$' '(' ID ')';
 >ENPATH:   ( ENVAR* PATH* ENVAR* )+ ;

The problem with this approach is it doesn't consider the case 
where the environment variable contains a /, or where it forms 
only part of the path (eg. "/home/user-$(USER)/docs".

A better combination that supports these as well would be:

fragment ENVAR : '$' '(' ID ')';

PATH : ( '/' (ID|INT|ENVAR)* )+;


On the other hand, the file structure given above is simple enough 
that using ANTLR seems like overkill.  Why not just read each line 
and split it at the first '='?



More information about the antlr-interest mailing list