Re: two solutions to Steven's challenge

I’m repeating Michael’s summary mostly to make it likely that my
solution appears “below the fold” in case you’re still thinking about it
yourself.

It took me a little bit of trial and error, but I arrived at a solution.
Curiously, at no point did I construct a solution that appeared to give
ambiguous results. Steven, can you share some examples of grammars that
succeed but are ambiguous?

> The challenge as I interpreted it:  Write a grammar such that
>
> (1) The following are all accepted as comments:
>
>     <test-input>(**)</test-input>
>     <test-input>(***)</test-input>
>     <test-input>(****)</test-input>
>     <test-input>(*****)</test-input>
>     <test-input>(*abc*)</test-input>
>     <test-input>(**abc*)</test-input>
>     <test-input>(*abc**)</test-input>
>     <test-input>(*abc*abc*)</test-input>
>     <test-input>(**abc*abc*)</test-input>
>     <test-input>(*abc**abc*)</test-input>
>     <test-input>(*abc*abc**)</test-input>
>     <test-input>(*abc* )(*abc*)</test-input>
>     <test-input>(***a*)</test-input>
>     <test-input>(**a*)</test-input>
>     <test-input>(**a*a*)</test-input>
>     <test-input>(**aa*)</test-input>
>     <test-input>(*a*)</test-input>
>
> (2) The following are all rejected as comments:
>
>     <test-input>(*abc*)(*abc*)</test-input>
>     <test-input>(</test-input>
>     <test-input>(*</test-input>
>     <test-input>(**</test-input>
>     <test-input>(***</test-input>
>     <test-input>(**a</test-input>
>     <test-input>(**a*</test-input>
>     <test-input>(**aa</test-input>
>     <test-input>(*a</test-input>
>     <test-input>(*a*</test-input>
>     <test-input>(*aa</test-input>
>     <test-input>a</test-input>
>     <test-input>)</test-input>
>     <test-input>*)</test-input>
>     <test-input>**)</test-input>
>
> The first item on this list is Steven's last example.  I made it a
> negative case because it is not one comment, and I did not want to
> bother trying to write a grammar for a series of comments interspersed
> with non-comment data.

If you wish to solve this challenge independently, stop reading now;
spoiler alert.









> My first solution is fairly straightforward:
>
>     comment = '(*', comment-element, '*)'.
>     -comment-element = (nonstar | ssbnsc)*, star*.
>     -nonstar = ~['*'].
>     -star = '*'.
>     { star-something, but not star-closeparen }
>     -ssbnsc = '*'+, ~['*)'].

I came up with:

comment: -'(*', body, -'*)' .
-body: ~[")"]* ; ~['*'], [")"] .

Which I think works.

                                        Be seeing you,
                                          norm

--
Norm Tovey-Walsh
Saxonica

Received on Saturday, 5 February 2022 16:57:31 UTC