var grammar = new Grammar();
var defaultLexerState = grammar.LexerStates.DefaultLexerState;
// Include the built-in symbols which should be matched in the default lexer state.
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; // Implicit conversion
var expression = grammar.NonTerminalSymbols.Add("Expression");
expression.Rule = ...
var variableDeclarationStatement =
grammar.NonTerminalSymbols.Add("VariableDeclarationStatement");
variableDeclarationStatement.Rule =
new AlternationSyntaxRule(
new ConcatenationSyntaxRule(
type, // Implicit conversions
identifier,
new OptionalSyntaxRule(
new ConcatenationSyntaxRule(
equals,
expression
)
),
semicolon
),
new ConcatenationSyntaxRule(
varKeyword,
identifier,
equals,
expression,
semicolon
)
);
grammar.StartSymbol = variableDeclarationStatement;