Skip to content

Checking for the left-recursion #496

Answered by pdubroy
evilguest asked this question in Help
Discussion options

You must be logged in to vote

Hi @evilguest

I'm not sure what you mean by "most of the PExpr instances seem to be just PExprs, with no hints on their actual types". If you are using a recent version of Ohm, the pexprs will be instances of the appropriate subclass of PExpr. Take this example:

import * as ohm from "ohm-js";

const arithmetic = ohm.grammar(`
  Arithmetic {
    Exp = Exp "+" Term -- plus
        | Term
    Term = Term "*" number -- times
         | number
    number = digit+
  }`);

for (const [ruleName, ruleInfo] of Object.entries(arithmetic.rules)) {
  const { body } = ruleInfo;
  console.log(body.constructor.name);
}

This produces

Seq
Alt
Seq
Alt
Plus

Unfortunately I don't think we have a generic w…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by evilguest
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants