[antlr-interest] Selection between lexer rule and literal

Bryan Ewbank ewbank at gmail.com
Tue Apr 19 08:27:16 PDT 2005


If you are attempting to match context-sensitive keywords (that is,
"components" is COMPONENTS sometimes, VALUE at other times), then you
need to use semantic predicates.

The lexer always reports "components" as VALUE, and the parser uses a
semantic predicate to conditionally match a VALUE for which something
is true.  The guts look like this:

   // predicate must be in an alternative to avoid parse errors.  Note that
   // ANTLR does not hoist predicates, therefore it cannot be in "component".
   rule1:
      ( { isComponent(LT(1)) }? component
      | value
      )
      ;

   component:
      v:VALUE { #v->setType(COMPONENT); }
      ;

You need to tie it into exactly what you need, but the semantic
predicate is, I think, the key.
On 4/19/05, togol machillan <togolmach2 at lycos.com> wrote:
> I would like the parser to match the occurence of COMPONENTS, for
> example, in the input file with the VALUE rule if it does not find
> any reference to the p_keyword_list rule in the rule currently being
> matched. The parser never matches COMPONENTS with the VALUE rule,
> although it is a valid token of the VALUE type.


More information about the antlr-interest mailing list