[antlr-interest] IDs and keywords

Nuno Pedrosa nunomdp at gmail.com
Thu Oct 20 02:31:37 PDT 2011


Hello everyone,

I have just started a project to convert our current way of processing the
file generated by our program into a more elegant way by using a parser
generator.

ANTLR so far as proven to be quite powerful but I think I have hit a bit of
a wall.

here is an extract of my grammar:
>>>>>>>>>>>>>>>>>>>>>>>
grammar MFL;

options{
 language = C;
}

model:
'MODEL'  ID  'ASSOC' cpa_vars id1=ID? id2=ID? ';'
;

cpa_vars returns [long var]:
    'CPA'     {$var = JVCPA;}
    | 'PSAT'  {$var = JVSVP;}
;

ID
    :('a'..'z'|'A'..'Z'|'0'..'9'|'_')
('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'-'|','|'+'|'.')*
;
>>>>>>>>>>>>>>>>>>>>>>>>

I think this should be enough to explain my problem.

the rule model should match things like, but there are situation where it
does not work as I expected:
example: this matches OK
MODEL mymodel ASSOC CPA BIP1 ;

but says that <missing ID> where cpa is.
MODEL cpa ASSOC CPA BIP1 ;

debugging the code I understand the lexer has assigned token type to the
literal present in the cpa_vars rule instead of the mode generic ID token
type.

My question is: how do I make sure I match ID instead of  'CPA' of another
rule for this case?

The configuration file I am trying to parse follows this structure that
depending in the place the tokens are, they are considered actual tokens or
else they are just general identifiers.

I sure will appreciate any help on this.

Best regards,

Nuno


More information about the antlr-interest mailing list