Pegjs: Recover grammar from peg parser

Created on 5 Jul 2019  ·  1Comment  ·  Source: pegjs/pegjs

Question

Has anyone done work to convert the output of peg back to the input grammar?

Description

I have recently inherited a project that uses PEG. We have the PEG parser but the source grammar seems to be lost or missing. Is it possible to recover the grammar from only the PEG parser code?

Steps to Reproduce

  1. Given a peg parser
  2. Recover the source grammar

Software

  • PEG.js: 0.10.0
  • Node.js: 12.6.0

Most helpful comment

The process is pretty straightforward to do by hand.

  • Every rule is a function that starts with peg$parse.
  • Every
} else {
    peg$currPos = s#;
    s# = peg$FAILED;
}

means there was an item in sequence.

  • Every while (s# !== peg$FAILED) is a repetition.
  • Every if (s# === peg$FAILED) is an alternative branch of a choice.

>All comments

The process is pretty straightforward to do by hand.

  • Every rule is a function that starts with peg$parse.
  • Every
} else {
    peg$currPos = s#;
    s# = peg$FAILED;
}

means there was an item in sequence.

  • Every while (s# !== peg$FAILED) is a repetition.
  • Every if (s# === peg$FAILED) is an alternative branch of a choice.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

vldmr1986 picture vldmr1986  ·  12Comments

mreinstein picture mreinstein  ·  12Comments

richb-hanover picture richb-hanover  ·  7Comments

emmenko picture emmenko  ·  15Comments

futagoza picture futagoza  ·  13Comments