Skip to content

Commit

Permalink
Union regexps and put it into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
KubEF committed May 6, 2024
1 parent dd9f667 commit ff9f4d4
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 159 deletions.
136 changes: 0 additions & 136 deletions tests/autotests/constants.py
@@ -1,141 +1,5 @@
from pyformlang.cfg import cfg

LABEL: str = "label"
IS_FINAL: str = "is_final"
IS_START: str = "is_start"

LABELS: list[str] = ["a", "b", "c", "d", "e", "f", "g", "h"]

REGEX_SIMPLE: list[str] = [
"(aa)*",
"a | a",
"a* | a",
"(ab) | (ac)",
"(ab) | (abc)",
"(abd) | (abc)",
"(abd*) | (abc*)",
"(abd)* | (abc)*",
"((abd) | (abc))*",
"a*a*",
"a*a*b",
"a* | (a | b)*",
"a*(a | b)*",
"(a | c)*(a | b)*",
]
REGEX_TUPLE: list[tuple[str, str]] = [
("a", "b"),
("a", "a"),
("a*", "a"),
("a*", "aa"),
("a*", "a*"),
("(aa)*", "a*"),
("(a|b)*", "a*"),
("(a|b)*", "b"),
("(a|b)*", "bbb"),
("a|b", "a"),
("a|b", "a|c"),
("(a|b)(c|d)", "(a|c)(b|d)"),
("(a|b)*", "(a|c)*"),
("a*b*", "(a|b)*"),
("(ab)*", "(a|b)*"),
]
QUERIES: list[str] = [
"a",
"a*",
"ab",
"abc",
"abcd",
"a*b*",
"(ab)*",
"ab*",
"ab*c*",
"ab*c",
"abc*",
"(a|b|c|d|e)*",
"(a|b|c|d|e)(a|b|c|d|e)*",
"(a|b|c|d|e)f*",
"(a|b)*",
"(a|b)*(c|d)*",
"(a | b)*(c | d)*(e | f)*",
"(a | b | c)*(d | e | f)*",
"((a|b)*c)*",
"((a | b) * c)*(d | e)",
"((a | b)*c)*((d | e)*f)*",
]

