Browse Source

A few adjustments to Nearley PR (mostly docs)

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.10.0
Erez Sh 4 years ago
parent
commit
b0feed8c12
5 changed files with 53 additions and 35 deletions
  1. +1
    -31
      README.md
  2. +1
    -1
      docs/features.md
  3. +1
    -0
      docs/index.md
  4. +47
    -0
      docs/nearley.md
  5. +3
    -3
      lark/tools/nearley.py

+ 1
- 31
README.md View File

@@ -100,7 +100,7 @@ See more [examples here](https://github.com/lark-parser/lark/tree/master/example
- **Python 2 & 3** compatible
- Automatic line & column tracking
- 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 [![codecov](https://codecov.io/gh/erezsh/lark/branch/master/graph/badge.svg)](https://codecov.io/gh/erezsh/lark)
- MyPy support using type stubs
- 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!

### 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

Lark uses the [MIT license](LICENSE).


+ 1
- 1
docs/features.md View File

@@ -21,7 +21,7 @@
# Extra features

- 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

### Experimental features


+ 1
- 0
docs/index.md View File

@@ -49,6 +49,7 @@ $ pip install lark-parser
* [Visitors & Transformers](visitors.md)
* [Classes](classes.md)
* [Cheatsheet (PDF)](lark_cheatsheet.pdf)
* [Importing grammars from Nearley](nearley.md)
* Discussion
* [Gitter](https://gitter.im/lark-parser/Lobby)
* [Forum (Google Groups)](https://groups.google.com/forum/#!forum/lark-parser)

+ 47
- 0
docs/nearley.md View File

@@ -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.

+ 3
- 3
lark/tools/nearley.py View File

@@ -1,4 +1,4 @@
"Converts between Lark and Nearley grammars. Work in progress!"
"Converts Nearley grammars to Lark"

import os.path
import sys
@@ -182,7 +182,7 @@ def main(fn, start, nearley_lib, es6=False):
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)

def get_parser():
def get_arg_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('start_rule', help='Rule within the nearley grammar to make the base rule')
@@ -191,6 +191,6 @@ def get_parser():
return parser

if __name__ == '__main__':
parser = get_parser()
parser = get_arg_parser()
args = parser.parse_args()
print(main(fn=args.nearley_grammar, start=args.start_rule, nearley_lib=args.nearley_lib, es6=args.es6))

Loading…
Cancel
Save