11from typing import Iterable , Optional , TypeAlias
22
33from syntactes ._action import Action
4- from syntactes ._state import State
4+ from syntactes ._state import LR0State
55from syntactes .grammar import Grammar
66from syntactes .token import Token
77
@@ -14,7 +14,7 @@ class Entry:
1414 a state to another state via a symbol.
1515 """
1616
17- def __init__ (self , from_state : State , token : Token , action : Action ) -> None :
17+ def __init__ (self , from_state : LR0State , token : Token , action : Action ) -> None :
1818 self .from_state = from_state
1919 self .token = token
2020 self .action = action
@@ -39,23 +39,23 @@ def __eq__(self, other) -> bool:
3939 )
4040
4141
42- class ParsingTable :
42+ class LR0ParsingTable :
4343 """
4444 Table that contains all the transitions from state to state with a symbol.
4545 """
4646
4747 def __init__ (self , grammar : Grammar ) -> None :
48- self .rows : dict [State , Row ] = dict ()
48+ self .rows : dict [LR0State , Row ] = dict ()
4949 self ._grammar = grammar
5050
51- def get_actions (self , state : State , token : Token ) -> Optional [list [Action ]]:
51+ def get_actions (self , state : LR0State , token : Token ) -> Optional [list [Action ]]:
5252 """
5353 Get the actions from state with given number with `token`.
5454 If there are no actions, returns None.
5555 """
5656 return self .rows .get (state , {}).get (token , None )
5757
58- def get (self , state : State ) -> Optional [Row ]:
58+ def get (self , state : LR0State ) -> Optional [Row ]:
5959 """
6060 Get the mapping of tokens to actions for the given state number.
6161 Returns None if the state is not found.
@@ -73,11 +73,11 @@ def add_entry(self, entry: Entry) -> None:
7373 @staticmethod
7474 def from_entries (
7575 entries : Iterable [Entry ], tokens : Iterable [Token ]
76- ) -> "ParsingTable " :
76+ ) -> "LR0ParsingTable " :
7777 """
7878 Create a parsing table from the given entries.
7979 """
80- table = ParsingTable (tokens )
80+ table = LR0ParsingTable (tokens )
8181 {table .add_entry (entry ) for entry in entries }
8282 return table
8383
@@ -128,3 +128,19 @@ def _table_pretty_str(self) -> str:
128128
129129 def _header_str (self ) -> str :
130130 return "LR0 PARSING TABLE"
131+
132+
133+ class SLRParsingTable (LR0ParsingTable ):
134+ @staticmethod
135+ def from_entries (
136+ entries : Iterable [Entry ], tokens : Iterable [Token ]
137+ ) -> "SLRParsingTable" :
138+ """
139+ Create a parsing table from the given entries.
140+ """
141+ table = SLRParsingTable (tokens )
142+ {table .add_entry (entry ) for entry in entries }
143+ return table
144+
145+ def _header_str (self ) -> str :
146+ return "SLR PARSING TABLE"
0 commit comments