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

`VL vl.homutov at gmail.com
Mon Oct 13 13:20:50 PDT 2008


Hello ALL.

Thirst, i'd like to say that ANLTR is a great tool. I recently started
to use and like it very much. Thanks,authors!

But i steel need to understand a lot. Below is what i'm trying to do and what i get.

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.
But i want to write a tool that will work with such files as is and do not evaluate such vars.
Currently i just send this vars to hidden channel, like comments and have rules to match
'normal' syntax. 

For example, i have thing like this to parse:

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* )+ ;

As i suggest, that would mean 'enpath is zero or more ENVAR, later zero or more PATH, later zero or more ENVAR, repeating.
When i try to compile this, i get 

Decision can match input such as "'$''('{'$', '.', 'A'..'Z', '_', 'a'..'z'}')'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input

Well, i read a chapter in ANTLR book about ambiguities and for now i understand this (in current context) as:
if path in center is empty, ANTLR don't know which ENVAR it should use: in the begin or in the end one?

Please, tell me if i'm wrong. What is worse - i don't know what to do in this situation ;-). How can i accomplish
my task - in the example above i'd like to get value of DOCS variable as literal string /home/$(USER)/docs AND
match only valid paths  (/home//docs/ is a valid path if environment variable is thrown out). 

Thank you, ALL!





More information about the antlr-interest mailing list