REGEXP_CFG: dict[str, list[cfg.CFG]] = {
"a": [cfg.CFG.from_text("S -> a"), cfg.CFG.from_text("S -> N B\nB -> $\nN -> a")],
"a*": [
cfg.CFG.from_text("S -> $ | a S"),
cfg.CFG.from_text("S -> $ | S S | a"),
cfg.CFG.from_text("S -> S a S | $"),
],
"a b c": [cfg.CFG.from_text("S -> a b c"), cfg.CFG.from_text("S -> a B\nB -> b c")],
"a*b*": [
cfg.CFG.from_text("S -> S1 S2\nS2 -> $ | b S2\nS1 -> $ | a S1"),
cfg.CFG.from_text("S -> $ | S1 | a S\nS1 -> $ | b S1"),
],
"(a b)*": [
cfg.CFG.from_text("S -> $ | a b S"),
cfg.CFG.from_text("S -> $ | S S1\nS1 -> a b"),
],
"a b*c*": [
cfg.CFG.from_text("S -> S1 S2 S3\nS1 -> a\nS2 -> $ | S2 b\nS3 -> $ | c S3"),
cfg.CFG.from_text("S -> a S2 S3\nS2 -> S2 b | $\nS3 -> c | $ | S3 S3"),
],
"(a|b|c|d|e)*": [
cfg.CFG.from_text("S -> $ | S1 S\nS1 -> a | b | c | d | e"),
cfg.CFG.from_text("S -> $ | a | b | c | d | e | S S"),
cfg.CFG.from_text("S -> $ | a S | b S | c S | e S | d S"),
],
"((a | b) * c)*(d | e)": [
cfg.CFG.from_text(
"S -> S1 S2\nS1 -> S1 S1 | $ | S3 c\n S2 -> d | e\n S3 -> b S3 | $ | a S3"
),
cfg.CFG.from_text("S -> S1 d | S1 e\nS1 -> S1 S3 c | $\nS3 -> b S3 | $ | a S3"),
],
}
GRAMMARS: list[list[cfg.CFG]] = [
[
cfg.CFG.from_text("S -> $ | a S b | S S"),
cfg.CFG.from_text("S -> $ | a S b S"),
cfg.CFG.from_text("S -> $ | S a S b"),
cfg.CFG.from_text("S -> $ | a S b | S S S"),
],
[
cfg.CFG.from_text("S -> $ | a S b | c S d | S S"),
cfg.CFG.from_text("S -> $ | a S b S | c S d S"),
cfg.CFG.from_text("S -> $ | S a S b | S c S d"),
cfg.CFG.from_text("S -> $ | a S b | c S d S | S S S"),
],
[
cfg.CFG.from_text("S -> $ | S1 S S2\nS1 -> a | c\n S2 -> b | d\n S -> S S"),
cfg.CFG.from_text("S -> $ | S1 S S2 S\n S1 -> a | c\nS2 -> b | d"),
cfg.CFG.from_text("S -> $ | S a S b | S a S d | S c S d | S c S b"),
cfg.CFG.from_text("S -> $ | S1 S S2 | S S S\nS1 -> a | c\nS2-> b | d"),
],
[
cfg.CFG.from_text("S -> S S | Se S1 Se\nSe -> $ | Se e\nS1 -> $ | a S1 b"),
cfg.CFG.from_text("S -> S1 | S S | e\nS1 -> $ | a S1 b"),
cfg.CFG.from_text("S -> S2 S | $\n S2 -> e | S1\n S1 -> $ | a S1 b"),
cfg.CFG.from_text("S -> $ | S1 S | e S\n S1 -> $ | a S1 b"),
],
[
cfg.CFG.from_text("S -> a S | $"),
cfg.CFG.from_text("S -> S1 | a\nS1 -> a S1 | $"),
],
]
GRAMMARS_DIFFERENT: list[cfg.CFG] = [
cfg.CFG.from_text(
"S -> S1 | S2\nS1 -> Sab | S1 c\nSab -> $ | a Sab b\nS2 -> Sbc | a S2\nSbc -> b Sbc c"
),
cfg.CFG.from_text("S -> a | b | S c S | S d S | e S f | g S"),
cfg.CFG.from_text("S -> $ | a S b | b S a | e S f | S S | c S d | f S c | f S e"),
]
EBNF_GRAMMARS: list[str] = [
"""S -> ( Sab c* ) | ( a* Sbc )
Sab -> a ( Sab | $ ) b
Sbc -> b ( Sbc | $ ) c""",
"S -> a | b | (S ( c | d ) S ) | ( e S f ) | ( g S )",
"S -> ( ( a S b ) | ( b S a ) | ( c S d ) | ( d S c ) | ( e S f ) | (f S e) )*",
]
78 changes: 78 additions & 0 deletions tests/autotests/grammars_constants.py
@@ -0,0 +1,78 @@
from pyformlang.cfg import cfg

