This topic gives an overview of the EBNF file format used to define a grammar.
The following topics are prerequisites to understanding this topic:
This topic contains the following sections:
To define a grammar for the Syntax Parsing Engine you can use EBNF files. EBNF stands for Extended Backus-Naur Form, which is a format used to define context-free grammars. The specification for this format is ISO 14977.
EBNF files contain one or more rules which provide concise descriptions of the symbols and productions for a grammar.
The following is an example of a non-terminal symbol rule in the EBNF format used in the Syntax Parsing Engine.
VariableDeclarationStatement =
Type, IdentifierToken, [EqualsToken, Expression], SemicolonToken
| VarKeyword, IdentifierToken, EqualsToken, Expression, SemicolonToken;
As you can see the head of the definition is separated from the body with an equals sign, concatenated symbols are separated by commas and the entire rule is ended with a semicolon. If the head symbol hasn’t had a rule to define it yet in the file, a new non-terminal symbol will be created to represent it. If it had been declared already, the productions for this rule would be alternatives to any other productions previously defined. So the VariableDeclarationStatement in this example could have also been defined with multiple rules:
VariableDeclarationStatement =
Type, IdentifierToken, [EqualsToken, Expression], SemicolonToken;
VariableDeclarationStatement =
VarKeyword, IdentifierToken, EqualsToken, Expression, SemicolonToken;
Non-terminal symbols can be used before they are defined. This is required so that context-free grammars can be recursive. Without this it would not be possible to allow nested classes to be defined in a language like C#.
This is a simplified definition of the C# language:
Root = {ClassDeclaration};
ClassDeclaration = ['public'], 'class', Identifier, '{', Members, '}';
Members = {Member};
Member = ClassDeclaration | FieldDeclaration;
FieldDeclaration = ['public'], Type, Identifier, ';';
Type = Identifier;
The following topics provide additional information related to this topic.