public abstract class GrammarRule extends java.lang.Object implements java.lang.Comparable<GrammarRule>
Whew!!! The thing is a pantsload. I get tired just thinking about all this stuff.
Modifier and Type | Field and Description |
---|---|
protected boolean |
isBaseRule
isBaseRule indicates that the GrammarRule is derived directly from the
baseSyntaxAxiom, and is unaffected by other NullsPermitted or
TypeConversion GrammarRules.
|
static java.util.Comparator<GrammarRule> |
MAX_SEQ_NBR
MAX_SEQ_NBR sequences by GrammarRule.maxSeqNbr & ruleNbr
|
protected int |
maxSeqNbr
maxSeqNbr is the greatest MObj(dot)seq of any Stmt in
paramTransformationTree, and represents precedence since a statement's
ParseTree should not refer to any Stmt's with sequence numbers higher
than the Stmt's own sequence number.
|
protected int |
nbrHypParamsUsed
nbrHypParamsUsed == the number of hypotheses actually in use in the
paramTransformationTree for the GrammarRule.
|
protected ParseTree |
paramTransformationTree
paramTransformationTree is a ParseTree that is "splice onto" a set of
Variable Hypotheses (if any) to produce a new sub-tree when a GrammarRule
is matched to a sub-sequence of an expression.
|
protected ParseNode[] |
paramVarHypNode
paramVarHypNode -- as unbelievable as this sounds -- has object
references to the VarHyp ParseNode's in paramTransformationTree, thus
eliminating most searches.
|
static java.util.Comparator<GrammarRule> |
RULE_NBR
RULE_NBR sequences by GrammarRule.ruleNbr
|
protected Cnst[] |
ruleFormatExpr
ruleFormatExpr contains the GrammarRule's Expression rewritten where each
Var's VarHyp's Type Code replaces the Var or VarHyp (ie "( wff -> wff )"
).
|
protected int[] |
ruleHypPos
ruleHypPos[i] is an index of a VarHyp's position within ruleFormatExpr
(helpful in EarleyParser).
|
protected int |
ruleNbr
ruleNbr uniquely identifies a GrammarRule.
|
Constructor and Description |
---|
GrammarRule()
Default constructor.
|
GrammarRule(Grammar grammar)
Constructor -- create skeleton GrammarRule w/ruleNbr.
|
GrammarRule(Grammar grammar,
Axiom baseSyntaxAxiom)
Constructor -- default GrammarRule for base Syntax Axioms, which means no
parameter "transformations".
|
Modifier and Type | Method and Description |
---|---|
static boolean |
add(Axiom baseSyntaxAxiom,
Grammar grammar)
Adds a Syntax Axiom's "base" grammar rule plus all derived grammar rules
to the Grammar.
|
abstract void |
addToGrammar(Grammar grammar,
Cnst[] ruleFormatExpr)
Add rule format expression to the Rule Forest.
|
ParseNode |
buildGrammaticalParseNode(ParseNodeHolder[] paramArray)
Builds a grammatical parse node from the GrammarRule using an input array
of parameters to substitute into the rule's variable hypotheses.
|
Cnst[] |
buildRuleFormatExpr()
Build ruleFormatExpr version of the GrammarRule's expression.
|
int |
compareTo(GrammarRule obj)
Compares GrammarRule object based on the seq.
|
abstract void |
deriveAdditionalRules(Grammar grammar)
deriveAdditionalRules based on the addition of a new GrammarRule to those
already generated and accepted.
|
boolean |
equals(java.lang.Object obj)
Compare for equality with another GrammarRule.
|
protected int |
findMatchingVarHypTyp(int nextSearch,
Cnst searchTyp)
Scans GrammarRule's paramVarHypNode array looking for a match to the
input Type Code.
|
Axiom |
getBaseSyntaxAxiom()
Return GrammarRule's "base" Syntax Axiom.
|
abstract GrammarRule |
getDupRule(Grammar grammar,
Cnst[] ruleFormatExpr)
Return a duplicate of the input ruleFormatExpr if it exists, or return
null.
|
abstract Cnst[] |
getForestRuleExpr()
Return the GrammarRule's ruleFormatExpr from the Rule Forest.
|
Cnst |
getGrammarRuleTyp()
Return GrammarRule's Type Code.
|
boolean |
getIsBaseRule()
Return GrammarRule's isBaseRule flag.
|
int |
getMaxSeqNbr()
Return GrammarRule's maxSeqNbr.
|
int |
getNbrHypParamsUsed()
Return GrammarRule's nbrHypParamsUsed.
|
ParseTree |
getParamTransformationTree()
Return GrammarRule's paramTransformationTree.
|
ParseNode[] |
getParamVarHypNode()
Return GrammarRule's paramVarHypNode array.
|
ParseNodeHolder[] |
getParseNodeHolderExpr()
return parseNodeHolderExpr version of the GrammarRule's expression.
|
Cnst[] |
getRuleFormatExpr()
Return GrammarRule's ruleFormatExpr.
|
java.lang.String |
getRuleFormatExprAsString()
Return GrammarRule's ruleFormatExpr as String of Stmt labels.
|
Cnst |
getRuleFormatExprFirst()
Return the first symbol of a GrammarRule's ruleFormatExpr.
|
Cnst |
getRuleFormatExprIthSymbol(int i)
Return the "i-th" symbol of a GrammarRule's ruleFormatExpr.
|
int[] |
getRuleHypPos()
Return GrammarRule's ruleHypPos array.
|
int |
getRuleNbr()
Return GrammarRule's ruleNbr.
|
int[] |
getSyntaxAxiomVarHypReseq()
Return GrammarRule's "base" Syntax Axiom's varHypReseq array.
|
int |
hashCode()
Computes hashcode for this GrammarRule.
|
void |
setIsBaseRule(boolean isBaseRule)
Set GrammarRule's isBaseRule flag.
|
void |
setMaxSeqNbr(int maxSeqNbr)
Set GrammarRule's maxSeqNbr.
|
void |
setNbrHypParamsUsed(int nbrHypParamsUsed)
Set GrammarRule's nbrHypParamsUsed.
|
void |
setRuleFormatExpr(Cnst[] rfe)
Set GrammarRule's ruleFormatExpr.
|
static java.lang.String |
showRuleFormatExprAsString(Cnst[] rfe)
Return a GrammarRule's ruleFormatExpr as String of Stmt labels.
|
java.lang.String |
toString() |
protected int ruleNbr
Rule Numbers are sssigned in sequence of rule creation, which corresponds to precedence -- that is, Metamath database sequence and how the Grammar Rules are generated.
Grammar.assignNextGrammarRuleNbr()
protected ParseTree paramTransformationTree
A GrammarRule may involve multiple Syntax Axioms including Type Conversions and Nulls Permitted Syntax Axioms. This means that a GrammarRule whose "base" Syntax Axiom has multiple variable hypotheses may, in practice require only a single variable hypothesis -- or none at all, due to Nulls Permitteds.
Note: (Axiom)paramTransformationTree.getRoot().getStmt()
==
GrammarRule "baseSyntaxAxiom". Therefore, we are not storing the
baseSyntaxAxiom redundantly as a separate element.
protected ParseNode[] paramVarHypNode
The paramVarHypNode array has the same size as GrammarRule's base syntax axiom's var hyp array, but may contain null entries since a GrammarRule may require fewer parameters due to Nulls Permitted rule(s).
protected int maxSeqNbr
protected int nbrHypParamsUsed
nbrHypParamsUsed may be less than the number of variable hypotheses for the base Syntax Axiom because the GrammarRule may employ nulls to "flesh out" the hypotheses for the base Syntax Axiom. Also, the number may be zero; this is the case for Constant Syntax Axioms and for Constant type Grammar Rules derived from Syntax Axioms where nulls are permitted.
protected Cnst[] ruleFormatExpr
This was a late addition to GrammarRule caused by the need of EarleyParser's Predictor and Lookahead to know the first symbol of a GrammarRule. To save further searching, perhaps expensive marches through the Grammar Rule Forest, I decided to add the entire ruleFormatExpr here. Given that the entire expression is now stored in GrammarRule and that there are cheaper ways to find duplicates than traipsing through the Grammar Rule Forest -- and EarleyParser does not require the Forest for its work -- there are few impediments to eliminating the Forest altogether (just need to rework mmj.util.Dump a bit, I think.)
protected int[] ruleHypPos
Array length = nbrHypParamsUsed and is sequenced by hyp position within the ruleFormatExpr.
protected boolean isBaseRule
It is here for the eventuality that we'll use an EarleyParser that handles its own NullsPermitted and TypeConversion rules (otherwise, it can say adios).
public static final java.util.Comparator<GrammarRule> RULE_NBR
public static final java.util.Comparator<GrammarRule> MAX_SEQ_NBR
public GrammarRule()
public GrammarRule(Grammar grammar)
grammar
- The Grammar.public abstract void addToGrammar(Grammar grammar, Cnst[] ruleFormatExpr)
grammar
- The Grammar object (Mr Big).ruleFormatExpr
- the expression to add.public abstract void deriveAdditionalRules(Grammar grammar)
A PriorityQueue is used to store new derivedGrammarRules awaiting processing (dup checking, parse checking and possible addition to the Grammar data.) The Comparator orders derived rules based on MaxSeqNbr and ruleNbr so that low Metamath sequence number statements are added to the Grammar before higher sequenced statements. The existing rules are stored in separate repositories, which are scanned, as needed, when a new rule is added; the order that the repositories are scanned is irrelevant because the PriorityQueue controls update processing sequence.
Note also that a newly derived rule may generate other derived rules, which in turn trigger other derivations; this is a "feature" (as well as a scary opportunity for an infinite loop!) The benefit is that each pair of rules may be considered separately; when processing the pair there is no need to worry about other possible Type Conversion and Null Permitted Rules. For example, an "A" Type hypothesis in a Notation Rule may be the target of a "B to A" Type Conversion, but the fact that there is a "C to B" or "C to A" conversion can be ignored -- if the "B to A" conversion generates a variant rule of the original rule, then when *that* rule comes back through the PriorityQueue, the "C to B" rule will automatically come into play. This also means that a "B to A" conversion will combine with a "C to B" conversion to generate a "C to A" conversion -- eventually.
grammar
- The Grammar.public abstract GrammarRule getDupRule(Grammar grammar, Cnst[] ruleFormatExpr)
grammar
- The Grammar.ruleFormatExpr
- the expression to add.public abstract Cnst[] getForestRuleExpr()
This is pretty obsolete now that ruleFormatExpr is stored in GrammarRule, but it is still used in Dump (for now).
public static boolean add(Axiom baseSyntaxAxiom, Grammar grammar)
This is a major function in GrammarRule. Hairy.
Checks for Type Conversion "loop" on Syntax Axiom base Grammar Rule,
baseSyntaxAxiom
- Syntax Axiom.grammar
- The Grammar.public int hashCode()
hashCode
in class java.lang.Object
public int compareTo(GrammarRule obj)
compareTo
in interface java.lang.Comparable<GrammarRule>
obj
- GrammarRule object to compare to this GrammarRulepublic boolean equals(java.lang.Object obj)
Equal if and only if the GrammarRule sequence numbers are equal. and the obj to be compared to this object is not null and is a GrammarRule as well.
equals
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public int getRuleNbr()
public ParseTree getParamTransformationTree()
public ParseNode[] getParamVarHypNode()
public int getMaxSeqNbr()
public void setMaxSeqNbr(int maxSeqNbr)
maxSeqNbr
- GrammarRule's maxSeqNbr.public int getNbrHypParamsUsed()
public void setNbrHypParamsUsed(int nbrHypParamsUsed)
nbrHypParamsUsed
- GrammarRule's nbrHypParamsUsed.public Axiom getBaseSyntaxAxiom()
public int[] getSyntaxAxiomVarHypReseq()
public Cnst getGrammarRuleTyp()
public int[] getRuleHypPos()
public Cnst[] getRuleFormatExpr()
public void setRuleFormatExpr(Cnst[] rfe)
rfe
- GrammarRule's ruleFormatExpr.public java.lang.String getRuleFormatExprAsString()
public static java.lang.String showRuleFormatExprAsString(Cnst[] rfe)
rfe
- ruleFormatExpr.public Cnst getRuleFormatExprFirst()
public Cnst getRuleFormatExprIthSymbol(int i)
i
- the indexpublic boolean getIsBaseRule()
public void setIsBaseRule(boolean isBaseRule)
isBaseRule
- flag.public ParseNodeHolder[] getParseNodeHolderExpr()
The root of a GrammarRule paramTransformationTree is always the baseSyntaxAxiom. Since no Syntax Axiom may be a composite function that is parseable into other Syntax Axiom rules, the only possible substitution into a *grammar rule* variable is 1) another variable via a Type Conversion Rule or 2) null, via a Nulls Permitted Rule. And, since GrammarRule contains the paramVarHypNode array we can directly write out the resulting formula and/or ruleFormatExpr and/or ParseNodeHolderExpr -- an element of paramVarHypNode == null *must be* a null substitution, by virtue of the above considerations.
public Cnst[] buildRuleFormatExpr()
This is a clone of GrammarRule.getParseNodeHolderExpr() with a different output format, essentially.
The root of a GrammarRule paramTransformationTree is always the baseSyntaxAxiom. Since no Syntax Axiom may be a composite function that is parseable into other Syntax Axiom rules, the only possible substitution into a *grammar rule* variable is 1) another variable via a Type Conversion Rule or 2) null, via a Nulls Permitted Rule. And, since GrammarRule contains the paramVarHypNode array we can directly write out the resulting formula and/or ruleFormatExpr and/or ParseNodeHolderExpr -- an element of paramVarHypNode == null *must be* a null substitution, by virtue of the above considerations.
public ParseNode buildGrammaticalParseNode(ParseNodeHolder[] paramArray)
This is a rude dog.
buildGrammaticalParseNode -- pads out the parameter list as needed (for nulls in the rule) and resequences the parameters from the symbol sequence order to the database sequence order used by Metamath for statement hypotheses. Then the parameters are substituted into the GrammarRule's parameterTransformationTree, which is basically a model parse sub-tree for the GrammarRule, requiring only replacement of the GrammarRule's variable hypotheses with the input parameters. Note that the variable hypothesis nodes for a grammar rule are at the bottom of the tree (true for all parse trees), and this means that we are splicing in the parameters at the bottom, building up a larger tree that contains the grammar rule at the top of the tree.
NOTE: Originally, the GrammarRule design didn't call for the parameter transformation data to be stored in a parse tree. But, in highly unusual grammars using Nulls Permitted, a Syntax Axiom with two variable hypotheses generates a Type Conversion Rule. With one of the hypotheses = null, a Syntax Axiom containing no constants in the expression and two variables generates a Type Conversion Grammar Rule. So.....we carry around this stinking tree structure that may contain Type Conversions and Nulls Permitted transformations along with variable hypotheses that are to be substituted. In other words, blame Nulls Permitted for this complexificationizing.
paramArray
- substitutions for variable hypothesis nodes of the
Grammar Rule's paramTransformationTree.protected int findMatchingVarHypTyp(int nextSearch, Cnst searchTyp)
This is a key function used in deriving additional rules using a TypeConversionRule or NullsPermittedRule.
nextSearch
- starting index of search.searchTyp
- Type Code sought among paramVarHypNodes.java.lang.IllegalStateException
- only if there is a severe bug in the code
(which performs a double-check here just to be on the safe
side -- we'd rather have a blow-up than deliver bogus
answers?)