Is it possible to add TOKEN automatically if conditional Token is absent

30 views
Skip to first unread message

Edgar Buchvalov

unread,
May 10, 2016, 7:30:57 AM5/10/16
to antlr-discussion

lessExpression: attribute lt dr=value? {if($ctx.dr==null) "somehow add predefined value to expression" };

If it is possible that when I enter:
"attr < 5" in java tree visitor I will get 5 when visiting valueContext, but
"attr < " will still visit valueContext, but with value ?.

is it possible to make such condition in rule file?

John B. Brodie

unread,
May 10, 2016, 11:19:11 AM5/10/16
to antlr-di...@googlegroups.com

Greetings!

Recall that the `?` meta operator is antlr sugar for the empty alternative. that is x? is the same as (x|) which i like to write as (x|/*empty*/) making the empty choice more explicit to a human reader.


And so you should use the expanded form and insert whatever action you need in the empty alternative. Something like:

lessExpression: attribute lt (dr=value | /*empty*/ { dr=predefinedDefaultValue } );


Hope this helps...

   -jbb

--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jim Idle

unread,
May 10, 2016, 9:07:49 PM5/10/16
to antlr-di...@googlegroups.com
In your listener or visitor, just check if ctx.dr == null. If it does, then you know to add your predefined value in to the mix. I would try to keep all actions out of the parser grammar itself.

Jim

--
Reply all
Reply to author
Forward
0 new messages