Discussion:
How to capture this?
lin q
2012-12-08 01:18:33 UTC
Permalink
Hi,

I need to capture such into grammar, there are 3 values, A, B and C, the
expected combinations are one of each, two of each and all three. Initially
I was thinking to use this grammar: A? B? C?, but this has a problem
because it allows that none of the three present. Different approach is to
list all the combinations in some way, but that does not scale well.

Any suggestion?

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
Jim Idle
2012-12-08 09:17:11 UTC
Permalink
Your first guess is about best, but add some action code to check the
combinations (or do so in your tree walk/visitor). Then you can issue
good error messages: "must specify at least one of A B C". "Cannot
repeat A" and so on. In general, make you parser accept any possible
combinations and then check semantics. Your end product will be much
better.

So:

x : (A | B | C)* ;

the count occurrences and act accordingly. Usually order of A B C
combinations is not relevant, though it can be artificially so because
of previous implementations of parsers fir instance.

Jim
Post by lin q
Hi,
I need to capture such into grammar, there are 3 values, A, B and C, the
expected combinations are one of each, two of each and all three. Initially
I was thinking to use this grammar: A? B? C?, but this has a problem
because it allows that none of the three present. Different approach is to
list all the combinations in some way, but that does not scale well.
Any suggestion?
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
Loading...