{n,m} - n to m occurences in sableCC

47 views
Skip to first unread message

psaravan...@isa.ae

unread,
Jul 24, 2015, 4:01:32 AM7/24/15
to SableCC

Hi all,

How can I mentioned n to m occurrence for particular token in the production.

SableCC supports some EBNF capabilities through the use of the *, +, and ? operators (note: the standard EBNF notations [ ] and { } are not available).

so I mentioned as

Productions

statement = token token? token? token? .. ;

but it throws conflict. is it anyway to mention like

statement = token{1,10} ;



Parane

unread,
Jul 24, 2015, 6:40:06 AM7/24/15
to SableCC, psaravan...@isa.ae
other than this way,
onetoken = token;
twotoken = [token1]:token [t2]:token;
.
.
onetoNtoken = onetoken | twotoken .....

Enrique Pierrot

unread,
Jul 22, 2017, 8:32:19 AM7/22/17
to SableCC
hi all,
I also try to specify the number of occurrences in sandcc.
example:
Token = ['a' .. 'z'] {1,30};

Etienne Gagnon

unread,
Jul 22, 2017, 8:45:42 AM7/22/17
to sab...@googlegroups.com
Hi Enrique,

It's pretty easy to do, but it's generally a bad idea. This should work:

Helpers

  letter = ['a'..'z'];

  five_optional_letters =
    letter? letter? letter? letter? letter?;
 
Tokens

  name =
    letter
    letter? letter? letter? letter?
    five_optional_letters
    five_optional_letters five_optional_letters
    five_optional_letters five_optional_letters;
Why do you need specifically to restrict the maximum number of letters to 30? Think about it. If someone provides a 31-letter name, the lexer will match two names, a 30-letter name and a 1-letter name, because of the longest-match rule. It would be much preferable to match a single 31-letter name and issue a semantic error about exceeding 30 letters.

Have fun!

Etienne
Etienne Gagnon, Ph.D.
http://sablecc.org
Reply all
Reply to author
Forward
0 new messages