var grammar = new Grammar();
var defaultLexerState = grammar.LexerStates.DefaultLexerState;
// デフォルトのレクサー状態で一致すべき組み込み記号が含まれます。
defaultLexerState.Symbols.Add(grammar.WhitespaceSymbol);
defaultLexerState.Symbols.Add(grammar.NewLineSymbol);
var equals = defaultLexerState.Symbols.Add("EqualsToken", "=");
var semicolon = defaultLexerState.Symbols.Add("SemicolonToken", ";");
var varKeyword = defaultLexerState.Symbols.Add("VarKeyword", "var");
var identifier = defaultLexerState.Symbols.Add("IdentifierToken",
"[_a-zA-Z][_a-zA-Z0-9]*", TerminalSymbolComparison.RegularExpression);
var type = grammar.NonTerminalSymbols.Add("Type");
type.Rule = identifier; // 暗黙の変換
var expression = grammar.NonTerminalSymbols.Add("Expression");
expression.Rule = ...
var variableDeclarationStatement =
grammar.NonTerminalSymbols.Add("VariableDeclarationStatement");
variableDeclarationStatement.Rule =
new AlternationSyntaxRule(
new ConcatenationSyntaxRule(
type, // 暗黙の変換
identifier,
new OptionalSyntaxRule(
new ConcatenationSyntaxRule(
equals,
expression
)
),
semicolon
),
new ConcatenationSyntaxRule(
varKeyword,
identifier,
equals,
expression,
semicolon
)
);
grammar.StartSymbol = variableDeclarationStatement;