Confusing error message and lack of understanding of lists

17 views
Skip to first unread message

Ivan Perez

unread,
Aug 12, 2020, 2:52:07 PM8/12/20
to BNFC Developers and Users
Hi,

I'm trying to write a production for a list.

I wrote:

[] . [LetSeq]  ::= ;
(:) . [LetSeq] ::= LetSeqItem [LetSeq] ;

Where LetSeqItem is non-empty. I get the following error message:

Bad list construction rule (:). [LetSeq] ::= LetSeqItem [LetSeq]

If I write instead:

LetSeq0 . LetSeq0 ::= ;
LetSeq1 . LetSeq0 ::= LetSeq0 LetSeqItem ;

Everything works. What am I exactly doing wrong?

Thanks,

Ivan

Aarne Ranta

unread,
Aug 13, 2020, 2:02:34 AM8/13/20
to bnfc...@googlegroups.com
Hello Ivan,

The (:) rule for lists should have the form

(:).  [C] ::= C [C]

That is, the same category for the item type as inside the list brackets. In your case, you could write

[] . [LetSeqItem]  ::= ;
(:) . [LetSeqItem] ::= LetSeqItem [LetSeqItem] ;

or more concisely (also eliminating this kind of errors)

terminator LetSeqItem "" ;

An explicit pair of [] and (:) rules is needed only if you add some more tokens than simply separators and terminators.

Regards

  Aarne.




--
You received this message because you are subscribed to the Google Groups "BNFC Developers and Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bnfc-dev+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bnfc-dev/5699483d-e237-4688-95f0-5f85106db990o%40googlegroups.com.

Ivan Perez

unread,
Aug 13, 2020, 9:30:06 AM8/13/20
to BNFC Developers and Users
Thanks for the info. That makes sense.

May I suggest adding this specifically to the documentation (where I haven't found this, at least, not as clearly stated) and to the error message in BNFC?

Cheers

Ivan
To unsubscribe from this group and stop receiving emails from it, send an email to bnfc...@googlegroups.com.

Ivan Perez

unread,
Aug 13, 2020, 9:53:51 AM8/13/20
to BNFC Developers and Users
And I related question:

Does the previous answer mean that there can only be one way to define lists of a specific symbol in a grammar if using the syntax [C] to represent them?

And what I mean by that is that, at the type level, you may want, for example, lists of C's separated by commas and lists of C's separated by semicolons to all be lists. But at grammar/production level, they are different symbols. For example, if you first define:

B. B ::= [C];

separator "," C;

Then you cannot define A to be lists of C's separated by semicolons using the same syntax, because [C] would have two different meanings in this grammar:

A. A -> [C];

-- I'd like to write now: separator ";" C;

I give them different names either, and then use [] and (:) to build lists in both cases. For example, the following would be illegal, right?

([]) . B ::= C;
(:) . B ::= B "," C;

([]) . A ::= C;
(:) . A ::= A ";" C;

Because I cannot use the constructors [] and (:) in those rules.

Am I wrong about any of this? What is the approach to follow in this case?

Thanks,

Ivan

Ivan Perez

unread,
Aug 13, 2020, 10:56:37 AM8/13/20
to BNFC Developers and Users
I think there was an error in my last message. The recursive constructions would also be illegal, I guess, because the productions are left-recursive but the constructor (:) for lists is right-recursive. But the main question stands.

Sorry about the extra noise.

Ivan

Aarne Ranta

unread,
Aug 13, 2020, 11:44:53 AM8/13/20
to bnfc...@googlegroups.com
Everything follows from the typing of (:) - in Haskell notation,

  (:) :: a -> [a] -> [a]

Hence it must use uniformly the same category.

A workaround for having different lists for the same category C is to coerce it into another category

  CtoD.  D ::= C

and then define [D] in a way different from [C]

  Aarne.








To unsubscribe from this group and stop receiving emails from it, send an email to bnfc-dev+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bnfc-dev/8a92b72a-2cad-4515-a249-36bbd2a2c1c1o%40googlegroups.com.

Andreas Abel

unread,
Oct 11, 2020, 5:34:35 AM10/11/20
to bnfc...@googlegroups.com
Hi Ivan,

a late comment: the documentation states this at


https://bnfc.readthedocs.io/en/v2.8.3/lbnf.html#the-type-correctness-of-lbnf-rules

However, the error message could be improved. I filed an issue:

https://github.com/BNFC/bnfc/issues/305

Cheers,
Andreas
Reply all
Reply to author
Forward
0 new messages