Skip to content

Commit 8bc83b2

Browse files
committed
Creates parsing table dir and restructures
1 parent 57684b2 commit 8bc83b2

File tree

8 files changed

+51
-37
lines changed

8 files changed

+51
-37
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
clean:
2-
rm -rf src/syntactes/__pycache__ src/syntactes/tests/__pycache__ src/syntactes/parser/__pycache__
2+
rm -rf src/syntactes/__pycache__ src/syntactes/tests/__pycache__ src/syntactes/parser/__pycache__ src/syntactes/parsing_table/__pycache__
33
rm -rf dist src/syntactes.egg-info
44

55
test:

src/syntactes/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22
from .rule import Rule
33
from .grammar import Grammar
44
from .generator import LR0Generator, SLRGenerator
5-
from .table import LR0ParsingTable, SLRParsingTable

src/syntactes/generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from syntactes import Grammar, Token
2-
from syntactes._action import Action, ActionType
2+
from syntactes._action import Action
33
from syntactes._item import LR0Item
44
from syntactes._state import LR0State
5-
from syntactes.table import Entry, LR0ParsingTable, SLRParsingTable
5+
from syntactes.parsing_table import Entry, LR0ParsingTable, SLRParsingTable
66

77

88
class LR0Generator:

src/syntactes/parser/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
ParserError,
1111
UnexpectedTokenError,
1212
)
13-
from syntactes.table import LR0ParsingTable
13+
from syntactes.parsing_table import LR0ParsingTable
1414

1515

1616
class LR0Parser:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .entry import Entry
2+
from .table import LR0ParsingTable, SLRParsingTable
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from typing import TypeAlias
2+
3+
from syntactes import Token
4+
from syntactes._action import Action
5+
from syntactes._state import LR0State
6+
7+
Row: TypeAlias = dict[Token, list[Action]]
8+
9+
10+
class Entry:
11+
"""
12+
An entry of the parsing table. Holds the information of a transition from
13+
a state to another state via a symbol.
14+
"""
15+
16+
def __init__(self, from_state: LR0State, token: Token, action: Action) -> None:
17+
self.from_state = from_state
18+
self.token = token
19+
self.action = action
20+
21+
def __repr__(self) -> str:
22+
return f"<Entry: {str(self)}>"
23+
24+
def __str__(self) -> str:
25+
return f"{self.from_state.number}, {self.action}, {self.token}"
26+
27+
def __hash__(self) -> int:
28+
return hash((self.from_state, self.token, self.action))
29+
30+
def __eq__(self, other) -> bool:
31+
if not isinstance(other, Entry):
32+
return False
33+
34+
return (
35+
self.from_state == other.from_state
36+
and self.token == other.token
37+
and self.action == other.action
38+
)
Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,11 @@
33
from syntactes import Grammar, Token
44
from syntactes._action import Action
55
from syntactes._state import LR0State
6+
from syntactes.parsing_table import Entry
67

78
Row: TypeAlias = dict[Token, list[Action]]
89

910

10-
class Entry:
11-
"""
12-
An entry of the parsing table. Holds the information of a transition from
13-
a state to another state via a symbol.
14-
"""
15-
16-
def __init__(self, from_state: LR0State, token: Token, action: Action) -> None:
17-
self.from_state = from_state
18-
self.token = token
19-
self.action = action
20-
21-
def __repr__(self) -> str:
22-
return f"<Entry: {str(self)}>"
23-
24-
def __str__(self) -> str:
25-
return f"{self.from_state.number}, {self.action}, {self.token}"
26-
27-
def __hash__(self) -> int:
28-
return hash((self.from_state, self.token, self.action))
29-
30-
def __eq__(self, other) -> bool:
31-
if not isinstance(other, Entry):
32-
return False
33-
34-
return (
35-
self.from_state == other.from_state
36-
and self.token == other.token
37-
and self.action == other.action
38-
)
39-
40-
4111
class LR0ParsingTable:
4212
"""
4313
Table that contains all the transitions from state to state with a symbol.
@@ -94,6 +64,11 @@ def pretty_str(self) -> str:
9464
"""
9565
return self._rules_pretty_str() + "\n\n" + self._table_pretty_str()
9666

67+
def conflicts(self) -> list:
68+
"""
69+
Retuns a list with all the conflicts in the parsing table.
70+
"""
71+
9772
def _rules_pretty_str(self) -> str:
9873
rules = [str(i) + ". " + str(r) for i, r in enumerate(self._grammar.rules)]
9974
rules_str = "\n".join(rules)

src/syntactes/tests/data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from syntactes._action import Action
33
from syntactes._item import LR0Item
44
from syntactes._state import LR0State
5-
from syntactes.table import Entry, LR0ParsingTable, SLRParsingTable
5+
from syntactes.parsing_table import Entry, LR0ParsingTable, SLRParsingTable
66

77
EOF = Token.eof()
88
S = Token("S", False)

0 commit comments

Comments
 (0)