Averest


Grammars Module

This module

Types

Type Description

ActionSLR1

data type for the actions of LR(0) and SLR(1) parsers

Grammar

Rule

StackSymbol

symbols on the stack of a SLR(1) parser

Symbol

data types for nonterminals and terminals

Word

Functions and values

Function or value Description

ActionsLR0 (states, transRel)

Full Usage: ActionsLR0 (states, transRel)

Parameters:
    states : Set<'a * Set<'b * 'c * Word>>
    transRel : 'd

Returns: ('a * Set<'b * 'c * Word> * Set<'b * 'c * Symbol[]>)[]

get actions of a LR(0) parser in the states of its LR(0) automaton

states : Set<'a * Set<'b * 'c * Word>>
transRel : 'd
Returns: ('a * Set<'b * 'c * Word> * Set<'b * 'c * Symbol[]>)[]

ChomskyNF g1

Full Usage: ChomskyNF g1

Parameters:
Returns: (Symbol * Symbol[][])[]

compute a Chomsky normal form, i.e., all production rules are only of the form A -> BC, A -> a and possibly S -> ϵ (if ϵ belongs to the language where S is the start symbol)

g1 : Grammar
Returns: (Symbol * Symbol[][])[]

CleanUp g

Full Usage: CleanUp g

Parameters:
Returns: (Symbol * Symbol[][])[]

This function performs some basic clean-up of a given grammar.

g : Grammar
Returns: (Symbol * Symbol[][])[]

CockeYoungerKasami g w

Full Usage: CockeYoungerKasami g w

Parameters:
Returns: bool * Map<(int * int), Set<Symbol>>

Cocke-Younger-Kasami algorithm for checking which nonterminals of a grammar in Chomsky normal form can produce a given word. The function returns a parse table that maps integers (i,l) to sets of nonterminals such that those nonterminals can produce the subword starting at index i of length l.

g : Grammar
w : Word
Returns: bool * Map<(int * int), Set<Symbol>>

DerivableNTs nullable g

Full Usage: DerivableNTs nullable g

Parameters:
Returns: Map<Symbol, Set<Symbol>>

This function computes the set of nonterminal symbols of a grammar that can derive other nonterminal symbols via derivations with more than one production step. If one of the nonterminals can produce itself, then the algorithm for elimination of left recursion may end up in an infinite loop, and requires basic cleanup steps in advance. In all other cases, the algorithm for elimination of left recursion will terminate.

nullable : Set<Symbol>
g : Grammar
Returns: Map<Symbol, Set<Symbol>>

ElimLeftFactor g1

Full Usage: ElimLeftFactor g1

Parameters:
Returns: (Symbol * Symbol[][])[]

Elimination of left factors of grammar rules, i.e., all right hand sides of the production rules of a nonterminal start with different prefixes.

g1 : Grammar
Returns: (Symbol * Symbol[][])[]

ElimLeftRecursion g1

Full Usage: ElimLeftRecursion g1

Parameters:
Returns: (Symbol * Symbol[][])[]

Elimination of left recursion of grammar rules (note that this algorithm may not terminate in case that there is a nonterminal A that can derive itself via more than one production step): For the resulting grammar, the right hand sides of production rules of nonterminal A may only start with a nonterminal if that nonterminal is greater than A (where greater refers to the ordering given in the grammar).

g1 : Grammar
Returns: (Symbol * Symbol[][])[]

FirstSetEquations nullable g

Full Usage: FirstSetEquations nullable g

Parameters:
Returns: Map<Symbol, Set<string>>

This function computes an equation system that defines the First sets as a least fixpoint. It is directly read from the grammar using the nullable nonterminals. The function returns a map that maps each nonterminal to a set of strings denoting sets whose union is the First set.

nullable : Set<Symbol>
g : Grammar
Returns: Map<Symbol, Set<string>>

FirstSetOfWord firstSets w

Full Usage: FirstSetOfWord firstSets w

Parameters:
Returns: Set<string>

compute the First sets of words with respect to the First sets computed for the single symbols of a given grammar

firstSets : Map<Symbol, Set<string>>
w : Word
Returns: Set<string>

FirstSets g

Full Usage: FirstSets g

Parameters:
Returns: Map<Symbol, Set<string>> * Map<Symbol, Set<string> list>

Compute the first sets of all terminals and nonterminals; i.e., the set of terminal symbols that are the leftmost symbols of words that can be derived from the considered symbols (and "" if the empty word can be derived). The result is a pair of maps (firstSets,firstSetsTable) where firstSets maps terminal and nonterminal symbols to a set of strings that are the first sets of those symbols. The second map firstSetsTable shows how the First sets have been computed by a fixpoint iteration.

g : Grammar
Returns: Map<Symbol, Set<string>> * Map<Symbol, Set<string> list>

FollowSetEquations firstSets g

Full Usage: FollowSetEquations firstSets g

Parameters:
Returns: Map<Symbol, (Set<string> * Set<string> * Set<string>)>

This function computes an equation system that defines the Follow sets as a least fixpoint. It is directly read from the grammar using the nullable nonterminals. The function returns a map that maps each nonterminal to a set of strings denoting sets whose union is the Follow set.

firstSets : Map<Symbol, Set<string>>
g : Grammar
Returns: Map<Symbol, (Set<string> * Set<string> * Set<string>)>

FollowSets firstSets g

Full Usage: FollowSets firstSets g

Parameters:
Returns: Map<Symbol, Set<string>> * Map<Symbol, Set<string> list>

compute for a given grammar g and its First sets the Follow sets of the nonterminal symbols

firstSets : Map<Symbol, Set<string>>
g : Grammar
Returns: Map<Symbol, Set<string>> * Map<Symbol, Set<string> list>

GrammarOfPDA (states, sigmaPD, sigmaIN, initState, transRel)

Full Usage: GrammarOfPDA (states, sigmaPD, sigmaIN, initState, transRel)

Parameters:
    states : string[]
    sigmaPD : string[]
    sigmaIN : string[]
    initState : string
    transRel : seq<string * string * string * string * string>

Returns: (Set<Symbol> * Set<Symbol> * Symbol * Grammar) * (Set<Symbol> * Set<Symbol> * Symbol * (Symbol * Symbol[][])[]) * (Set<Symbol> * Set<Symbol> * Symbol * (Symbol * Symbol[][])[]) option

generate a grammar for a given PDA according to the standard algorithm; the function returns three grammars (gr1,gr2,gr3) where gr1 is the direct result of the standard algorithm, gr2 is its basic clean-up by removing unreachable and useless symbols, and in grammar gr3, the nonterminals were renamed to A,B,C,.. if possible (therefore it is an option type).

states : string[]
sigmaPD : string[]
sigmaIN : string[]
initState : string
transRel : seq<string * string * string * string * string>
Returns: (Set<Symbol> * Set<Symbol> * Symbol * Grammar) * (Set<Symbol> * Set<Symbol> * Symbol * (Symbol * Symbol[][])[]) * (Set<Symbol> * Set<Symbol> * Symbol * (Symbol * Symbol[][])[]) option

GrammarTool qscoll

Full Usage: GrammarTool qscoll

Parameters:

function to be called by the cgi script

qscoll : NameValueCollection

GreibachNF g1

Full Usage: GreibachNF g1

Parameters:
Returns: (Symbol * Symbol[][])[]

Greibach normal form demands that all right hand sides of production rules start with a terminal symbol. The following function assumes that the given grammar has no left recursion so that all right hand sides of production rules of a nonterminal start may only start with nonterminals greater than that nonterminal. This algorithm rewrites also these occurrences so that all right hand sides will start with terminal symbols only.

g1 : Grammar
Returns: (Symbol * Symbol[][])[]

LR0Automaton g

Full Usage: LR0Automaton g

Parameters:
Returns: (Symbol * Symbol[][])[] * Set<int * Set<Symbol * Word * Word>> * Map<(int * Symbol), int>

compute the LR(0) automaton for LR(0) or SLR(1) parsing

g : Grammar
Returns: (Symbol * Symbol[][])[] * Set<int * Set<Symbol * Word * Word>> * Map<(int * Symbol), int>

LR0Automaton2Dot ostr (states, transRel)

Full Usage: LR0Automaton2Dot ostr (states, transRel)

Parameters:

write an LR0 automaton in dot format to an output stream

ostr : TextWriter
states : Set<int * Set<Symbol * Symbol[] * Symbol[]>>
transRel : Map<(int * Symbol), int>

MkActionTableSLR1 followSets (g, states, transRel)

Full Usage: MkActionTableSLR1 followSets (g, states, transRel)

Parameters:
Returns: Map<('a * Symbol), Set<ActionSLR1>>

constructs the action and goto table for a LR0 parser whose LR(0) automaton has the given states and transitions

followSets : Map<Symbol, Set<string>>
g : Grammar
states : seq<'a * Set<Symbol * Word * Symbol[]>>
transRel : Map<('a * Symbol), int>
Returns: Map<('a * Symbol), Set<ActionSLR1>>

MkParseTable firstSets followSets g

Full Usage: MkParseTable firstSets followSets g

Parameters:
Returns: Map<(Symbol * Symbol), Set<Symbol * Word>>

compute the parse table for a LL(1) grammar

firstSets : Map<Symbol, Set<string>>
followSets : Map<Symbol, 'a>
g : Grammar
Returns: Map<(Symbol * Symbol), Set<Symbol * Word>>

NonterminalsOfGrammar g

Full Usage: NonterminalsOfGrammar g

Parameters:
Returns: Set<Symbol>

NonterminalsOfGrammar computes for a given grammar the set of terminals.

g : Grammar
Returns: Set<Symbol>

NullableSymbols g

Full Usage: NullableSymbols g

Parameters:
Returns: Set<Symbol>

Compute the nonterminal symbols of a grammar that can produce the empty word (this information can alternatively be computed by the First sets).

g : Grammar
Returns: Set<Symbol>

ParseGrammar s

Full Usage: ParseGrammar s

Parameters:
    s : string

Returns: Grammar

Parsing a grammar: captial letters are nonterminals, while all other letters are interpreted as terminal symbols. Rules must be given in the form A -> w1 | w2 | ... | wn where in each line contains the rules of one nonterminal symbol

s : string
Returns: Grammar

ParsePDA s

Full Usage: ParsePDA s

Parameters:
    s : string

Returns: string[] * string[] * string[] * string * (string * string * string * string * string) list

parse a pushdown automaton which should looks, for example, as follows: states: q0,q1,q2 sigmaPD: $,a,b sigmaIN: a,b transitions: ($,q0,ϵ,ϵ,q0); ($,q0,a,$a,q0); (a,q0,a,aa,q0); (a,q0,b,ϵ,q1); ($,q1,ϵ,ϵ,q1); (a,q1,b,ϵ,q1);

s : string
Returns: string[] * string[] * string[] * string * (string * string * string * string * string) list

ParseTreeOfSLR12Dot ostr (nodes, edges)

Full Usage: ParseTreeOfSLR12Dot ostr (nodes, edges)

Parameters:

write a parse tree as generated by ParseTreeOfSLR1Parse to dot format

ostr : TextWriter
nodes : Set<int * Symbol>
edges : Set<int * int>

ParseTreeOfSLR1Parse pL

Full Usage: ParseTreeOfSLR1Parse pL

Parameters:
Returns: Set<int * Symbol> * Set<int * int>

generate a parse tree of a parse run generated by function PrintSLR1Parse

pL : ('a * 'b * ActionSLR1) list
Returns: Set<int * Symbol> * Set<int * int>

ParserLL1 startP parseTable w

Full Usage: ParserLL1 startP parseTable w

Parameters:
Returns: (Symbol list * Symbol list * string) list

perform predictive LL(1) parsing based on a ParseTable

startP : Symbol
parseTable : Map<(Symbol * Symbol), Set<Symbol * Symbol[]>>
w : string
Returns: (Symbol list * Symbol list * string) list

ParserSLR1 actTable w

Full Usage: ParserSLR1 actTable w

Parameters:
Returns: (StackSymbol list * Symbol list * ActionSLR1) list

perform SLR(1) parsing based on an action table

actTable : Map<(int * Symbol), Set<ActionSLR1>>
w : string
Returns: (StackSymbol list * Symbol list * ActionSLR1) list

PdaTool qscoll

Full Usage: PdaTool qscoll

Parameters:

function to be called by the cgi script

qscoll : NameValueCollection

PrintGrammar g

Full Usage: PrintGrammar g

Parameters:

print a grammar

g : Grammar

PrintLL1Parse pl

Full Usage: PrintLL1Parse pl

Parameters:

print the parsing derivation as a html table

pl : seq<Symbol list * Symbol list * string>

PrintSLR1Parse pL

Full Usage: PrintSLR1Parse pL

Parameters:

print the parsing derivation as computed by ParserSLR1 as a html table

pL : seq<StackSymbol list * Symbol list * 'a>

ProductiveSymbols g

Full Usage: ProductiveSymbols g

Parameters:
Returns: Set<Symbol>

Compute the productive symbols of a grammar g, i.e., those nonterminal symbols that can produce a nonempty set of terminal words.

g : Grammar
Returns: Set<Symbol>

ReachableSymbols g

Full Usage: ReachableSymbols g

Parameters:
Returns: Set<Symbol>

Compute the nonterminal symbols of a grammar g, i.e., those nonterminal symbols that can be reached by productions from the start symbol.

g : Grammar
Returns: Set<Symbol>

RemoveChainProductions g

Full Usage: RemoveChainProductions g

Parameters:
Returns: (Symbol * Word[])[]

This function removes all chain productions A->B of a grammar.

g : Grammar
Returns: (Symbol * Word[])[]

RemoveEpsilonProductions nullable g

Full Usage: RemoveEpsilonProductions nullable g

Parameters:
Returns: Grammar

This function removes all epsilon productions A->ϵ of a grammar.

nullable : Set<Symbol>
g : Grammar
Returns: Grammar

RemoveUselessRules reachSym prodSym g

Full Usage: RemoveUselessRules reachSym prodSym g

Parameters:
Returns: (Symbol * Symbol[][])[]

Clean-up a given context-free grammar by removing useless non-terminals and their rules, i.e., nonterminals that are either not reachable or that are no productive (cannot generate any terminal word). The obtained grammar is equivalent, but has only useful symbols and rules.

reachSym : Set<Symbol>
prodSym : Set<Symbol>
g : Grammar
Returns: (Symbol * Symbol[][])[]

TerminalsOfGrammar g

Full Usage: TerminalsOfGrammar g

Parameters:
Returns: Set<Symbol>

TerminalsOfGrammar computes for a given grammar the set of terminals.

g : Grammar
Returns: Set<Symbol>