Mercury library  1.0
Translation of CFG grammars into an object model (Bachelor's thesis).
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
Grammar.cs
Go to the documentation of this file.
1 using Mercury.Exceptions;
2 using Mercury.Nucleus.Collections;
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 
7 namespace Mercury.Syntax
8 {
9 //=============================================================================
10 // Grammar
11 //=============================================================================
12 
18  public class Grammar
19  {
20  //--[ Properties ]---------------------------------------------------
21 
23  public IReadOnlyList<Symbol> Nonterminals { get; internal set; }
24 
26  public IReadOnlyList<Symbol> Terminals { get; internal set; }
27 
29  public IReadOnlyList<Rule> Rules { get; internal set; }
30 
33  public Symbol Root { get; internal set; }
34 
37  public Symbol Epsilon { get; internal set; }
38  }
39 
40 //=============================================================================
41 // ExtendedGrammar
42 //=============================================================================
43 
48  internal class ExtendedGrammar : Grammar
49  {
50  //--[ Internal fields ]----------------------------------------------
51 
52  internal IMultiDictionary<Symbol, Rule> rulemap; // accessed by ExtendedGrammarBuilder
53 
54  //--[ Properties ]---------------------------------------------------
55 
57  public IReadOnlyList<Rule> TerminalRules { get; internal set; }
58 
60  public IReadOnlyList<Rule> EpsilonRules { get; internal set; }
61 
62  //--[ Methods ]------------------------------------------------------
63 
69  public IReadOnlyList<Rule> RulesWithInitial(Symbol s)
70  {
71  IReadOnlyList<Rule> rules;
72 
73  return rulemap.TryGetValue(s, out rules) ? new List<Rule>(rules) : new List<Rule>();
74  }
75  }
76 
77 }
The epsilon symbol.
Represents a single symbol of the grammar. This is an immutable class. For a symbol name to be val...
Definition: Symbol.cs:51
Represents a Context-Free Grammar. This is an immutable class, to create a grammar use Mercury...
Definition: Grammar.cs:18