5

I need to match in ANTLR a message containing 2 fields separated by a / First field can have 1..3 digits, the second field can have 1..2 digits,

this does not work

msg: f1 '/' f2;

f1: DIGIT(DIGIT(DIGIT)?)? ;
f2: DIGIT(DIGIT)?
  1. How can I avoid ambiguity in such a case?
  2. Is there a more elegant way to express the number of repetitions with ANTLR?

Thanks a lot Chris

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
chris
  • 398
  • 2
  • 11

1 Answers1

4

AFAIR (it's some time ago I last used ANTLR), you can use "fragment" to avoid having f1 and f2 as a top level token:

msg: f1 '/' f2;

fragment f1: DIGIT(DIGIT(DIGIT)?)? ;
fragment f2: DIGIT(DIGIT)?

This allows you to have an ambiguity between f1 and f2, because they don't have to match on their own.

Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
  • I still get the same error :-( [MismatchedTokenException] Thanks! – chris Mar 11 '10 at 21:16
  • Sorry, my fault: I solved it; if DIGIT was a fragment too, it was not working; if DIGIT is NOT a fragment, it works!! Thanks – chris Mar 11 '10 at 21:20