- 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 globalohm
. - Node.js: Install
ohm-js
usingnpm install ohm-js
, thenrequire('ohm-js')
to load the module.
- Browser: Load directly from unpkg by adding
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 asucceeded
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.
- Modified grammar to recognize numbers using
- 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 theaddOperation
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.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。