distributors-grammar
Copyright(C) 2026 - Eitan Chatav
LicenseBSD-style (see the file LICENSE)
MaintainerEitan Chatav <eitan.chatav@gmail.com>
Stabilityprovisional
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Control.Lens.Grammar.Analysis

Description

Static analyses of RegExtended Bnfs, as generated from context-free grammars by Control.Lens.Grammar.regbnfG, over any token.

A RegEx is the free KleeneStarAlgebra of token classes & nonterminals, so an analysis which composes, like nullability or the first tokens of a word, is the unique homomorphism out of it, foldRegEx, into another KleeneStarAlgebra, with a least fixpoint, fixRules, for the rules naming each other. An analysis which depends on context, like the tokens which may follow a rule, flows down from its callers, in follows. The LL(1) conflicts, ll1Conflicts, are built of them.

Synopsis

Folding

foldRegEx Source #

Arguments

:: KleeneStarAlgebra k 
=> (TokenClass token -> k)

token classes

-> (String -> k)

nonterminals

-> RegEx token 
-> k 

The homomorphism out of the free KleeneStarAlgebra of token classes & nonterminals, a RegEx, into any other, given the interpretations of its token classes & its nonterminals.

prop> foldRegEx tokenClass nonTerminal = id

bnfRules :: Categorized token => Bnf (RegEx token) -> Map String (RegEx token) Source #

The rules of a Bnf, by name, a name's rules joined as alternatives.

fixRules Source #

Arguments

:: (Categorized token, Eq k) 
=> k

bottom

-> (Map String k -> RegEx token -> k)

a body, given the rules

-> Bnf (RegEx token) 
-> Map String k 

The least fixpoint of an analysis of the rules of a Bnf, from bottom, interpreting each rule's body given the analyses of the rules it names, until they stabilize, as they do for a monotone analysis into a lattice of finite height.

Leading atoms

data Leading a Source #

Whether a word can be empty, & the atoms it can lead with, a KleeneStarAlgebra which, at token classes, is the first tokens of words, & at sets of rule names, their left corners.

>>> foldRegEx (Leading False) (const falseP) ("a*b|c?" :: RegEx Char)
Leading {leadingNullable = True, leadingAtoms = TokenClass (OneOf (fromList "abc"))}

Constructors

Leading 

Fields

Instances

Instances details
DisjunctiveMonoid a => DisjunctiveMonoid (Leading a) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Methods

falseP :: Leading a #

(>|<) :: Leading a -> Leading a -> Leading a #

DisjunctiveMonoid a => KleeneStarAlgebra (Leading a) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Methods

starK :: Leading a -> Leading a #

plusK :: Leading a -> Leading a #

optK :: Leading a -> Leading a #

quantK :: Int -> Maybe Int -> Leading a -> Leading a #

DisjunctiveMonoid a => Monoid (Leading a) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Methods

mempty :: Leading a #

mappend :: Leading a -> Leading a -> Leading a #

mconcat :: [Leading a] -> Leading a #

DisjunctiveMonoid a => Semigroup (Leading a) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Methods

(<>) :: Leading a -> Leading a -> Leading a #

sconcat :: NonEmpty (Leading a) -> Leading a #

stimes :: Integral b => b -> Leading a -> Leading a #

Read a => Read (Leading a) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Show a => Show (Leading a) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Methods

showsPrec :: Int -> Leading a -> ShowS #

show :: Leading a -> String #

showList :: [Leading a] -> ShowS #

Eq a => Eq (Leading a) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Methods

(==) :: Leading a -> Leading a -> Bool #

(/=) :: Leading a -> Leading a -> Bool #

Ord a => Ord (Leading a) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Methods

compare :: Leading a -> Leading a -> Ordering #

(<) :: Leading a -> Leading a -> Bool #

(<=) :: Leading a -> Leading a -> Bool #

(>) :: Leading a -> Leading a -> Bool #

(>=) :: Leading a -> Leading a -> Bool #

max :: Leading a -> Leading a -> Leading a #

min :: Leading a -> Leading a -> Leading a #

firsts :: Categorized token => Bnf (RegEx token) -> Map String (Leading (TokenClass token)) Source #

The nullability & first tokens of each rule of a Bnf.

firstOf :: Categorized token => Map String (Leading (TokenClass token)) -> RegEx token -> Leading (TokenClass token) Source #

The nullability & first tokens of a RegEx, given those of the rules it names.

