# Rule ordering question

26 views

### Michael Spertus

Aug 8, 2018, 1:20:39 AM8/8/18
to marpa parser
Thanks for helping me to get to a (surprising) answer to my previous question. I was hoping you could help me with another. I want the following grammar to parse 'xy' as a single statement

statements ::= statement+
statement ::= xy | x | y
x ::= 'x'
y ::= 'y'
xy ::= 'x' 'y'

Unfortunately, it always parses as two statements, even if I use the attached "high_rule_only" code and rank the statement alternatives as

statement ::= xy rank => 1 | x | y

I still get two statements. Is there a way I can do this with ordering, or do I need to do something like traverse the ASF? Note that I need to do this at the ::= level rather than with lexemes because in the actual grammar I care about x and y are complicated rules themselves.

Thanks,
Mike

### Michael Spertus

Aug 8, 2018, 1:33:23 AM8/8/18
to marpa parser
This time with attachment :/
rank.pl

### Lukas Atkinson

Aug 8, 2018, 3:01:24 AM8/8/18
Your grammar as written is ambiguous and therefore Marpa gives you all
parses in an unspecified order – to see them, iterate over the value
like

while (my \$ref = \$recce->value) {
print Dumper \$\$ref;
}

Marpa's ranks are a bit unintuitive, I previously ran into very
similar problems. This lead to the Marpa::R2::Semantics::Rank
document[1] being written (Thanks Jeffrey!). That document shows a
related example. The solution seems to be to spell out the sequence
rule explicitly:

statements ::= xy rank => 1
statements ::= x
statements ::= y
statements ::= statements xy rank => 1
statements ::= statements x
statements ::= statements y

The docs emphasize: “The rank of a parse choice is the rank of the
rule of its cause”, which suggests the problem is the intermediate
statement rule. If I understand correctly, the "statements ::=
statement+" sequence rule has no choices because it always gets a
statement at each position (not a choice between x and xy). And the
rank within statement does not matter because … I still don't
understand this 100%.

[1]: https://metacpan.org/pod/release/JKEGL/Marpa-R2-5.043_043/pod/Semantics/Rank.pod
> --
> 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.

### Michael Spertus

Aug 8, 2018, 2:31:32 PM8/8/18
to marpa parser
Thanks for the link, Amon. As I suspected, it says you should travers the ASF in such cases...

In a related question, is there any way for me to reject a rule during the read() by listening for a prediction/completion events? A number of my rules have conditions on their arguments that are known not to be BNF-friendly. I know I can bail from those in a semantic action, but I would like to reject before an exponential explosion in the number of ambiguous parses. Does that make sense?

Thanks,

Mike

### Jeffrey Kegler

Aug 8, 2018, 2:40:47 PM8/8/18
to Marpa Parser Mailing LIst
It make *lots* of sense and I've considered it as a feature in R3.  It will never happen in R2 -- it'd be a massive risky change and R2 is stable.

Incidentally, a limited implementation of it is how LATM lexing works.  As a special case, I selectively turn on and off predictions in Earley set 0 of the lexer.

To unsubscribe from this group and stop receiving emails from it, send an email to marpa-parser+unsubscribe@googlegroups.com.

### Michael Spertus

Aug 8, 2018, 5:50:16 PM8/8/18
to marpa parser
Thanks for the response Jeffrey. That would be awesome. Consider this a vote for early bailing (Earley bailing?) in R3.

Mike

### Michael Spertus

Aug 8, 2018, 6:21:12 PM8/8/18
to marpa parser
Forgot to ask: Is there a way for me to access a rule's rank from within its semantic action? That way I could simulate the tree ordering from indirect ranks during ASF pruning

Thanks,
Mike