Class Grammar
Abstract base class for grammar implementations in the Earley parser.
Inherited Members
Namespace: EarleyParser
Assembly: EarleyParser.dll
Syntax
public abstract class Grammar
Remarks
Provides the foundation for both context-free and linear indexed grammars.
Key features:
-
Grammar Structure:
- Dictionary of production rules indexed by left-hand side
- Support for both context-free and linear indexed rules
- Special symbols for grammar operations (Gamma, Start, Epsilon, Star)
-
Grammar Analysis:
- Detection of recursive non-terminals
- Analysis of grammar reachability
- Support for parts of speech vocabulary
-
Rule Management:
- Dynamic rule addition and removal
- Support for tentative grammar modifications
- Validation of grammar structure
Usage:
- Instantiate either ContextFreeGrammar or LinearIndexedGrammar
- Load rules from grammar files (see CFGExample.txt and LIGExample.txt)
- Load parts of speech from vocabulary.json
The grammar class is the foundation of the parsing system, defining the language structure that the Earley parser will recognize.
Constructors
| Edit this page View SourceGrammar(IEnumerable<Rule>)
Initializes a new instance of the Grammar class.
Declaration
public Grammar(IEnumerable<Rule> rules1)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<Rule> | rules1 | The collection of rules to initialize the grammar with. |
Fields
| Edit this page View SourceEpsilonSymbol
Gets the epsilon symbol used for empty productions.
Declaration
public const string EpsilonSymbol = "Epsilon"
Field Value
| Type | Description |
|---|---|
| string |
GammaSymbol
Gets the gamma symbol used in grammar operations.
Declaration
public const string GammaSymbol = "Gamma"
Field Value
| Type | Description |
|---|---|
| string |
PartsOfSpeech
Gets or sets the set of valid parts of speech.
Declaration
public static HashSet<string> PartsOfSpeech
Field Value
| Type | Description |
|---|---|
| HashSet<string> |
Rules
Gets the dictionary of production rules indexed by their left-hand side.
Declaration
public readonly Dictionary<string, List<Rule>> Rules
Field Value
| Type | Description |
|---|---|
| Dictionary<string, List<Rule>> |
Remarks
Rules are organized into a dictionary with their Left Hand Side as key. For Linear Indexed Grammar, multiple Context Free rules may be generated for one LIG rule. Only includes CFG Rules reachable from START nonterminal.
StarSymbol
Gets the star symbol used in grammar operations.
Declaration
public const string StarSymbol = "*"
Field Value
| Type | Description |
|---|---|
| string |
StartSymbol
Gets the start symbol used to begin parsing.
Declaration
public const string StartSymbol = "START"
Field Value
| Type | Description |
|---|---|
| string |
Methods
| Edit this page View SourceFindAllRecursiveNonterminals()
Finds all recursive non-terminals in the grammar.
Declaration
public HashSet<string> FindAllRecursiveNonterminals()
Returns
| Type | Description |
|---|---|
| HashSet<string> | A set of non-terminals that are recursive. |
FindRecursiveNonterminal(string, string, HashSet<string>)
Determines whether a non-terminal is recursive.
Declaration
public bool FindRecursiveNonterminal(string targetNT, string currentNT, HashSet<string> visited)
Parameters
| Type | Name | Description |
|---|---|---|
| string | targetNT | The non-terminal to check for recursion. |
| string | currentNT | The current non-terminal being examined. |
| HashSet<string> | visited | Set of non-terminals already visited. |
Returns
| Type | Description |
|---|---|
| bool | True if the target non-terminal is recursive; otherwise, false. |
GetRules()
Gets all rules in the grammar.
Declaration
public List<Rule> GetRules()
Returns
| Type | Description |
|---|---|
| List<Rule> | A list of all rules in the grammar. |
ToString()
Returns a string representation of the grammar.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | A string containing all rules and their count. |