Show / Hide Table of Contents

Class Grammar

Abstract base class for grammar implementations in the Earley parser.

Inheritance
object
Grammar
ContextFreeGrammar
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
Namespace: EarleyParser
Assembly: EarleyParser.dll
Syntax
public abstract class Grammar
Remarks

Provides the foundation for both context-free and linear indexed grammars.

Key features:

  1. 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)
  2. Grammar Analysis:
    • Detection of recursive non-terminals
    • Analysis of grammar reachability
    • Support for parts of speech vocabulary
  3. 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 Source

Grammar(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 Source

EpsilonSymbol

Gets the epsilon symbol used for empty productions.

Declaration
public const string EpsilonSymbol = "Epsilon"
Field Value
Type Description
string
| Edit this page View Source

GammaSymbol

Gets the gamma symbol used in grammar operations.

Declaration
public const string GammaSymbol = "Gamma"
Field Value
Type Description
string
| Edit this page View Source

PartsOfSpeech

Gets or sets the set of valid parts of speech.

Declaration
public static HashSet<string> PartsOfSpeech
Field Value
Type Description
HashSet<string>
| Edit this page View Source

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.

| Edit this page View Source

StarSymbol

Gets the star symbol used in grammar operations.

Declaration
public const string StarSymbol = "*"
Field Value
Type Description
string
| Edit this page View Source

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 Source

FindAllRecursiveNonterminals()

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

Overrides
object.ToString()
  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX