parse() — first impression and suggestions

27 views
Skip to first unread message

Ruslan Shvedov

unread,
Sep 12, 2014, 12:51:45 AM9/12/14
to marpa-...@googlegroups.com
I've been using parse when working on commonmark parser and here is the how it turned out. 

First, parse() does a great job when testing with unambiguous grammar and input — it throws an exception on ambiguity and that's what you want.

However, when thing get more complex and I needed, e.g. just dump multiple parses, the recognizer is hidden behind the otherwise very useful parse() interface, and to do that _while still keeping the behaviour in unambiguous case_, I had to mimic parse()'s functionality, like this

I understand that it is by design, hence my suggestion is that in list context parse(), rather than throwing an exception, return its recognizer (probably after series_restart() so that it's ready to use), like this. 

my ($value, $recognizer) = $slg->parse( { input => \$input } );

Another, arguably better, option would be to return the abstract syntax forest parse() produces, e.g.

my ($value, $asf) = $slg->parse( { input => \$input } );

This would require adding recognizer() method to Marpa::R2::ASF (grammar() is already there) .

The full use case I have in mind:

my ($value, $recognizer_or_asf) = $slg->parse( { input => \$input } );

# check for ambiguity
if (not defined $value){
# use the recognizer or the asf to handle ambiguity
# and find the required value 
# e.g. sort the parses or prune the ASF or the ASTs
# ...
}

# process $value if needed
# ...

The case where exception is thrown on ambiguity continues as is:

my $value = $slg->parse( { input => \$input } );

Jeffrey Kegler

unread,
Sep 13, 2014, 11:12:36 AM9/13/14
to marpa-...@googlegroups.com
Marpa::R2 is nearly frozen and the new $slg->parse() is squeezed in past the freeze as an exception, because it will be very good for those just starting -- it simplifies the initial tutorial and example a good deal.  These suggestions may have more merit as a design, but they get *way* beyond that intent.  My intent is that those who want to do anything fancy simply ditch $slg->parse() and use the lower level methods, as currently, and with that idea I kept the features to a minimum, and avoided access to the fancier features, like events or ASF's.

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

Reply all
Reply to author
Forward
0 new messages