REGEXP_CFG: dict[str, list[cfg.CFG]] = {
"a": [cfg.CFG.from_text("S -> a"), cfg.CFG.from_text("S -> N B\nB -> $\nN -> a")],
"a*": [
cfg.CFG.from_text("S -> $ | a S"),
cfg.CFG.from_text("S -> $ | S S | a"),
cfg.CFG.from_text("S -> S a S | $"),
],
"a b c": [cfg.CFG.from_text("S -> a b c"), cfg.CFG.from_text("S -> a B\nB -> b c")],
"a*b*": [
cfg.CFG.from_text("S -> S1 S2\nS2 -> $ | b S2\nS1 -> $ | a S1"),
cfg.CFG.from_text("S -> $ | S1 | a S\nS1 -> $ | b S1"),
],
"(a b)*": [
cfg.CFG.from_text("S -> $ | a b S"),
cfg.CFG.from_text("S -> $ | S S1\nS1 -> a b"),
],
"a b*c*": [
cfg.CFG.from_text("S -> S1 S2 S3\nS1 -> a\nS2 -> $ | S2 b\nS3 -> $ | c S3"),
cfg.CFG.from_text("S -> a S2 S3\nS2 -> S2 b | $\nS3 -> c | $ | S3 S3"),
],
"(a|b|c|d|e)*": [
cfg.CFG.from_text("S -> $ | S1 S\nS1 -> a | b | c | d | e"),
cfg.CFG.from_text("S -> $ | a | b | c | d | e | S S"),
cfg.CFG.from_text("S -> $ | a S | b S | c S | e S | d S"),
],
"((a | b) * c)*(d | e)": [
cfg.CFG.from_text(
"S -> S1 S2\nS1 -> S1 S1 | $ | S3 c\n S2 -> d | e\n S3 -> b S3 | $ | a S3"
),
cfg.CFG.from_text("S -> S1 d | S1 e\nS1 -> S1 S3 c | $\nS3 -> b S3 | $ | a S3"),
],
}
GRAMMARS: list[list[cfg.CFG]] = [
[
cfg.CFG.from_text("S -> $ | a S b | S S"),
cfg.CFG.from_text("S -> $ | a S b S"),
cfg.CFG.from_text("S -> $ | S a S b"),
cfg.CFG.from_text("S -> $ | a S b | S S S"),
],
[
cfg.CFG.from_text("S -> $ | a S b | c S d | S S"),
cfg.CFG.from_text("S -> $ | a S b S | c S d S"),
cfg.CFG.from_text("S -> $ | S a S b | S c S d"),
cfg.CFG.from_text("S -> $ | a S b | c S d S | S S S"),
],
[
cfg.CFG.from_text("S -> $ | S1 S S2\nS1 -> a | c\n S2 -> b | d\n S -> S S"),
cfg.CFG.from_text("S -> $ | S1 S S2 S\n S1 -> a | c\nS2 -> b | d"),
cfg.CFG.from_text("S -> $ | S a S b | S a S d | S c S d | S c S b"),
cfg.CFG.from_text("S -> $ | S1 S S2 | S S S\nS1 -> a | c\nS2-> b | d"),
],
[
cfg.CFG.from_text("S -> S S | Se S1 Se\nSe -> $ | Se e\nS1 -> $ | a S1 b"),
cfg.CFG.from_text("S -> S1 | S S | e\nS1 -> $ | a S1 b"),
cfg.CFG.from_text("S -> S2 S | $\n S2 -> e | S1\n S1 -> $ | a S1 b"),
cfg.CFG.from_text("S -> $ | S1 S | e S\n S1 -> $ | a S1 b"),
],
[
cfg.CFG.from_text("S -> a S | $"),
cfg.CFG.from_text("S -> S1 | a\nS1 -> a S1 | $"),
],
]
GRAMMARS_DIFFERENT: list[cfg.CFG] = [
cfg.CFG.from_text(
"S -> S1 | S2\nS1 -> Sab | S1 c\nSab -> $ | a Sab b\nS2 -> Sbc | a S2\nSbc -> b Sbc c"
),
cfg.CFG.from_text("S -> a | b | S c S | S d S | e S f | g S"),
cfg.CFG.from_text("S -> $ | a S b | b S a | e S f | S S | c S d | f S c | f S e"),
]
EBNF_GRAMMARS: list[str] = [
"""S -> ( Sab c* ) | ( a* Sbc )
Sab -> a ( Sab | $ ) b
Sbc -> b ( Sbc | $ ) c""",
"S -> a | b | (S ( c | d ) S ) | ( e S f ) | ( g S )",
"S -> ( ( a S b ) | ( b S a ) | ( c S d ) | ( d S c ) | ( e S f ) | (f S e) )*",
]
43 changes: 43 additions & 0 deletions tests/autotests/regex_constants.py
@@ -0,0 +1,43 @@
REGEXES = [
"(abd*) | (abc*)",
"(ab)*",
"a*a*",
"bbb",
"(a | b | c)*(d | e | f)*",
"a | a",
"(ab) | (ac)",
"abc*",
"abc",
"(abd) | (abc)",
"a* | (a | b)*",
"(a|b|c|d|e)(a|b|c|d|e)*",
"(a|b)*(c|d)*",
"(a|b)*",
"a* | a",
"(a|b|c|d|e)f*",
"aa",
"ab*",
"ab",
"ab*c*",
"(ab) | (abc)",
"a|c",
"(a|c)(b|d)",
"b",
"a",
"a*a*b",
"((a | b)*c)*((d | e)*f)*",
"((abd) | (abc))*",
"(a|c)*",
"((a | b) * c)*(d | e)",
"a*b*",
"(a | c)*(a | b)*",
"(a | b)*(c | d)*(e | f)*",
"a*(a | b)*",
"(abd)* | (abc)*",
"ab*c",
"(aa)*",
"a*",
"(a|b|c|d|e)*",
"((a|b)*c)*",
"abcd",
]
7 changes: 4 additions & 3 deletions tests/autotests/test_task2.py
Expand Up @@ -8,9 +8,10 @@
import pytest
import random
import itertools
from helper import GraphWordsHelper, generate_rnd_graph, generate_rnd_start_and_final
from constants import IS_FINAL, IS_START, REGEX_SIMPLE
from helper import GraphWordsHelper, generate_rnd_start_and_final
from constants import IS_FINAL, IS_START
from fixtures import small_graph
from regex_constants import REGEXES

