欧姆:解析变得简单

  • Ohm Overview: Ohm is a JavaScript parsing library created at HARC for language research. It's a language implementation toolkit to prototype new languages and experiment with extensions. Can be used to parse custom file formats and build parsers, interpreters, and compilers.
  • Setup:

    • Browser: Load directly from unpkg by adding <script src="https://unpkg.com/ohm/dist/ohm.js"></script> to get the global ohm.
    • Node.js: Install ohm-js using npm install ohm-js, then require('ohm-js') to load the module.
  • Getting Started:

    • Consists of a domain-specific language (based on parsing expression grammars - PEGs) and a library.
    • Recommended to use the Ohm Editor for grammar writing as its parsing visualization helps in understanding and debugging.
  • Creating a Grammar:

    • A grammar is made up of rules. Example: A simple "Arithmetic" grammar with a single rule "Exp" having a literal string "42". Instantiate a grammar object using the Ohm library.
  • Matching Input: Use the grammar object's match method to recognize arithmetic expressions. Returns a MatchResult object with a succeeded method.
  • Parsers and Parse Trees: A parser produces a parse tree. In Ohm, you don't directly deal with parse trees but seeing them helps understand how a grammar works.
  • Recognizing Numbers:

    • Modified grammar to recognize numbers using | (choice), range operator (..), and built-in "digit" rule. Also used repetition operators like + to match numbers with multiple digits.
  • Addition and Subtraction: Extended the grammar to support addition and subtraction with case labels. Noted that the new "Exp" rule is recursive (left recursive) and supported by Ohm. Verified with parse trees.
  • Multiplication and Division: Added "times" and "div" cases to the "Exp" rule. Handled operator precedence by refactoring into separate "MulExp" rules and updating the "plus" and "minus" cases. Verified with parse trees.
  • Defining Semantics:

    • Ohm separates grammars from semantic actions. A set of semantic actions is usually written using an action dictionary.
    • To associate semantic actions with a grammar, create a Semantics object using the createSemantics method and add an operation with an action dictionary using the addOperation method.
  • Writing Semantic Actions:

    • Semantic actions are JavaScript functions associated with rules. The number of arguments depends on the arity of the rule body.
    • Each argument is a semantic adapter representing a parse tree node. Pass-through actions can be omitted if not needed.
    • Case labels are a shorthand for declaring separate rules with the same arity.
  • Putting It All Together: A complete "eval" operation definition with omitted pass-through actions. Can use multiple operations with the same grammar by adding them to the semantics.
  • Further Reading: Refer to documentation pages like Documentation index, Syntax reference, API reference. Use the Ohm Editor to experiment with real-world grammars and join communities like Discord, GitHub Discussions, or the Ohm mailing list for questions.
阅读 9
0 条评论