Browse Source

Improved documentation and code style (minor)

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.6.0
Erez Shinan 7 years ago
parent
commit
847870fdc8
5 changed files with 28 additions and 9 deletions
  1. +16
    -3
      README.md
  2. +6
    -3
      examples/calc.py
  3. +1
    -1
      examples/conf_earley.py
  4. +1
    -1
      examples/conf_lalr.py
  5. +4
    -1
      examples/json_parser.py

+ 16
- 3
README.md View File

@@ -22,6 +22,7 @@ Most importantly, Lark will save you time and prevent you from getting parsing h

- [Documentation wiki](https://github.com/erezsh/lark/wiki)
- [Tutorial](/docs/json_tutorial.md) for writing a JSON parser.
- [Cheatsheet (PDF)](/docs/lark_cheatsheet.pdf)
- Blog post: [How to write a DSL with Lark](http://blog.erezsh.com/how-to-write-a-dsl-in-python-with-lark/)
- [Forum @googlegroups](https://groups.google.com/forum/#!forum/lark-parser) (New)

@@ -33,16 +34,27 @@ Lark has no dependencies.

[![Build Status](https://travis-ci.org/lark-parser/lark.svg?branch=master)](https://travis-ci.org/lark-parser/lark)

### Syntax Highlighting (new)

Lark now provides syntax highlighting for its grammar files (\*.lark):

- [Sublime Text & TextMate](https://github.com/lark-parser/lark_syntax)
- [vscode](https://github.com/lark-parser/vscode-lark)


### Hello World

Here is a little program to parse "Hello, World!" (Or any other similar phrase):

```python
from lark import Lark

l = Lark('''start: WORD "," WORD "!"
%import common.WORD
%ignore " "

%import common.WORD // imports from terminal library
%ignore " " // Disregard spaces in text
''')

print( l.parse("Hello, World!") )
```

@@ -56,7 +68,7 @@ Notice punctuation doesn't appear in the resulting tree. It's automatically filt

### Fruit flies like bananas

Lark is very good at handling ambiguity. Here's how it parses the phrase "fruit flies like bananas":
Lark is great at handling ambiguity. Let's parse the phrase "fruit flies like bananas":

![fruitflies.png](examples/fruitflies.png)

@@ -153,6 +165,7 @@ Lark is currently accepting pull-requests.

There are many ways you can help the project:

* Help solve issues
* Improve the documentation
* Write new grammars for Lark's library
* Write a blog post introducing Lark to your audience


+ 6
- 3
examples/calc.py View File

@@ -4,6 +4,7 @@

from lark import Lark, Transformer, v_args


try:
input = raw_input # For Python2 compatibility
except NameError:
@@ -34,7 +35,8 @@ calc_grammar = """
%ignore WS_INLINE
"""

@v_args(inline=True)

@v_args(inline=True) # Affects the signatures of the methods
class CalculateTree(Transformer):
from operator import add, sub, mul, truediv as div, neg
number = float
@@ -50,10 +52,10 @@ class CalculateTree(Transformer):
return self.vars[name]



calc_parser = Lark(calc_grammar, parser='lalr', transformer=CalculateTree())
calc = calc_parser.parse


def main():
while True:
try:
@@ -62,11 +64,12 @@ def main():
break
print(calc(s))


def test():
print(calc("a = 1+2"))
print(calc("1+a*-3"))


if __name__ == '__main__':
# test()
main()


+ 1
- 1
examples/conf_earley.py View File

@@ -19,9 +19,9 @@ parser = Lark(r"""
section: "[" NAME "]" _NL item+
item: NAME "=" VALUE? _NL
VALUE: /./+

%import common.CNAME -> NAME
%import common.NEWLINE -> _NL

%import common.WS_INLINE
%ignore WS_INLINE
""", parser="earley")


+ 1
- 1
examples/conf_lalr.py View File

@@ -20,9 +20,9 @@ parser = Lark(r"""
section: "[" NAME "]" _NL item+
item: NAME "=" VALUE? _NL
VALUE: /./+

%import common.CNAME -> NAME
%import common.NEWLINE -> _NL

%import common.WS_INLINE
%ignore WS_INLINE
""", parser="lalr")


+ 4
- 1
examples/json_parser.py View File

@@ -33,6 +33,7 @@ json_grammar = r"""
%ignore WS
"""


class TreeToJson(Transformer):
@v_args(inline=True)
def string(self, s):
@@ -47,6 +48,7 @@ class TreeToJson(Transformer):
true = lambda self, _: True
false = lambda self, _: False


# json_parser = Lark(json_grammar, parser='earley', lexer='standard')
# def parse(x):
# return TreeToJson().transform(json_parser.parse(x))
@@ -54,6 +56,7 @@ class TreeToJson(Transformer):
json_parser = Lark(json_grammar, parser='lalr', lexer='standard', transformer=TreeToJson())
parse = json_parser.parse


def test():
test_json = '''
{
@@ -71,8 +74,8 @@ def test():
import json
assert j == json.loads(test_json)


if __name__ == '__main__':
# test()
with open(sys.argv[1]) as f:
print(parse(f.read()))


Loading…
Cancel
Save