This topic explains a grammar’s non-terminal symbols.
The following topics are prerequisites to understanding this topic:
This topic contains the following sections:
Non-terminal symbols represent groupings of textual elements. Like terminal symbols, non-terminal symbols have a unique name so the parser can distinguish them from other symbols. This name must be unique across all terminal and non-terminal symbols in the same grammar.
In addition to this name, the non-terminal symbol also defines sequences of symbols it can represent in the document. Each non-terminal symbol can represent one or more sequences of symbols. For example, a variable declaration in C# can be created in various ways:
A type followed by an identifier followed by a semicolon.
A type followed by an identifier followed by an equals sign followed by an expression followed by a semicolon.
The “var” keyword followed by an identifier followed by an equals sign followed by an expression followed by a semicolon.
And so on…
The same non-terminal symbol can represent all these variations. If one of these sequences of symbols is found in a document in a place where a variable declaration is expected, the variable declaration non-terminal symbol can be used to represent those symbols.
Each sequence of symbols a non-terminal symbol can represent is called a production. A production is typically written with the non-terminal symbol, known as the head of the production, on the left of an arrow and the sequence of symbols, known as the body of the production, on the right. For example, these might be three productions for a variable declaration symbol:
VariableDeclarationStatement → Type IdentifierToken SemicolonToken
VariableDeclarationStatement → Type IdentifierToken EqualsToken Expression SemicolonToken
VariableDeclarationStatement → VarKeyword IdentifierToken EqualsToken Expression SemicolonToken
And there may be others as well. The productions tell the syntax analyzer the ways in which non-terminal symbols can represent groups of symbols so it can go about constructing a hierarchical syntax tree representing a document.
The productions for a non-terminal symbol, and therefore the sequences of symbols it can represent, are defined by a rule tree exposed by the Rule property on the NonTerminalSymbol class.
The following topics provide additional information related to this topic.