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

audinue picture audinue  路  13Comments

futagoza picture futagoza  路  13Comments

brettz9 picture brettz9  路  8Comments

vldmr1986 picture vldmr1986  路  12Comments

StoneCypher picture StoneCypher  路  6Comments