Discussion:
Can I use "|" for multi string element
lin q
2012-11-29 00:11:20 UTC
Permalink
Hi,

The data input may be of this format: "command arg" and the arg must be 1
or 2 of "arg1", "arg2" and "arg3", namely it could be any of the following:

command arg1
command arg2
command arg3
command arg1 arg2
command arg1 arg3
command arg2 arg3

I wonder if I could define the grammar like this:
cmd : 'command' ('arg1' | 'arg2' | 'arg3' | 'arg1' 'arg2' | 'arg1' 'arg3' |
'arg2' 'arg3')

Or do I have to put 2 arg into one string like this:
cmd : 'command' ('arg1' | 'arg2' | 'arg3' | 'arg1 arg2' | 'arg1 arg3' |
'arg2 arg3')

The latter is very restrictive to me.

Thanks!

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
Nick Vlassopoulos
2012-11-29 12:52:09 UTC
Permalink
Hi Lin,

Although I am not an antlr expert, I think that what you are looking for
are the "Validating Semantic Predicates". Stackoverflow has a really
nice post on this (
http://stackoverflow.com/questions/3056441/what-is-a-semantic-predicate-in-antlr3
)

Also, the antlr website has some nice examples (see for example
previous posts in the antlr-interest list).

HTH,

N.
Post by lin q
Hi,
The data input may be of this format: "command arg" and the arg must be 1
command arg1
command arg2
command arg3
command arg1 arg2
command arg1 arg3
command arg2 arg3
cmd : 'command' ('arg1' | 'arg2' | 'arg3' | 'arg1' 'arg2' | 'arg1' 'arg3' |
'arg2' 'arg3')
cmd : 'command' ('arg1' | 'arg2' | 'arg3' | 'arg1 arg2' | 'arg1 arg3' |
'arg2 arg3')
The latter is very restrictive to me.
Thanks!
List: http://www.antlr.org/mailman/listinfo/antlr-interest
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...