Skip to content

Commit

Permalink
Add unit tests; fix scanner bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingcitaldo125 committed May 9, 2021
1 parent e9ba8c0 commit 6308da6
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 11 deletions.
Empty file added __init__.py
Empty file.
Empty file added earley/__init__.py
Empty file.
Binary file added earley/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added earley/__pycache__/earley.cpython-36.pyc
Binary file not shown.
14 changes: 3 additions & 11 deletions earley.py → earley/earley.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def scan(S, k, state, words, do_state_print=False):
# If we've either gone 'over the deep end(k is out of range)', or we're in range,
# but the word is not a part of speech, then we can be reasonably certain that
# the input is not within the language
if words[k] in parts_of_speech and (nxt_elem_scanner == words[k] or nxt_elem_scanner == 'number'):
if words[k] in parts_of_speech:
n_lax = swap_around_dot(state, dot)

nitem = (state[0], n_lax, state[2])
Expand Down Expand Up @@ -204,7 +204,7 @@ def earley_parse(words, grammar, do_state_print=False):
if not is_finished(state, dot):
nxt_elem = get_next_element(state, dot)
if is_nonterminal(nxt_elem): # predict
added = predict(S, k, nxt_elem, words, grammar, do_state_print = do_state_print)
added = predict(S, k, nxt_elem, words, grammar, do_state_print = False)
else: # scanner(terminal)
added = scan(S, k, state, words, do_state_print = do_state_print)
else: # we should be finished with a particular production
Expand Down Expand Up @@ -245,16 +245,8 @@ def process_grammar(grammar):


def main():
pgrammar = process_grammar(load_grammar(".\\grammar.txt"))

# TBD: Move to unit tests
pgrammar = process_grammar(load_grammar("..\\grammars\\grammar.txt"))
print(earley_parse("2+3*4", pgrammar)) # True
print(earley_parse("1", pgrammar)) # True
print(earley_parse("1+", pgrammar)) # False
print(earley_parse("1+2", pgrammar)) # True
print(earley_parse("1+2*", pgrammar)) # False
print(earley_parse("1+2*3", pgrammar)) # True
print(earley_parse("1+2*3/", pgrammar)) # False

if __name__ == "__main__":
main()
File renamed without changes.
Empty file added tests/__init__.py
Empty file.
Binary file added tests/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file not shown.
87 changes: 87 additions & 0 deletions tests/test_earley.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'''test_earley.py'''
import sys
import pytest

sys.path.insert(0, '..')

from earley.earley import load_grammar, process_grammar, earley_parse


@pytest.fixture
def grammar_raw():
gramm = load_grammar('..\\grammars\\grammar.txt')
return gramm

@pytest.fixture
def grammar(grammar_raw):
gram_dict = process_grammar(grammar_raw)
return gram_dict


def test_process_grammar_pass(grammar):
assert(grammar=={'P': 'S', 'S': 'S + M | M', 'M': 'M * T | T', 'T': 'number'})

def test_single_number_pass(grammar):
res = earley_parse("1", grammar)

assert(res)

def test_full_example_pass(grammar):
res = earley_parse("2+3*4", grammar)

assert(res)

def test_all_integers_pass(grammar):
res = earley_parse("0+1+2+3+4+5+6+7+8+9", grammar)

assert(res)

def test_addition_pass_1(grammar):
res = earley_parse("1+2", grammar)

assert(res)

def test_addition_pass_2(grammar):
res = earley_parse("1+2+3", grammar)

assert(res)

def test_addition_mul_pass(grammar):
res = earley_parse("1+2*3", grammar)

assert(res)

def test_fail_1(grammar):
res = earley_parse("1+", grammar)

assert res == False

def test_fail_2(grammar):
res = earley_parse("1+2*", grammar)

assert res == False

def test_fail_3(grammar):
res = earley_parse("1+2*3/", grammar)

assert res == False

def test_sub_fail_1(grammar):
res = earley_parse("1-2", grammar)

assert res == False

def test_sub_fail_2(grammar):
res = earley_parse("1-", grammar)

assert res == False

def test_div_fail_1(grammar):
res = earley_parse("1/2", grammar)

assert res == False

def test_div_fail_2(grammar):
res = earley_parse("1/", grammar)

assert res == False

0 comments on commit 6308da6

Please sign in to comment.