Marpa:...::Order(3pm)

πŸ“¦
version 12.000000 Β· 2 versions
, in ubuntu stonking

πŸ’– NAME πŸ’–

okay so bestie, gather round: this is Marpa::R2::Deprecated::NAIF::Semantics::Order β€” and it's all about how the NAIF ranks ambiguous parses. πŸ¦‹ it's a page from the User Contributed Perl Documentation, and that's the whole vibe. iykyk.


🚨 THE NAIF INTERFACE IS DEPRECATED 🚨

fr fr, this entire document is about the NAIF interface, and the NAIF interface is deprecated. PLEASE DO NOT USE IT FOR NEW DEVELOPMENT. like, not even a little bit. 🫢


🌸 Description 🌸

so, this doc deals with Marpa's low-level NAIF interface. if you're new to Marpa, or you're not sure which interface you're interested in, or you don't even know what the Named Argment InterFace (NAIF) is, then bestie, you probably want to go look at the document on semantics for the SLIF interface instead. no shade, it's just the right door to knock on. πŸšͺ

Marpa allows ambiguous parses. here's the tea: while an unambiguous parse can produce at most one parse tree and one parse result, an ambiguous parse will produce a parse series. a parse series is a sequence of parse trees, and each one of those trees has its own parse result. πŸ’…

this document describes ways of controlling the order in which the NAIF recognizer's "value" method evaluates the parse trees of an ambiguous parse; it also describes ways to exclude selected parse trees from the parse series. two-for-one, obsessed. πŸ”₯

also, almost all of what's said in this document also applies to the SLIF recognizer's "value" method. and the difference between the two? certain named arguments which control the parse order are present in the NAIF, but are not present in the SLIF, and that accounts for the differences between the two. periodt. 🌈

πŸ“ Duplicate parses are eliminated πŸ“

when evaluating the parse trees in a parse series, Marpa never evaluates the same parse tree twice. and what this means probably matches the programmer intuition of what it should mean. Marpa considers two parse trees to be the same if they are semantic equivalents.

two parse trees are semantic equivalents if and only if a recursive, top-down evaluation of each applies the same rules in the same order at the same earleme locations. this definition implies that, given any deterministic semantics, two parse trees which are semantic equivalents will always produce the same parse result β€” hence the term. πŸ’€ and when the Marpa documentation refers to duplicate parses, it means the two are semantic equivalents, unless otherwise stated.

🧚 Default parse order 🧚

in this document, the term arbitrary parse order means an arbitrary choice among the strict total orders of the equivalence classes that contain the semantically equivalent parse trees. this set of equivalence classes is finite. (small, but powerful.)

traversal of the parse trees in arbitrary parse order will always be well-behaved in the sense that no two parse trees will be semantic duplicates, and no unique (semantic non-duplicate) parse tree will be omitted in it. no other property of arbitrary parse order is guaranteed. for example, the order may change each time the parse series is traversed. πŸ™ˆ

by calling the recognizer's "value" method repeatedly, Marpa can produce all the parse results in the current parse series. the default is for the parse results to be returned in an arbitrary parse order. this corresponds to the "none" value of the recognizer's ranking_method named argument.

πŸ‘‘ Ranking methods πŸ‘‘

Marpa recognizer objects have a ranking_method named argument, whose value can be the name of a ranking method, or "none" β€” which means the default ranking method is the one to be used. slay.

πŸ”₯ The "rule" ranking method πŸ”₯

the rule method ranks alternative parses according to their rules. every rule has a rule rank. a rule's rank can be specified using the rank named argument for that rule. rule ranks must be integers. and if no rule rank is specified, the rule rank is 0.

⭐ The "high_rule_only" ranking method ⭐

the "high_rule_only" ranking method is like the "rule" ranking method, except that at every choice point, it discards all of the choices which have a rank lower than that of the highest ranked alternative. no low-rank crumbs, we're throwing those away. πŸ—‘οΈ

and because "high_rule_only" eliminates some parse trees, it can reduce or eliminate the ambiguity of a parse β€” but it does not necessarily do either. why? because at each choice point among the parse trees, it's possible that several of the choices, or all of them, will have the same rank as the highest ranked alternative. so like… no promises, bestie. πŸ’€

πŸŽ€ Rule ranking πŸŽ€

a parse series is kept in a structure called a parse bocage. the parse bocage is a tree-like structure whose root node is the common root of all the parse trees of the parse series. in an unambiguous parse, there will be only one parse tree, and the parse bocage will be equivalent to that parse tree. in an ambiguous parse, there will be choice points in the parse bocage. at those choice points there will be two or more alternatives β€” choices which result in different parse trees.

when ranking, the logic traverses the parse bocage, looking for choice points. from the point of view of the individual parse trees, this traversal will be top-down and left-to-right. and at the choice points, the alternatives are ranked (in the "rule" ranking method) or selected (in the "high_rule_only" ranking method), by comparing them like this:

  • Different ranks: if the two alternatives have different rule ranks, they must also have different rules. the alternative with the higher rule rank will rank high.
  • Same Rule: if the two alternatives have the same rule, they rank as described under "Null variant ranking".
  • Same rank, different rules: two different rules can have the same rank. if the two alternatives are for different rules, but the two rules have the same rank, the relative order of the two alternatives is arbitrary.

🌸 Null variant ranking 🌸

some rules have a RHS which contains proper nullables: symbols which may be nulled, but which are not nulling symbols. (nulling symbols are symbols which are always nulled.)

when a rule contains proper nullables, each application of that rule to a section of input creates a nulling variant β€” that rule with a specific pattern of null and non-null symbols. in many cases this creates an ambiguity, because different nulling variants could apply to the same substring of input. and in ambiguous parsings of this kind, some applications may want to rank nulling variants that start with non-null symbols higher. other applications may want to do the exact opposite β€” to rank nulling variants that start with null symbols higher. both vibes exist, and both are valid. πŸ¦‹

the null_ranking named argument for rules specifies which nulling variants are ranked high or low. ranking of nulling variants is done left-to-right, with the null preference as indicated by the null_ranking named argument. specifically:

  • if the null_ranking is "low", then the closer a nulling variant places its visible (non-null) symbols to the start of the rule, the higher it ranks. "low" null ranking is the default.
  • if the null_ranking is "high", then the closer a nulling variant places its null symbols to the start of the rule, the higher it ranks.

πŸ¦‹ A general approach to sorting parses πŸ¦‹

the most general way to sort Marpa parses is for the application to take control. main character energy, honestly. the application can set up the Marpa semantic actions so that the parse result of every parse tree is a <rank, true_value> duple. the duples can then be sorted by rank. and once the results are sorted, the rank element of the duple can be discarded. (those familiar with the Schwartzian transform may note a resemblance. in Perl, duples can be implemented as references to arrays of 2 elements.)

⚠️ the user needs to be careful. in theory, ambiguity can cause an exponential explosion in the number of results. in practice, ambiguity tends to get out of hand very easily. producing and sorting all the parses can take a very long time. 😭

okay legal bestie moment, here's the fine print, verbatim because that's how licenses work πŸ’…

Copyright 2022 Jeffrey Kegler
This file is part of Marpa::R2.  Marpa::R2 is free software: you can
redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.

Marpa::R2 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser
General Public License along with Marpa::R2.  If not, see
http://www.gnu.org/licenses/.

and that's the whole page, bestie 🫢 it came from the User Contributed Perl Documentation, the version stamp at the bottom says perl v5.42.3, and the date on it is 2026-08-20. go forth and rank those parses (or don't, because again… deprecated πŸ’–). stay iconic βœ¨πŸŽ€

πŸ“¦ where this page comes from