Class Grammar
java.lang.Object
org.episteme.natural.computing.ai.automata.Grammar
Context-Free Grammar implementation.
A context-free grammar is a formal grammar consisting of:
- A set of terminal symbols (the alphabet)
- A set of non-terminal symbols (variables)
- A set of production rules
- A start symbol
- Since:
- 1.0
- Author:
- Silvere Martin-Michiellot, Gemini AI (Google DeepMind)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds an epsilon (empty) production.addNonTerminal(String nonTerminal) Adds a non-terminal symbol.addProduction(String lhs, String... rhsSymbols) Adds a production rule.addTerminal(String terminal) Adds a terminal symbol.addTerminals(String... terminals) Adds multiple terminal symbols.generateRandom(int maxSteps) Generates a random derivation from the start symbol.Returns all non-terminal symbols.getProductions(String nonTerminal) Returns all productions for a given non-terminal.Returns the start symbol.Returns all terminal symbols.booleanisNonTerminal(String symbol) Checks if a symbol is a non-terminal.booleanisTerminal(String symbol) Checks if a symbol is a terminal.toString()
-
Constructor Details
-
Grammar
Creates a new grammar with the given start symbol.- Parameters:
startSymbol- the start symbol (non-terminal)
-
-
Method Details
-
addTerminal
-
addTerminals
-
addNonTerminal
-
addProduction
-
addEpsilonProduction
-
getStartSymbol
-
getTerminals
-
getNonTerminals
-
getProductions
-
isTerminal
Checks if a symbol is a terminal.- Parameters:
symbol- the symbol- Returns:
- true if terminal
-
isNonTerminal
Checks if a symbol is a non-terminal.- Parameters:
symbol- the symbol- Returns:
- true if non-terminal
-
generateRandom
Generates a random derivation from the start symbol.Useful for testing and generating sample strings. May not terminate for grammars with left recursion.
- Parameters:
maxSteps- maximum derivation steps- Returns:
- the derived string, or null if max steps exceeded
-
toString
-