33from syntactes import Grammar , Token
44from syntactes ._action import Action
55from syntactes ._state import LR0State
6+ from syntactes .parsing_table import Conflict , Entry
67
78Row : 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-
4111class LR0ParsingTable :
4212 """
4313 Table that contains all the transitions from state to state with a symbol.
@@ -48,6 +18,21 @@ def __init__(self, grammar: Grammar) -> None:
4818 self ._grammar = grammar
4919 self ._initial_state = None
5020
21+ @staticmethod
22+ def from_entries (
23+ entries : Iterable [Entry ], tokens : Iterable [Token ]
24+ ) -> "LR0ParsingTable" :
25+ """
26+ Create a parsing table from the given entries.
27+ """
28+ table = LR0ParsingTable (tokens )
29+ {table .add_entry (entry ) for entry in entries }
30+ return table
31+
32+ @property
33+ def initial_state (self ) -> LR0State :
34+ return self ._initial_state
35+
5136 def get_actions (self , state : LR0State , token : Token ) -> Optional [list [Action ]]:
5237 """
5338 Get the actions from state with given number with `token`.
@@ -73,26 +58,23 @@ def add_entry(self, entry: Entry) -> None:
7358 actions = row .setdefault (entry .token , list ())
7459 actions .append (entry .action )
7560
76- @staticmethod
77- def from_entries (
78- entries : Iterable [Entry ], tokens : Iterable [Token ]
79- ) -> "LR0ParsingTable" :
80- """
81- Create a parsing table from the given entries.
82- """
83- table = LR0ParsingTable (tokens )
84- {table .add_entry (entry ) for entry in entries }
85- return table
86-
8761 def pretty_str (self ) -> str :
8862 """
8963 Returns a pretty-formatted string representation of the table.
9064 """
9165 return self ._rules_pretty_str () + "\n \n " + self ._table_pretty_str ()
9266
93- @property
94- def initial_state (self ) -> LR0State :
95- return self ._initial_state
67+ def conflicts (self ) -> list [Conflict ]:
68+ """
69+ Retuns a list with all the conflicts in the parsing table.
70+ """
71+ conflicts = []
72+ for state , row in self .rows .items ():
73+ for token , actions in row .items ():
74+ if len (actions ) > 1 :
75+ conflicts .append (Conflict (state , token , actions ))
76+
77+ return conflicts
9678
9779 def _rules_pretty_str (self ) -> str :
9880 rules = [str (i ) + ". " + str (r ) for i , r in enumerate (self ._grammar .rules )]
0 commit comments