Documentation ¶
Index ¶
- Variables
- func FilterNonShiftHelper(h *Helper) bool
- func FilterTerminalLeaf(tn *tr.TreeNode[*Helper]) bool
- type ActAccept
- type ActReduce
- type ActShift
- type Actioner
- type CMPerSymbol
- type ConflictSolver
- func (cs *ConflictSolver) AppendHelper(h *Helper)
- func (cs *ConflictSolver) DeleteHelper(h *Helper)
- func (cs *ConflictSolver) FString(indentLevel int) []string
- func (cs *ConflictSolver) FindConflicts() CMPerSymbol
- func (cs *ConflictSolver) GetElemsWithLhs(rhs string) []*Helper
- func (cs *ConflictSolver) Init(symbol string) error
- func (cs *ConflictSolver) MakeExpansionForests(index int, nextRhs map[*Helper]string) (map[*Helper][]string, error)
- func (cs *ConflictSolver) Match(stack *ds.DoubleStack[gr.Tokener]) ([]Actioner, error)
- func (cs *ConflictSolver) Solve() error
- func (cs *ConflictSolver) SolveAmbiguous(index int, conflicts []*Helper) (bool, error)
- func (cs *ConflictSolver) SolveAmbiguousShifts() error
- type Err0thRhsNotSet
- type ErrAmbiguousGrammar
- type ErrCannotCreateItem
- type ErrHelper
- type ErrHelpersConflictingSize
- type ErrInvalidPosition
- type ErrItemIsNil
- type ErrNoActionProvided
- type ErrNoElementsFound
- type ExpansionTree
- type Helper
- func (h *Helper) AppendRhs(symbol string) error
- func (h *Helper) Copy() intf.Copier
- func (h *Helper) EvaluateLookahead()
- func (h *Helper) GetAction() Actioner
- func (h *Helper) GetLookahead() *string
- func (h *Helper) Init(symbol string) error
- func (h *Helper) IsShift() bool
- func (h *Helper) Match(top gr.Tokener, stack *ds.DoubleStack[gr.Tokener]) error
- func (h *Helper) ReplaceRhsAt(index int, otherH *Helper) (*Helper, error)
- func (h *Helper) SetAction(action Actioner)
- func (h *Helper) Size() int
- func (h *Helper) String() string
- type InfoStruct
- type Item
- func (i *Item) Copy() intf.Copier
- func (item *Item) GetPos() int
- func (item *Item) GetRhs() string
- func (item *Item) GetRhsAt(index int) (string, error)
- func (item *Item) GetRule() *gr.Production
- func (item *Item) GetSymbolsUpToPos() []string
- func (item *Item) IndicesOfRhs(rhs string) []int
- func (item *Item) IsLhsRhs(rhs string) bool
- func (item *Item) IsReduce() bool
- func (item *Item) ReplaceRhsAt(index int, otherI *Item) (*Item, error)
- func (i *Item) String() string
Constants ¶
This section is empty.
Variables ¶
var GlobalDebugMode bool = true
Functions ¶
func FilterNonShiftHelper ¶
FilterNonShiftHelper filters out non-shift helpers.
Parameters:
- h: The helper to filter.
Returns:
- bool: True if the helper is a shift helper, false otherwise.
Types ¶
type ActAccept ¶
type ActAccept struct { // Rule is the rule to reduce by. Rule *gr.Production // Rhs is the right-hand side tokens of the rule. Rhs []string }
ActAccept represents an accept action.
func NewAcceptAction ¶
func NewAcceptAction(rule *gr.Production) (*ActAccept, error)
NewAcceptAction creates a new accept action.
Parameters:
- rule: The rule to reduce by.
Returns:
- *ActAccept: A pointer to the new accept action.
- error: An error of type *ers.ErrInvalidParameter if the rule is nil.
func (*ActAccept) AppendRhs ¶
AppendRhs appends a right-hand side token to the accept action. It never returns an error.
Parameters:
- rhs: The right-hand side token to append.
func (*ActAccept) Copy ¶
Copy creates a copy of the accept action.
Returns:
- intf.Copier: The copy of the accept action.
func (*ActAccept) GetRule ¶
func (a *ActAccept) GetRule() *gr.Production
GetRule returns the rule to reduce by.
Returns:
- *gr.Production: The rule to reduce by.
func (*ActAccept) Match ¶
Match matches the accept action with the top of the stack.
Parameters:
- top: The top of the stack.
- stack: The stack.
Returns:
- error: An error if the action does not match the top of the stack.
type ActReduce ¶
type ActReduce struct { // Rule is the rule to reduce by. Rule *gr.Production // Rhs is the right-hand side tokens of the rule. Rhs []string }
ActReduce represents a reduce action.
func NewActReduce ¶
func NewActReduce(rule *gr.Production) (*ActReduce, error)
NewActReduce creates a new reduce action.
Parameters:
- rule: The rule to reduce by.
Returns:
- *ActReduce: A pointer to the new reduce action.
- error: An error of type *ers.ErrInvalidParameter if the rule is nil.
func (*ActReduce) AppendRhs ¶
AppendRhs appends a right-hand side token to the reduce action. It never returns an error.
Parameters:
- rhs: The right-hand side token to append.
func (*ActReduce) Copy ¶
Copy creates a copy of the reduce action.
Returns:
- intf.Copier: The copy of the reduce action.
func (*ActReduce) GetRule ¶
func (a *ActReduce) GetRule() *gr.Production
GetRule returns the rule to reduce by.
Returns:
- *gr.Production: The rule to reduce by.
func (*ActReduce) Match ¶
Match matches the reduce action with the top of the stack.
Parameters:
- top: The top of the stack.
- stack: The stack.
Returns:
- error: An error if the action does not match the top of the stack.
type ActShift ¶
type ActShift struct { // Lookahead is the lookahead token ID for the shift action. Lookahead *string // Rhs is the right-hand side tokens of the rule. Rhs []string }
ActShift represents a shift action.
func NewActShift ¶
func NewActShift() *ActShift
NewActShift creates a new shift action.
Returns:
- *ActShift: A pointer to the new shift action.
func (*ActShift) AppendRhs ¶
AppendRhs appends a right-hand side token to the shift action. It never returns an error.
Parameters:
- rhs: The right-hand side token to append.
func (*ActShift) Copy ¶
Copy creates a copy of the shift action.
Returns:
- intf.Copier: The copy of the shift action.
func (*ActShift) Match ¶
Match matches the shift action with the top of the stack.
Parameters:
- top: The top of the stack.
- stack: The stack.
Returns:
- error: An error if the action does not match the top of the stack.
func (*ActShift) SetLookahead ¶
SetLookahead sets the lookahead token ID for the shift action.
Parameters:
- lookahead: The lookahead token ID.
type Actioner ¶
type Actioner interface { // AppendRhs appends a right-hand side token to the action. // // Parameters: // - rhs: The right-hand side token to append. AppendRhs(rhs string) // Match matches the action with the top of the stack. // // Parameters: // - top: The top of the stack. // - stack: The stack. // // Returns: // - error: An error if the action does not match the top of the stack. Match(top gr.Tokener, stack *ds.DoubleStack[gr.Tokener]) error // Size returns the size of the action. // // Returns: // - int: The size of the action. Size() int fmt.Stringer intf.Copier }
Actioner represents an action that the parser will take.
type ConflictSolver ¶
type ConflictSolver struct {
// contains filtered or unexported fields
}
ConflictSolver solves conflicts in a decision table.
func NewConflictSolver ¶
func NewConflictSolver(symbols []string, rules []*gr.Production) (*ConflictSolver, error)
NewConflictSolver is a constructor of ConflictSolver.
Parameters:
- symbols: The symbols in the decision table.
- rules: The rules in the decision table.
Returns:
- *ConflictSolver: The pointer to the new ConflictSolver.
- error: An error if the operation failed.
Errors:
- *ErrCannotCreateItem: If an item cannot be created.
- *ers.ErrInvalidParameter: If the item is nil.
func SolveConflicts ¶
func SolveConflicts(symbols []string, rules []*gr.Production) (*ConflictSolver, error)
SolveConflicts solves conflicts in a decision table.
Parameters:
- symbols: The symbols in the decision table.
- rules: The rules in the decision table.
Returns:
- map[string][]*Helper: The elements in the decision table with conflicts solved.
- error: An error if the operation failed.
func (*ConflictSolver) AppendHelper ¶
func (cs *ConflictSolver) AppendHelper(h *Helper)
AppendHelper is a method that appends a helper to the decision table.
Parameters:
- h: The helper to append.
func (*ConflictSolver) DeleteHelper ¶
func (cs *ConflictSolver) DeleteHelper(h *Helper)
DeleteHelper is a method that removes an helper from the decision table.
Parameters:
- h: The helper to remove.
func (*ConflictSolver) FString ¶
func (cs *ConflictSolver) FString(indentLevel int) []string
FString returns a formatted string representation of the decision table multilined with a specific indentation level.
Parameters:
- indentLevel: The level of indentation.
Returns:
- []string: A formatted string representation of the decision table.
func (*ConflictSolver) FindConflicts ¶
func (cs *ConflictSolver) FindConflicts() CMPerSymbol
FindConflicts is a method that finds conflicts for a specific symbol.
Parameters:
- symbol: The symbol to find conflicts for.
Returns:
- CMPerSymbol: The conflicts per symbol.
func (*ConflictSolver) GetElemsWithLhs ¶
func (cs *ConflictSolver) GetElemsWithLhs(rhs string) []*Helper
GetElemsWithLhs is a method that returns all elements with a specific LHS.
Parameters:
- rhs: The RHS to find elements for.
Returns:
- []*Helper: The elements with the specified LHS.
func (*ConflictSolver) Init ¶
func (cs *ConflictSolver) Init(symbol string) error
Init is a method that initializes the elements for a specific symbol. This can be used in the ExecuteSymbols method.
Parameters:
- symbol: The symbol to initialize the elements for.
Returns:
- error: An error if the operation failed.
Errors:
- *ErrNoElementsFound: If no elements are found for the symbol.
func (*ConflictSolver) MakeExpansionForests ¶
func (cs *ConflictSolver) MakeExpansionForests(index int, nextRhs map[*Helper]string) (map[*Helper][]string, error)
MakeExpansionForests creates a forest of expansion trees rooted at the next symbol of the conflicting rules.
Parameters:
- index: The index of the conflicting rules.
- nextRhs: The next symbol of the conflicting rules.
Returns:
- map[*Helper][]*ExpansionTree: The forest of expansion trees.
- error: An error of type *ErrHelper if the operation failed.
func (*ConflictSolver) Match ¶
func (cs *ConflictSolver) Match(stack *ds.DoubleStack[gr.Tokener]) ([]Actioner, error)
func (*ConflictSolver) Solve ¶
func (cs *ConflictSolver) Solve() error
SolveConflicts is a method that solves conflicts in a decision table.
func (*ConflictSolver) SolveAmbiguous ¶
func (cs *ConflictSolver) SolveAmbiguous(index int, conflicts []*Helper) (bool, error)
func (*ConflictSolver) SolveAmbiguousShifts ¶
func (cs *ConflictSolver) SolveAmbiguousShifts() error
SolveAmbiguousShifts is a method that solves ambiguous shifts in a decision table.
Returns:
- error: An error if the operation failed.
Errors:
- *ErrHelpersConflictingSize: If the helpers have conflicting sizes.
- *ErrHelper: If there is an error appending the right-hand side to the helper.
type Err0thRhsNotSet ¶
type Err0thRhsNotSet struct{}
Err0thRhsNotSet is an error that is returned when the 0th right-hand side is not set.
func NewErr0thRhsNotSet ¶
func NewErr0thRhsNotSet() *Err0thRhsNotSet
NewErr0thRhsNotSet creates a new error of type *Err0thRhsNotSet.
Returns:
- *Err0thRhsNotSet: A pointer to the new error.
func (*Err0thRhsNotSet) Error ¶
func (e *Err0thRhsNotSet) Error() string
Error returns the error message: "0th RHS not set".
Returns:
- string: The error message.
type ErrAmbiguousGrammar ¶
type ErrAmbiguousGrammar struct{}
ErrAmbiguousGrammar is an error that is returned when a grammar is ambiguous.
func NewErrAmbiguousGrammar ¶
func NewErrAmbiguousGrammar() *ErrAmbiguousGrammar
NewErrAmbiguousGrammar creates a new error of type *ErrAmbiguousGrammar.
Returns:
- *ErrAmbiguousGrammar: A pointer to the new error.
func (*ErrAmbiguousGrammar) Error ¶
func (e *ErrAmbiguousGrammar) Error() string
Error returns the error message: "grammar is ambiguous".
Returns:
- string: The error message.
type ErrCannotCreateItem ¶
type ErrCannotCreateItem struct{}
ErrCannotCreateItem is an error that is returned when an item cannot be created.
func NewErrCannotCreateItem ¶
func NewErrCannotCreateItem() *ErrCannotCreateItem
NewErrCannotCreateItem creates a new error of type *ErrCannotCreateItem.
Returns:
- *ErrCannotCreateItem: A pointer to the new error.
func (*ErrCannotCreateItem) Error ¶
func (e *ErrCannotCreateItem) Error() string
Error returns the error message: "cannot create item".
Returns:
- string: The error message.
type ErrHelper ¶
type ErrHelper struct { // Elem is the helper that caused the error. Elem *Helper // Reason is the reason for the error. Reason error }
ErrHelper is an error that is returned when something goes wrong with a helper.
func NewErrHelper ¶
NewErrHelper creates a new error of type *ErrHelper.
Parameters:
- elem: The helper that caused the error.
- reason: The reason for the error.
Returns:
- *ErrHelper: A pointer to the new error.
type ErrHelpersConflictingSize ¶
type ErrHelpersConflictingSize struct{}
ErrHelpersConflictingSize is an error that is returned when helpers have conflicting sizes.
func NewErrHelpersConflictingSize ¶
func NewErrHelpersConflictingSize() *ErrHelpersConflictingSize
NewErrHelpersConflictingSize creates a new error of type *ErrHelpersConflictingSize.
Returns:
- *ErrHelpersConflictingSize: A pointer to the new error.
func (*ErrHelpersConflictingSize) Error ¶
func (e *ErrHelpersConflictingSize) Error() string
Error returns the error message: "helpers have conflicting sizes".
Returns:
- string: The error message.
type ErrInvalidPosition ¶
type ErrInvalidPosition struct{}
ErrInvalidPosition is an error that is returned when a position is invalid (i.e., less than 0).
func NewErrInvalidPosition ¶
func NewErrInvalidPosition() *ErrInvalidPosition
NewErrInvalidPosition creates a new error of type *ErrInvalidPosition.
Returns:
- *ErrInvalidPosition: A pointer to the new error.
func (*ErrInvalidPosition) Error ¶
func (e *ErrInvalidPosition) Error() string
Error returns the error message: "invalid position".
Returns:
- string: The error message.
type ErrItemIsNil ¶
type ErrItemIsNil struct{}
ErrItemIsNil is an error that is returned when an item is nil.
func NewErrItemIsNil ¶
func NewErrItemIsNil() *ErrItemIsNil
NewErrItemIsNil creates a new error of type *ErrItemIsNil.
Returns:
- *ErrItemIsNil: A pointer to the new error.
func (*ErrItemIsNil) Error ¶
func (e *ErrItemIsNil) Error() string
Error returns the error message: "item is nil".
Returns:
- string: The error message.
type ErrNoActionProvided ¶
type ErrNoActionProvided struct{}
ErrNoActionProvided is an error that is returned when no action is provided.
func NewErrNoActionProvided ¶
func NewErrNoActionProvided() *ErrNoActionProvided
NewErrNoActionProvided creates a new error of type *ErrNoActionProvided.
Returns:
- *ErrNoActionProvided: A pointer to the new error.
func (*ErrNoActionProvided) Error ¶
func (e *ErrNoActionProvided) Error() string
Error returns the error message: "no action provided".
Returns:
- string: The error message.
type ErrNoElementsFound ¶
type ErrNoElementsFound struct { // Symbol is the symbol for which no elements were found. Symbol string }
ErrNoElementsFound is an error that is returned when no elements are found for a symbol.
func NewErrNoElementsFound ¶
func NewErrNoElementsFound(symbol string) *ErrNoElementsFound
NewErrNoElementsFound creates a new error of type *ErrNoElementsFound.
Parameters:
- symbol: The symbol for which no elements were found.
Returns:
- *ErrNoElementsFound: A pointer to the new error.
func (*ErrNoElementsFound) Error ¶
func (e *ErrNoElementsFound) Error() string
Error returns the error message: "no elements found for symbol (symbol)".
Returns:
- string: The error message.
type ExpansionTree ¶
type ExpansionTree struct {
// contains filtered or unexported fields
}
ExpansionTree is a tree of expansion helpers.
func NewExpansionTreeRootedAt ¶
func NewExpansionTreeRootedAt(cs *ConflictSolver, h *Helper) (*ExpansionTree, error)
NewExpansionTree creates a new expansion tree where the root is h and every node is a helper whose LHS is the 0th RHS of the parent node. However, the leaves of the tree are helpers whose 0th RHS is a terminal symbol.
Parameters:
- cs: The conflict solver.
- h: The root of the expansion tree.
Returns:
- *ExpansionTree: The new expansion tree.
- error: An error if the operation failed.
Errors:
- *ers.Err0thRhsNotSet: The 0th RHS of the root is not set.
- *ers.ErrInvalidParameter: The root is nil.
func (*ExpansionTree) Collapse ¶
func (et *ExpansionTree) Collapse() []string
Collapse collapses the expansion tree into a slice of strings that are the 0th RHS of the terminal leaves.
Returns:
- []string: The collapsed expansion tree.
func (*ExpansionTree) PruneNonTerminalLeaves ¶
func (et *ExpansionTree) PruneNonTerminalLeaves()
PruneNonTerminalLeaves prunes the non-terminal leaves of the expansion tree.
func (*ExpansionTree) Size ¶
func (et *ExpansionTree) Size() int
Size returns the size of the expansion tree.
Returns:
- int: The size of the expansion tree.
type Helper ¶
type Helper struct { // Item is the item of the helper. *Item // Action is the action of the helper. Action Actioner }
Helper represents a helper in a decision table.
func NewHelper ¶
NewHelper is a constructor of Helper.
Parameters:
- item: The item of the helper.
- action: The action of the helper.
Returns:
- *Helper: The pointer to the new Helper.
- error: An error of type *ers.ErrInvalidParameter if the item is nil.
func (*Helper) AppendRhs ¶
AppendRhs appends a symbol to the right-hand side of the action.
Parameters:
- symbol: The symbol to append.
Returns:
- error: An error of type *ErrNoActionProvided if the action is nil.
func (*Helper) EvaluateLookahead ¶
func (h *Helper) EvaluateLookahead()
EvaluateLookahead evaluates the lookahead of the shift action. If the action is not a shift action, this method does nothing.
func (*Helper) GetAction ¶ added in v0.1.9
GetAction returns the action of the helper.
Returns:
- Actioner: The action of the helper.
func (*Helper) GetLookahead ¶
GetLookahead returns the lookahead of the shift action. If the action is not a shift action, this method returns nil.
Returns:
- *string: The lookahead token ID.
func (*Helper) Init ¶
Init initializes the helper with the specified symbol.
Parameters:
- symbol: The symbol to initialize the helper with.
Returns:
- error: An error of type *ers.ErrInvalidParameter if the rule is nil.
func (*Helper) IsShift ¶
IsShift returns true if the action is a shift action.
Returns:
- bool: True if the action is a shift action. Otherwise, false.
func (*Helper) Match ¶ added in v0.1.9
Match matches the top of the stack with the helper.
Parameters:
- top: The top of the stack.
- stack: The stack.
Returns:
- error: An error if the match failed.
Behaviors:
- The stack is refused.
func (*Helper) ReplaceRhsAt ¶
ReplaceRhsAt replaces the right-hand side of the item at the specified index with the right-hand side of the other item.
Parameters:
- index: The index of the right-hand side to replace.
- otherH: The other helper.
Returns:
- *Helper: The new helper with the replaced right-hand side.
- error: An error if the operation failed.
Errors:
- *ers.ErrInvalidParameter: The index is out of bounds, otherH is nil, otherH.Item is nil, or otherH.Item.Rule is nil.
- *gr.ErrLhsRhsMismatch: The left-hand side of the item does not match the right-hand side of the other item.
func (*Helper) SetAction ¶
SetAction sets the action of the helper.
Parameters:
- action: The action to set.
type InfoStruct ¶
type InfoStruct struct {
// contains filtered or unexported fields
}
InfoStruct is the information about the expansion tree.
func NewInfoStruct ¶
func NewInfoStruct(root *Helper) (*InfoStruct, error)
NewInfoStruct creates a new InfoStruct.
Parameters:
- root: The root of the expansion tree.
Returns:
- *InfoStruct: The new InfoStruct.
- error: An error of type *ers.ErrInvalidParameter if the root is nil.
Behaviors:
- The root is set to seen.
func (*InfoStruct) Copy ¶
func (is *InfoStruct) Copy() intf.Copier
Copy creates a copy of the InfoStruct.
Returns:
- intf.Copier: A copy of the InfoStruct.
type Item ¶
type Item struct { // Rule is the production rule that the item represents. Rule *gr.Production // Pos is the position of the item in the production rule. Pos int // contains filtered or unexported fields }
Item represents an item in a decision table.
func NewItem ¶
func NewItem(rule *gr.Production, pos int) (*Item, error)
NewItem is a constructor of Item.
Parameters:
- rule: The production rule that the item represents.
- pos: The position of the item in the production rule.
Returns:
- *Item: The pointer to the new Item.
- error: An error of type *ers.ErrInvalidParameter if the rule is nil or the pos is out of bounds.
func (*Item) GetPos ¶
GetPos returns the position of the item in the production rule.
Returns:
- int: The position of the item.
func (*Item) GetRhs ¶
GetRhs returns the right-hand side of the production rule at the current position.
Returns:
- string: The right-hand side of the production rule.
func (*Item) GetRhsAt ¶
GetRhsAt returns the right-hand side of the production rule at the specified index.
Parameters:
- index: The index of the right-hand side to get.
Returns:
- string: The right-hand side of the production rule.
- error: An error if it is unable to get the right-hand side.
Errors:
- *ers.ErrInvalidParameter: If the index is out of bounds or the item's rule is nil.
func (*Item) GetRule ¶
func (item *Item) GetRule() *gr.Production
GetRule returns the production rule that the item represents.
Returns:
- *gr.Production: The production rule that the item represents.
func (*Item) GetSymbolsUpToPos ¶
GetSymbolsUpToPos returns the symbols of the production rule up to the current position.
Returns:
- []string: The symbols of the production rule up to the current position.
Behaviors:
- The symbols are reversed. Thus, the symbol at index 0 is the current symbol of the item.
func (*Item) IndicesOfRhs ¶
IndicesOfRhs returns the indices of the right-hand side of the item that match the specified right-hand side.
Parameters:
- rhs: The right-hand side to search for.
Returns:
- []int: The indices of the right-hand side.
func (*Item) IsLhsRhs ¶
IsLhsRhs returns true if the left-hand side of the production rule matches the right-hand side.
Parameters:
- rhs: The right-hand side to compare with the left-hand side.
Returns:
- bool: True if the left-hand side matches the right-hand side. Otherwise, false.
func (*Item) IsReduce ¶
IsReduce returns true if the item is a reduce item.
Returns:
- bool: True if the item is a reduce item. Otherwise, false.
Behaviors:
- If the item's rule is nil, it returns false.
func (*Item) ReplaceRhsAt ¶
ReplaceRhsAt replaces the right-hand side of the production rule at the given index with the right-hand side of the other item.
Parameters:
- index: The index of the right-hand side to replace.
- otherI: The other item to replace the right-hand side with.
Returns:
- *Item: The new item with the replaced right-hand side.
- error: An error if it is unable to replace the right-hand side.
Errors:
- *ers.ErrInvalidParameter: If the other item is nil, otherI.Rule is nil, or the index is out of bounds.
- *gr.ErrLhsRhsMismatch: If the left-hand side of the production rule does not match the right-hand side.