@@ -100,7 +100,7 @@ See more [examples here](https://github.com/lark-parser/lark/tree/master/example | |||||
- **Python 2 & 3** compatible | - **Python 2 & 3** compatible | ||||
- Automatic line & column tracking | - Automatic line & column tracking | ||||
- Standard library of terminals (strings, numbers, names, etc.) | - Standard library of terminals (strings, numbers, names, etc.) | ||||
- Import grammars from Nearley.js | |||||
- Import grammars from Nearley.js ([read more](/docs/nearley.md)) | |||||
- Extensive test suite [](https://codecov.io/gh/erezsh/lark) | - Extensive test suite [](https://codecov.io/gh/erezsh/lark) | ||||
- MyPy support using type stubs | - MyPy support using type stubs | ||||
- And much more! | - And much more! | ||||
@@ -159,36 +159,6 @@ Check out the [JSON tutorial](/docs/json_tutorial.md#conclusion) for more detail | |||||
Using Lark? Send me a message and I'll add your project! | Using Lark? Send me a message and I'll add your project! | ||||
### How to use Nearley grammars in Lark | |||||
Lark comes with a tool to convert grammars from [Nearley](https://github.com/Hardmath123/nearley), a popular Earley library for Javascript. It uses [Js2Py](https://github.com/PiotrDabkowski/Js2Py) to convert and run the Javascript postprocessing code segments. | |||||
First, ensure you have Lark installed with the `nearley` component included: | |||||
```bash | |||||
pip install lark-parser[nearley] | |||||
``` | |||||
Here's an example: | |||||
```bash | |||||
git clone https://github.com/Hardmath123/nearley | |||||
python -m lark.tools.nearley nearley/examples/calculator/arithmetic.ne main nearley > ncalc.py | |||||
``` | |||||
You can use the output as a regular python module: | |||||
```python | |||||
>>> import ncalc | |||||
>>> ncalc.parse('sin(pi/4) ^ e') | |||||
0.38981434460254655 | |||||
``` | |||||
The Nearley converter also supports an experimental converter for newer JavaScript (ES6+), using the `--es6` flag: | |||||
```bash | |||||
git clone https://github.com/Hardmath123/nearley | |||||
python -m lark.tools.nearley nearley/examples/calculator/arithmetic.ne main nearley --es6 > ncalc.py | |||||
``` | |||||
## License | ## License | ||||
Lark uses the [MIT license](LICENSE). | Lark uses the [MIT license](LICENSE). | ||||
@@ -21,7 +21,7 @@ | |||||
# Extra features | # Extra features | ||||
- Import rules and tokens from other Lark grammars, for code reuse and modularity. | - Import rules and tokens from other Lark grammars, for code reuse and modularity. | ||||
- Import grammars from Nearley.js | |||||
- Import grammars from Nearley.js ([read more](/docs/nearley.md)) | |||||
- CYK parser | - CYK parser | ||||
### Experimental features | ### Experimental features | ||||
@@ -49,6 +49,7 @@ $ pip install lark-parser | |||||
* [Visitors & Transformers](visitors.md) | * [Visitors & Transformers](visitors.md) | ||||
* [Classes](classes.md) | * [Classes](classes.md) | ||||
* [Cheatsheet (PDF)](lark_cheatsheet.pdf) | * [Cheatsheet (PDF)](lark_cheatsheet.pdf) | ||||
* [Importing grammars from Nearley](nearley.md) | |||||
* Discussion | * Discussion | ||||
* [Gitter](https://gitter.im/lark-parser/Lobby) | * [Gitter](https://gitter.im/lark-parser/Lobby) | ||||
* [Forum (Google Groups)](https://groups.google.com/forum/#!forum/lark-parser) | * [Forum (Google Groups)](https://groups.google.com/forum/#!forum/lark-parser) |
@@ -0,0 +1,47 @@ | |||||
# Importing grammars from Nearley | |||||
Lark comes with a tool to convert grammars from [Nearley](https://github.com/Hardmath123/nearley), a popular Earley library for Javascript. It uses [Js2Py](https://github.com/PiotrDabkowski/Js2Py) to convert and run the Javascript postprocessing code segments. | |||||
## Requirements | |||||
1. Install Lark with the `nearley` component: | |||||
```bash | |||||
pip install lark-parser[nearley] | |||||
``` | |||||
2. Acquire a copy of the nearley codebase. This can be done using: | |||||
```bash | |||||
git clone https://github.com/Hardmath123/nearley | |||||
``` | |||||
## Usage | |||||
Here's an example of how to import nearley's calculator example into Lark: | |||||
```bash | |||||
git clone https://github.com/Hardmath123/nearley | |||||
python -m lark.tools.nearley nearley/examples/calculator/arithmetic.ne main nearley > ncalc.py | |||||
``` | |||||
You can use the output as a regular python module: | |||||
```python | |||||
>>> import ncalc | |||||
>>> ncalc.parse('sin(pi/4) ^ e') | |||||
0.38981434460254655 | |||||
``` | |||||
The Nearley converter also supports an experimental converter for newer JavaScript (ES6+), using the `--es6` flag: | |||||
```bash | |||||
git clone https://github.com/Hardmath123/nearley | |||||
python -m lark.tools.nearley nearley/examples/calculator/arithmetic.ne main nearley --es6 > ncalc.py | |||||
``` | |||||
## Notes | |||||
- Lark currently cannot import templates from Nearley | |||||
- Lark currently cannot export grammars to Nearley | |||||
These might get added in the future, if enough users ask for them. |
@@ -1,4 +1,4 @@ | |||||
"Converts between Lark and Nearley grammars. Work in progress!" | |||||
"Converts Nearley grammars to Lark" | |||||
import os.path | import os.path | ||||
import sys | import sys | ||||
@@ -182,7 +182,7 @@ def main(fn, start, nearley_lib, es6=False): | |||||
grammar = f.read() | grammar = f.read() | ||||
return create_code_for_nearley_grammar(grammar, start, os.path.join(nearley_lib, 'builtin'), os.path.abspath(os.path.dirname(fn)), es6=es6) | return create_code_for_nearley_grammar(grammar, start, os.path.join(nearley_lib, 'builtin'), os.path.abspath(os.path.dirname(fn)), es6=es6) | ||||
def get_parser(): | |||||
def get_arg_parser(): | |||||
parser = argparse.ArgumentParser('Reads Nearley grammar (with js functions) outputs an equivalent lark parser.') | parser = argparse.ArgumentParser('Reads Nearley grammar (with js functions) outputs an equivalent lark parser.') | ||||
parser.add_argument('nearley_grammar', help='Path to the file containing the nearley grammar') | parser.add_argument('nearley_grammar', help='Path to the file containing the nearley grammar') | ||||
parser.add_argument('start_rule', help='Rule within the nearley grammar to make the base rule') | parser.add_argument('start_rule', help='Rule within the nearley grammar to make the base rule') | ||||
@@ -191,6 +191,6 @@ def get_parser(): | |||||
return parser | return parser | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
parser = get_parser() | |||||
parser = get_arg_parser() | |||||
args = parser.parse_args() | args = parser.parse_args() | ||||
print(main(fn=args.nearley_grammar, start=args.start_rule, nearley_lib=args.nearley_lib, es6=args.es6)) | print(main(fn=args.nearley_grammar, start=args.start_rule, nearley_lib=args.nearley_lib, es6=args.es6)) |