Multiplicity constraints and (possibly) something like lambda functions?

54 views
Skip to first unread message

Athanasios Anastasiou

unread,
Mar 25, 2014, 7:04:16 AM3/25/14
to antlr-di...@googlegroups.com
Hello everyone

Is it possible to define multiplicity constraints on tokens through ANTLR's metalanguage? An example specification would be "Match 2 to 4 decimal digits" or something like [0-9]{1,4}.

I have looked through the documentation but all i have discovered (so far) is ways of doing it that look like this:
someList locals[int i=4;]: NUM{$i--;} (SYM_COMA{$i>0}? NUM{$i--;})*;

Which, i do get, but doing this every time i have to define a list does not seem practical and not very readable too.

Which brings us to lambdas, would it be possible to define a lambda or other pattern (like C++ templates for example) that i can define once and call many and it would get expanded in the code during compilation time? For example:

DEF someList<maxLen, tokClass> locals[int i=<maxLen>;]: <tokClass>{$<maxLen>--;} (SYM_COMA{$<maxLen>>0}? <tokClass>{$<maxLen>--;})*;
SYM_COMA:',';

list1: someList<4,NUM>;
list2: someList<12,NUM>; //(and so on)


All the best
AA

Sam Harwell

unread,
Mar 25, 2014, 7:42:21 AM3/25/14
to antlr-di...@googlegroups.com

Hi AA,

 

[0-9]{1,4} is just syntactic sugar for the following:

 

[0-9] ([0-9] ([0-9] ([0-9])?)?)?

 

ANTLR doesn’t support the earlier syntax, but either of these forms is rarely needed and generally not recommended. Whenever possible, it’s preferable to place your multiplicity validation in a separate semantic analysis phase that is performed after the parsing is complete. For ANTLR 4, this would be written as a listener or visitor.

 

Thanks,

Sam

--
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.

Athanasios Anastasiou

unread,
Mar 25, 2014, 8:19:22 AM3/25/14
to antlr-di...@googlegroups.com
Hello Sam

Thank you for the quick response.

I guess i am going to have to work as suggested if i want to use ANTLR but i am left with mixed feelings :)

Being able to specify a multiplicity constraint on a rule is still about defining its structure, not its meaning, but maybe i am talking from a limited experience point of view.

All the best
AA


--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/mAIRHaU8B0o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to antlr-discussi...@googlegroups.com.

Sam Harwell

unread,
Mar 25, 2014, 8:43:24 AM3/25/14
to antlr-di...@googlegroups.com

Hi AA,

 

When parsing, you generally want to move as many constraints from your grammar to a semantic analyzer as you can, as long as it doesn’t result in your parser producing unusable trees. This strategy results in much more meaningful error messages being reported to users. In some cases, such as Unicode escape sequences within strings, you may actually need to use multiplicity constraints. For example, the following grammar fragment uses modes to pass escape sequences within strings to the parser as individual tokens. The multiplicity constraint in the UnicodeEscape rule is required to ensure that \u00523 is passed as two tokens: UnicodeEscape “\u0052” and Text “3”.

 

DoubleQuote : '"' -> pushMode(StringMode);

 

mode StringMode;

 

StringMode_DoubleQuote : DoubleQuote -> type(DoubleQuote), popMode;

Text : ~[\\"]+;

UnicodeEscape : '\\u' HexDigit HexDigit HexDigit HexDigit;

InvalidUnicodeEscape : '\\u' (HexDigit (HexDigit HexDigit?)?)?;

Escape : '\\' [rnt];

InvalidEscape : '\\' (. | EOF);

Athanasios Anastasiou

unread,
Mar 25, 2014, 1:06:27 PM3/25/14
to antlr-di...@googlegroups.com
Hello Sam

Many thanks for your response. I appreciate the comment.

All the best
AA




Jim Idle

unread,
Mar 25, 2014, 10:01:26 PM3/25/14
to antlr-di...@googlegroups.com
Oy! That's my email! ;)

Jim
Reply all
Reply to author
Forward
0 new messages