# Fix import statements in try block to run tests
try:
Expand All @@ -20,7 +21,7 @@


class TestRegexToDfa:
@pytest.mark.parametrize("regex_str", REGEX_SIMPLE)
@pytest.mark.parametrize("regex_str", REGEXES)
def test(self, regex_str: str) -> None:
regex = Regex(regex_str)
regex_cfg = regex.to_cfg()
Expand Down
6 changes: 4 additions & 2 deletions tests/autotests/test_task3.py
Expand Up @@ -7,7 +7,7 @@
import pytest
import random
import itertools
from constants import REGEX_TUPLE
from regex_constants import REGEXES

# Fix import statements in try block to run tests
try:
Expand All @@ -18,7 +18,9 @@


class TestIntersect:
@pytest.mark.parametrize("regex_str1, regex_str2", REGEX_TUPLE)
@pytest.mark.parametrize(
"regex_str1, regex_str2", itertools.combinations(REGEXES, 2)
)
def test(self, regex_str1: str, regex_str2: str) -> None:
dfa1 = FiniteAutomaton(regex_to_dfa(regex_str1))
dfa2 = FiniteAutomaton(regex_to_dfa(regex_str2))
Expand Down
5 changes: 2 additions & 3 deletions tests/autotests/test_task4.py
Expand Up @@ -5,8 +5,7 @@
import random
from copy import deepcopy
import pytest
from networkx import MultiDiGraph
from constants import QUERIES, LABELS
from regex_constants import REGEXES
from helper import generate_rnd_start_and_final
from fixtures import graph

Expand All @@ -21,7 +20,7 @@

@pytest.fixture(scope="class", params=range(5))
def query(request) -> str:
return random.choice(QUERIES)
return random.choice(REGEXES)


class TestReachability:
Expand Down
2 changes: 1 addition & 1 deletion tests/autotests/test_task6.py
Expand Up @@ -4,7 +4,7 @@
# Otherwise, please report it
import itertools
import pytest
from constants import REGEXP_CFG, GRAMMARS
from grammars_constants import REGEXP_CFG, GRAMMARS
from rpq_template_test import rpq_cfpq_test, different_grammars_test
from fixtures import graph

Expand Down
2 changes: 1 addition & 1 deletion tests/autotests/test_task7.py
Expand Up @@ -5,7 +5,7 @@
import itertools
from copy import deepcopy
import pytest
from constants import REGEXP_CFG, GRAMMARS, GRAMMARS_DIFFERENT
from grammars_constants import REGEXP_CFG, GRAMMARS, GRAMMARS_DIFFERENT
from helper import generate_rnd_start_and_final
from rpq_template_test import rpq_cfpq_test, different_grammars_test
from fixtures import graph
Expand Down
2 changes: 1 addition & 1 deletion tests/autotests/test_task8.py
Expand Up @@ -5,7 +5,7 @@
import itertools
from copy import deepcopy
import pytest
from constants import REGEXP_CFG, GRAMMARS, GRAMMARS_DIFFERENT, EBNF_GRAMMARS
from grammars_constants import REGEXP_CFG, GRAMMARS, GRAMMARS_DIFFERENT, EBNF_GRAMMARS
from helper import generate_rnd_start_and_final
from rpq_template_test import rpq_cfpq_test, different_grammars_test
from fixtures import graph
Expand Down

0 comments on commit ff9f4d4

Please sign in to comment.