leftRecursive :: Categorized token => Bnf (RegEx token) -> Set String Source #

The rules of a Bnf which can name themselves before reading a token, on which a predictive parser diverges.

Guards

data Guard token Source #

A guard on the next token, or the end of the input, a BooleanAlgebra, by which a predictive parser decides. The tokens which may follow a rule are a Guard.

Constructors

Guard 

Fields

Instances

Instances details
Categorized token => BooleanAlgebra (Guard token) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Methods

(>&<) :: Guard token -> Guard token -> Guard token #

notP :: Guard token -> Guard token #

trueP :: Guard token #

Categorized token => DisjunctiveMonoid (Guard token) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Methods

falseP :: Guard token #

(>|<) :: Guard token -> Guard token -> Guard token #

(Categorized token, Show token, Show (Categorize token)) => Show (Guard token) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Methods

showsPrec :: Int -> Guard token -> ShowS #

show :: Guard token -> String #

showList :: [Guard token] -> ShowS #

Categorized token => Eq (Guard token) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Methods

(==) :: Guard token -> Guard token -> Bool #

(/=) :: Guard token -> Guard token -> Bool #

Categorized token => Ord (Guard token) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Methods

compare :: Guard token -> Guard token -> Ordering #

(<) :: Guard token -> Guard token -> Bool #

(<=) :: Guard token -> Guard token -> Bool #

(>) :: Guard token -> Guard token -> Bool #

(>=) :: Guard token -> Guard token -> Bool #

max :: Guard token -> Guard token -> Guard token #

min :: Guard token -> Guard token -> Guard token #

follows :: Categorized token => Bnf (RegEx token) -> Map String (Guard token) Source #

The tokens which may follow each rule of a Bnf, & whether its input may end, the least fixpoint of their callers'.

>>> follows ("{start} = \\q{a}b|\\q{a}\n{a} = c" :: Bnf (RegEx Char))
fromList [("a",Guard {guardEnd = True, guardNext = TokenClass (OneOf (fromList "b"))})]

LL(1)

ll1Conflicts :: Categorized token => Bnf (RegEx token) -> [LL1Conflict token] Source #

The conflicts which keep a RegExtended Bnf from being LL(1), that is, from being parsed predictively by one token of lookahead, committing to an alternative by its first token, as tParsector does. An empty list means the grammar is LL(1).

Alternatives are tried in order, so besides the usual conditions, that alternatives start with distinct tokens & that repetitions & options start with tokens which can't follow them, a nullable alternative mustn't precede one which can consume tokens, since it would succeed first, without consuming them. Left recursion is reported too, since a predictive parser diverges on it. A conflict which a try resolves isn't reported: of alternatives, the first of which backtracks, or of a repetition which backtracks, since a predictive parser then tries what follows anew.

>>> map conflictSort (ll1Conflicts ("{start} = ab|ac" :: Bnf (RegEx Char)))
[LL1FirstFirst]
>>> ll1Conflicts ("{start} = ab|c" :: Bnf (RegEx Char))
[]

data LL1Conflict token Source #

A conflict keeping a grammar from being LL(1), in a rule, or Nothing for the start expression, of a sort, with the tokens in conflict, at a subexpression.

Instances

Instances details
(Categorized token, Show token, Show (Categorize token)) => Show (LL1Conflict token) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Methods

showsPrec :: Int -> LL1Conflict token -> ShowS #

show :: LL1Conflict token -> String #

showList :: [LL1Conflict token] -> ShowS #

Categorized token => Eq (LL1Conflict token) Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Methods

(==) :: LL1Conflict token -> LL1Conflict token -> Bool #

(/=) :: LL1Conflict token -> LL1Conflict token -> Bool #

data LL1ConflictSort Source #

Sorts of LL1Conflicts.

Constructors

LL1FirstFirst

alternatives which start with the same tokens

LL1FirstFollow

an option, repetition or nullable alternative which starts with tokens which may follow it

LL1NullableAlternatives

alternatives which both match the empty word

LL1NullableFirst

a nullable alternative before one which consumes tokens

LL1NullableRepetition

an option or repetition of a pattern which matches the empty word

LL1LeftRecursion

a rule which names itself before reading a token

Instances

Instances details
Bounded LL1ConflictSort Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Enum LL1ConflictSort Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Read LL1ConflictSort Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Show LL1ConflictSort Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Eq LL1ConflictSort Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis

Ord LL1ConflictSort Source # 
Instance details

Defined in Control.Lens.Grammar.Analysis