turing-py 2.2.2__tar.gz → 2.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {turing_py-2.2.2 → turing_py-2.3.0}/PKG-INFO +1 -1
- {turing_py-2.2.2 → turing_py-2.3.0}/pyproject.toml +1 -1
- turing_py-2.3.0/src/tmpy/dsl/loader.py +129 -0
- turing_py-2.2.2/src/tmpy/dsl/loader.py +0 -63
- {turing_py-2.2.2 → turing_py-2.3.0}/LICENSE +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/README.md +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/__init__.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/alphabet/__init__.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/alphabet/input_alphabet.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/alphabet/symbol.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/alphabet/tape_alphabet.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/dsl/__init__.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/dsl/ast.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/dsl/lexer.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/dsl/parser.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/dsl/token.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/exception.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/machine/__init__.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/machine/machine.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/machine/states.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/machine/tape.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/transition/__init__.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/transition/direction.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/transition/transition.py +0 -0
- {turing_py-2.2.2 → turing_py-2.3.0}/src/tmpy/transition/transition_function.py +0 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
@author: Eric Santos <ericshantos13@gmail.com>
|
|
4
|
+
|
|
5
|
+
Loader for the Turing machine DSL.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from ..alphabet import Alphabet, Symbol, TapeAlphabet
|
|
11
|
+
from ..machine import States, TuringMachine
|
|
12
|
+
from ..transition import Direction, Transition, TransitionFunction
|
|
13
|
+
from .ast import MachineNode
|
|
14
|
+
from .lexer import Lexer
|
|
15
|
+
from .parser import Parser
|
|
16
|
+
from .token import Token
|
|
17
|
+
|
|
18
|
+
move_map = {"L": Direction.LEFT, "R": Direction.RIGHT, "S": Direction.STAY}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class MachineLoader:
|
|
22
|
+
|
|
23
|
+
@classmethod
|
|
24
|
+
def load(cls, path: str | Path) -> TuringMachine:
|
|
25
|
+
|
|
26
|
+
text = cls._read_file(path)
|
|
27
|
+
tokens = cls._lex(text)
|
|
28
|
+
ast = cls._parse(tokens)
|
|
29
|
+
|
|
30
|
+
symbols, blank = cls._collect_symbols(ast)
|
|
31
|
+
|
|
32
|
+
alphabet, tape_alphabet = cls._build_alphabets(symbols, blank)
|
|
33
|
+
states = cls._build_states(ast)
|
|
34
|
+
delta = cls._build_delta(ast)
|
|
35
|
+
|
|
36
|
+
return TuringMachine(states, alphabet, tape_alphabet, delta)
|
|
37
|
+
|
|
38
|
+
@classmethod
|
|
39
|
+
def _collect_symbols(cls, ast: MachineNode) -> tuple[set[Symbol], Symbol]:
|
|
40
|
+
symbols: set[Symbol] = set()
|
|
41
|
+
|
|
42
|
+
for t in ast.transitions:
|
|
43
|
+
symbols.add(Symbol(t.read))
|
|
44
|
+
symbols.add(Symbol(t.write))
|
|
45
|
+
|
|
46
|
+
if ast.blank is None:
|
|
47
|
+
raise ValueError("Blank symbol not defined")
|
|
48
|
+
|
|
49
|
+
blank = Symbol(ast.blank)
|
|
50
|
+
|
|
51
|
+
return symbols, blank
|
|
52
|
+
|
|
53
|
+
@classmethod
|
|
54
|
+
def _build_alphabets(cls, symbols: set[Symbol], blank: Symbol) -> tuple[Alphabet, TapeAlphabet]:
|
|
55
|
+
input_alphabet = Alphabet(*[s for s in symbols if s != blank])
|
|
56
|
+
tape_alphabet = TapeAlphabet(blank, *symbols)
|
|
57
|
+
|
|
58
|
+
return input_alphabet, tape_alphabet
|
|
59
|
+
|
|
60
|
+
@classmethod
|
|
61
|
+
def _build_states(cls, ast: MachineNode) -> States:
|
|
62
|
+
states = set()
|
|
63
|
+
|
|
64
|
+
for t in ast.transitions:
|
|
65
|
+
states.add(t.state)
|
|
66
|
+
states.add(t.next_state)
|
|
67
|
+
|
|
68
|
+
if ast.start is None:
|
|
69
|
+
raise ValueError("Start state not defined")
|
|
70
|
+
|
|
71
|
+
if ast.accept is None:
|
|
72
|
+
raise ValueError("Accept state not defined")
|
|
73
|
+
|
|
74
|
+
states.add(ast.start)
|
|
75
|
+
states.add(ast.accept)
|
|
76
|
+
|
|
77
|
+
return States(
|
|
78
|
+
*states,
|
|
79
|
+
initial_state=ast.start,
|
|
80
|
+
final_states={ast.accept},
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
@classmethod
|
|
84
|
+
def _build_delta(cls, ast: MachineNode):
|
|
85
|
+
transitions = []
|
|
86
|
+
|
|
87
|
+
for node in ast.transitions:
|
|
88
|
+
transitions.append(
|
|
89
|
+
Transition(
|
|
90
|
+
node.state,
|
|
91
|
+
Symbol(node.read),
|
|
92
|
+
node.next_state,
|
|
93
|
+
Symbol(node.write),
|
|
94
|
+
move_map[node.move],
|
|
95
|
+
)
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
return TransitionFunction(*transitions)
|
|
99
|
+
|
|
100
|
+
@classmethod
|
|
101
|
+
def _read_file(cls, path: str | Path) -> str:
|
|
102
|
+
|
|
103
|
+
with open(path, "r") as f:
|
|
104
|
+
return f.read()
|
|
105
|
+
|
|
106
|
+
@classmethod
|
|
107
|
+
def _lex(cls, text: str) -> list[Token]:
|
|
108
|
+
|
|
109
|
+
lexer = Lexer(text)
|
|
110
|
+
|
|
111
|
+
return lexer.tokenize()
|
|
112
|
+
|
|
113
|
+
@classmethod
|
|
114
|
+
def _parse(cls, tokens: list[Token]) -> MachineNode:
|
|
115
|
+
|
|
116
|
+
parser = Parser(tokens)
|
|
117
|
+
|
|
118
|
+
return parser.parse()
|
|
119
|
+
|
|
120
|
+
@classmethod
|
|
121
|
+
def _build_transitions(cls, ast: MachineNode) -> list[Transition]:
|
|
122
|
+
|
|
123
|
+
transitions = []
|
|
124
|
+
|
|
125
|
+
for node in ast.transitions:
|
|
126
|
+
|
|
127
|
+
transitions.append(Transition(node.state, Symbol(node.read), node.next_state, Symbol(node.write), node.move))
|
|
128
|
+
|
|
129
|
+
return transitions
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
"""
|
|
3
|
-
@author: Eric Santos <ericshantos13@gmail.com>
|
|
4
|
-
|
|
5
|
-
Loader for the Turing machine DSL.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
from pathlib import Path
|
|
9
|
-
from typing import Iterable
|
|
10
|
-
|
|
11
|
-
from ..alphabet import Symbol
|
|
12
|
-
from ..transition import Transition
|
|
13
|
-
from .ast import MachineNode
|
|
14
|
-
from .lexer import Lexer
|
|
15
|
-
from .parser import Parser
|
|
16
|
-
from .token import Token
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
class MachineLoader:
|
|
20
|
-
|
|
21
|
-
@classmethod
|
|
22
|
-
def load(cls, path: str | Path) -> Iterable[Transition]:
|
|
23
|
-
|
|
24
|
-
text = cls._read_file(path)
|
|
25
|
-
|
|
26
|
-
tokens = cls._lex(text)
|
|
27
|
-
|
|
28
|
-
ast = cls._parse(tokens)
|
|
29
|
-
|
|
30
|
-
transitions = cls._build_transitions(ast)
|
|
31
|
-
|
|
32
|
-
return transitions
|
|
33
|
-
|
|
34
|
-
@classmethod
|
|
35
|
-
def _read_file(cls, path: str | Path) -> str:
|
|
36
|
-
|
|
37
|
-
with open(path, "r") as f:
|
|
38
|
-
return f.read()
|
|
39
|
-
|
|
40
|
-
@classmethod
|
|
41
|
-
def _lex(cls, text: str) -> list[Token]:
|
|
42
|
-
|
|
43
|
-
lexer = Lexer(text)
|
|
44
|
-
|
|
45
|
-
return lexer.tokenize()
|
|
46
|
-
|
|
47
|
-
@classmethod
|
|
48
|
-
def _parse(cls, tokens: list[Token]) -> MachineNode:
|
|
49
|
-
|
|
50
|
-
parser = Parser(tokens)
|
|
51
|
-
|
|
52
|
-
return parser.parse()
|
|
53
|
-
|
|
54
|
-
@classmethod
|
|
55
|
-
def _build_transitions(cls, ast: MachineNode) -> list[Transition]:
|
|
56
|
-
|
|
57
|
-
transitions = []
|
|
58
|
-
|
|
59
|
-
for node in ast.transitions:
|
|
60
|
-
|
|
61
|
-
transitions.append(Transition(node.state, Symbol(node.read), node.next_state, Symbol(node.write), node.move))
|
|
62
|
-
|
|
63
|
-
return transitions
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|