Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
GreedyParser
Description
This module contains a simple greedy parser for path grammars.
The grammar is provided by an evaluator (Eval
).
In addition, the parser takes a policy function
that picks a reduction option in each step.
Synopsis
- data Trans tr = Trans {}
- data GreedyState tr tr' slc op
- showFrozen :: Show slc => Path tr' slc -> String
- showOpen :: Show slc => Path tr slc -> String
- data ActionSingle slc tr s f = ActionSingle (StartStop slc, Trans tr, StartStop slc) (LeftmostSingle s f)
- data ActionDouble slc tr s f h = ActionDouble (StartStop slc, Trans tr, slc, Trans tr, StartStop slc) (LeftmostDouble s f h)
- type Action slc tr s f h = Either (ActionSingle slc tr s f) (ActionDouble slc tr s f h)
- parseGreedy :: forall m tr tr' slc slc' s f h. (Monad m, MonadIO m, Show tr', Show slc, Show tr, Show s, Show f, Show h) => Eval tr tr' slc slc' (Leftmost s f h) -> ([Action slc tr s f h] -> ExceptT String m (Action slc tr s f h)) -> Path slc' tr' -> ExceptT String m (Analysis s f h tr slc)
- pickRandom :: StatefulGen g m => g -> [slc] -> ExceptT String m slc
- parseRandom :: (Show tr', Show slc, Show tr, Show s, Show f, Show h) => Eval tr tr' slc slc' (Leftmost s f h) -> Path slc' tr' -> ExceptT String IO (Analysis s f h tr slc)
- parseRandom' :: (Show tr', Show slc, Show tr, Show s, Show f, Show h, StatefulGen g IO) => g -> Eval tr tr' slc slc' (Leftmost s f h) -> Path slc' tr' -> ExceptT String IO (Analysis s f h tr slc)
Parsing State
A transition during greedy parsing. Augments transition data with a flag that indicates whether the transition is a transitive right (2nd) parent of a spread.
Constructors
Trans | |
data GreedyState tr tr' slc op Source #
The state of the greedy parse between steps. Generally, the current reduction consists of frozen transitions between the ⋊ and the current location and open transitions between the current location and ⋉.
⋊==[1]==[2]==[3]——[4]——[5]——⋉ └ frozen ┘ | └ open ┘ midSlice (current position) frozen: ==[2]==[1]== midSlice: [3] open: ——[4]——[5]——
This is the GSSemiOpen
case:
The slice at the current pointer ([3]
)
is represented as an individual slice (midSlice
).
The frozen part is represented by a Path
of frozen transitions (tr'
) and slices (slc
).
in reverse direction, i.e. from midslice
back to ⋊ (excluding ⋊).
The open part is a Path
of open transitions (tr
) and slices (slc
)
in forward direction from midSlice
up to ⋉.
There are two special cases.
All transitions can be frozen (GSFrozen
),
in which case state only contains the backward Path
of frozen transitions
(excluding ⋊ and ⋉):
⋊==[1]==[2]==[3]==⋉ └ current position represented as: ==[3]==[2]==[1]==
Or all transitions can be open (GSOpen
),
in which case the state is just the forward path of open transitions:
⋊——[1]——[2]——[3]——⋉ └ current position represented as: ——[1]——[2]——[3]——
The open and semiopen case additionally have a list of operations in generative order.
Constructors
GSFrozen !(Path (Maybe tr') slc) | |
GSSemiOpen | |
Fields
| |
GSOpen !(Path (Trans tr) slc) ![op] |
Instances
(Show slc, Show o) => Show (GreedyState tr tr' slc o) Source # | |
Defined in GreedyParser Methods showsPrec :: Int -> GreedyState tr tr' slc o -> ShowS # show :: GreedyState tr tr' slc o -> String # showList :: [GreedyState tr tr' slc o] -> ShowS # |
showFrozen :: Show slc => Path tr' slc -> String Source #
Helper function for showing the frozen part of a piece.
showOpen :: Show slc => Path tr slc -> String Source #
Helper function for showing the open part of a piece.
Parsing Actions
data ActionSingle slc tr s f Source #
A parsing action (reduction step) with a single parent transition. Combines the parent elements with a single-transition derivation operation.
Constructors
ActionSingle | |
Fields
|
Instances
(Show slc, Show tr, Show s, Show f) => Show (ActionSingle slc tr s f) Source # | |
Defined in GreedyParser Methods showsPrec :: Int -> ActionSingle slc tr s f -> ShowS # show :: ActionSingle slc tr s f -> String # showList :: [ActionSingle slc tr s f] -> ShowS # |
data ActionDouble slc tr s f h Source #
A parsing action (reduction step) with two parent transitions. Combines the parent elements with a double-transition derivation operation.
Constructors
ActionDouble | |
Fields
|
Instances
(Show slc, Show tr, Show s, Show f, Show h) => Show (ActionDouble slc tr s f h) Source # | |
Defined in GreedyParser Methods showsPrec :: Int -> ActionDouble slc tr s f h -> ShowS # show :: ActionDouble slc tr s f h -> String # showList :: [ActionDouble slc tr s f h] -> ShowS # |
type Action slc tr s f h = Either (ActionSingle slc tr s f) (ActionDouble slc tr s f h) Source #
An alias that combines ActionSingle
and ActionDouble
, representing all possible reduction steps.
Parsing Algorithm
Arguments
:: forall m tr tr' slc slc' s f h. (Monad m, MonadIO m, Show tr', Show slc, Show tr, Show s, Show f, Show h) | |
=> Eval tr tr' slc slc' (Leftmost s f h) | the evaluator of the grammar to be used |
-> ([Action slc tr s f h] -> ExceptT String m (Action slc tr s f h)) | the policy: picks a parsing action from a list of options
(determines the |
-> Path slc' tr' | the input piece |
-> ExceptT String m (Analysis s f h tr slc) | the full parse or an error message |
Parse a piece in a greedy fashion.
At each step, a policy chooses from the possible reduction actions,
the reduction is applied, and parsing continues
until the piece is fully reduced or no more reduction operations are available.
Returns the full derivation from the top (⋊——⋉
) or an error message.
pickRandom :: StatefulGen g m => g -> [slc] -> ExceptT String m slc Source #
A policy that picks the next action at random.
Must be partially applied with a random generator before passing to parseGreedy
.
Entry Points
Arguments
:: (Show tr', Show slc, Show tr, Show s, Show f, Show h) | |
=> Eval tr tr' slc slc' (Leftmost s f h) | the grammar's evaluator |
-> Path slc' tr' | the input piece |
-> ExceptT String IO (Analysis s f h tr slc) | a random reduction of the piece (or an error message) |
Parse a piece randomly using a fresh random number generator.
Arguments
:: (Show tr', Show slc, Show tr, Show s, Show f, Show h, StatefulGen g IO) | |
=> g | a random number generator |
-> Eval tr tr' slc slc' (Leftmost s f h) | the grammar's evaluator |
-> Path slc' tr' | the input piece |
-> ExceptT String IO (Analysis s f h tr slc) | a random reduction of the piece (or an error message) |
Parse a piece randomly using an existing random number generator.