how to handle "rule expression: must label all alternatives or none"

113 views
Skip to first unread message

edgar hoover

unread,
Aug 16, 2020, 8:19:26 AM8/16/20
to antlr-discussion

Hi all,
I've got a long rule, he is a cutdown version:


expression :
        pmqident
    |   SP expression EP    # bracketed_expr
    ;

Where

pmqident :
        sident ( ( dot | dotdot )  sident ) *
    ;

(I'm capturing the fact that a bracketed expression is used because I need to textually output brackets if they were input; this is more source-to-source rather than a standard compiler)

This gives me the aforementioned error that I must label all alternatives or none, but I don't see the need, and doing so complicates things.  If I do as it asks:

expression :
        pmqident          # pmqident
    |   SP expression EP    # bracketed_expr
    ;

it rejects it saying
"rule alt label pmqident conflicts with rule pmqident", which sort of makes sense, but in which case why do I even have to label it differently, like this:

expression :
        pmqident          # pmqident1
    |   SP expression EP    # bracketed_expr
    ;

Which will just increase the rules I have to deal with (now I have to deal with ExitPmqident1 as an additional rule for the listener).

I could macro-in the definition of pmqident, but that rule is used in multiple places so it seems silly thing to do.

The alternative is to pull out bracketed_expr into its own rule, like this:

expression :
        pmqident
    |   bracketed_expr
    ;

bracketed_expr :
        SP expression EP
    ;

Which is probably how I'm going to do it, but I feel like I'm doing something wrong, so what's the best way to handle this? 

thanks

jan
Reply all
Reply to author
Forward
0 new messages