Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add draft version of multi-line grammar #7

Merged
merged 7 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ To run the test suite, `pytest` is recommended, and can be done via:
```bash
pytest -v --maxfail=1 larktools/tests/test_suite.py
```

### Debugging

For grammar development using an alternative, less optimized parsing strategy can help avoiding rule conflicts: Use `earley` instead of `lalr`
8 changes: 7 additions & 1 deletion src/larktools/ebnf_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

start: assign_var

assign_var: VARNAME "=" arith_expr
julia-sprenger marked this conversation as resolved.
Show resolved Hide resolved
assign_var: VARNAME "=" arith_expr

variable: VARNAME ("[" INDEX "]")*
VARNAME: LETTER (LETTER | DIGIT)*
Expand All @@ -26,6 +26,11 @@
// but without the fancy tree shaping directives explained at
// https://lark-parser.readthedocs.io/en/stable/tree_construction.html


line: arith_expr

multi_line_block: (line _NL? | _NL )*

arith_expr: sum
sum: product | addition | subtraction
addition: sum "+" product
Expand Down Expand Up @@ -57,4 +62,5 @@

%import common.WS_INLINE
%ignore WS_INLINE
%import common.NEWLINE -> _NL
"""
16 changes: 16 additions & 0 deletions src/larktools/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ def eval_bracketed_arith_expr(node, env):
assert get_name(child) == "arith_expr"
return eval_arith_expr(child, env)

def eval_line(node, env):
# this is the content of a single line of input
child = get_children(node)[0]
child_name = get_name(child)
if child_name == "arith_expr":
return eval_arith_expr(child, env)

def eval_multi_line_block(node, env):
# this can be either an arithmetic expression or
# composed lines
children = get_children(node)
for child in children:
child_name = get_name(child)
assert child_name == "line"
res = eval_line(child, env)
return res

def eval_variable(node, env):
children = get_children(node)
Expand Down
32 changes: 32 additions & 0 deletions tests/test_syntax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest
from typing import Optional, Union

from lark import Lark

from larktools.ebnf_grammar import grammar
from larktools.evaluation import eval_multi_line_block


class SyntaxParser:
def __init__(self):
self.parser = Lark(grammar, parser="lalr", start="multi_line_block")
self.parse = self.parser.parse

def parse_and_eval(self, expression: str, env: Optional[Union[None, dict]] = None) -> Union[int, float]:
tree = self.parse(expression)
res = eval_multi_line_block(tree, {} if env is None else env)
return res


def _parse_and_assert(expression: str, expected: Union[int, float]) -> None:
parser = SyntaxParser()
res = parser.parse_and_eval(expression)
assert expected == res

def test_multi_line():
_parse_and_assert("5\n8", 8)
_parse_and_assert("\n\n\n8", 8)
_parse_and_assert("8\n\n\n", 8)
_parse_and_assert("5+5\n3+4\n1+2", 3)
_parse_and_assert("\n\n5\n\n3\n8", 8)