Rule Table
Overview
A rule list is used by the developer to specify where the Builder will insert a list of
rules into the skeleton program. The notation allows the developer to specify anything
from an enumerated constant list to a list of "case" statements. The following
diagram shows the format used for to denote a Rule List. The notation is almost identical
to that of symbol lists.
Structure
The ##Rule-Table block was added in Version 2.5 of the Builder.
|
##RULE-TABLE |
|
|
... |
|
[ |
##RULES |
|
|
... |
|
[ |
##RULE-SYMBOLS |
|
|
... |
|
|
##END-RULE-SYMBOLS |
] |
|
... |
|
|
##END-RULES |
] |
|
... |
|
|
##END-RULE-TABLE |
|
|
or |
|
##RULES |
|
|
... |
|
[ |
##RULE-SYMBOLS |
|
|
... |
|
|
##END-RULE-SYMBOLS |
] |
|
... |
|
|
##END-RULES |
|
|
Options
The following tags can be used to enhance the text generated by the skeleton program.
[ |
##ID-CASE |
{ ProperCase | Uppercase | Lowercase
| None } |
] |
[ |
##ID-SEPARATOR |
Text |
] |
[ |
##ID-SYMBOL-PREFIX |
Text |
] |
[ |
##ID-RULE-PREFIX |
Text |
] |
[ |
##DELIMITER |
Text |
] |
Name |
Description |
##ID-CASE |
When the GOLD Parser Builder creates identifiers for each
constant, the system can put each in either ProperCase or Uppercase. This value should be
set to the standard conventions used in the target language. Lowercase will be supported
in the next version. |
##ID-SEPARATOR |
For readability, many programming languages allow the use
of characters, such as underscores and dashes, to be used in identifiers. The value of
this field will used in the constant names. |
##ID-SYMBOL-PREFIX |
The value of this field will be added to the front each
generated symbol constant. |
##ID-RULE-PREFIX |
The value of this field will be added to the front each
generated rule constant. |
##DELIMITER |
This tag is used to specify the characters used to
display lists. This variable is used in the construction of rule lists, symbol lists, and
chararacter sets. |
Variables
RULE-TABLE Block
Name |
Description |
%Count% |
The Count variable contains the number of rules in the
grammar's Rule Table. This variable is useful for array declaration or setting the global
variables before storing each of the actual rules. |
RULES Block
Name |
Description |
%Delimiter% |
For each state in the table, this variable is set to the
value set with the ##Delimiter tag. For the last item in the list, the value is set to a
number of spaces. |
%Description% |
This variable contains a friendly description of the rule
- using Backus-Naur form. The variable is designed so that the developer can put comments
directly into the code that describes the actual content of the rule. |
%Description.XML% |
This variable will display the contents of the
Description in XML format. |
%ID% |
This variable contains a name generated by the GOLD
Parser Builder for each rule in the grammar. The format of the ID is specified by the the
template's parameter fields. |
%ID.Padded% |
When creating a list, the value of each of these
variables will contain added spaces so that each identifier is the same width. The
variable is primarily used in the construction of enumerated constants or anywhere when
the text should "line-up" for readability. |
%Index% |
This variable contains the index of the rule in the
table. |
%NonterminalIndex% |
Each rule contains both a head and body. This variable
contains the symbol index of the nonterminal that represents the head of the rule. |
%SymbolCount% |
This variable contains the number of symbols that are
part of the rule's body. This variable is useful for setting the size of arrays before
storing each of the symbols. |
%Value% |
This variable simply contains the index of the rule in
the table. This variable has the same value as %Index%. |
%Value.Padded% |
Like the ID.Padded variable, each item in a generated
list will contain a number of spaces such that each is the same width. |
RULE-SYMBOLS Block
Name |
Description |
%Delimiter% |
For each state in the table, this variable is set to the
value set with the ##Delimiter tag. For the last item in the list, the value is set to a
number of spaces. |
%SymbolIndex% |
This variable contains the index of the symbol in the
grammar's Symbol Table. |
Example: Rule Table
The following displays a template that will output the content of the Rule Table using
formatted text.
##RULE-TABLE |
Table Count: %Count% |
##RULES |
Rule %Index% |
Value: %Value% |
Nonterminal: %NonterminalIndex% |
Description: %Description% |
Symbol
Count: %SymbolCount% |
Rule
Symbols: |
##RULE-SYMBOLS |
%SymbolIndex% |
##END-RULE-SYMBOLS |
##END-RULES |
##END-RULE-TABLE |
If the "Simple" example grammar is used, the program template will create the
following text for Rule #6. The rules before and after #6 were excluded for brevity.
Table Count: 28 |
. |
. |
. |
Rule 6 |
Value: 6 |
Nonterminal: 36 |
Description: <Statement> ::= if <Expression> then <Statements> end |
Rule Count:
5 |
Rule
Symbols: |
26 |
33 |
30 |
37 |
24 |
Example: Enumerated Constants
The following are a few examples on how to define various rule lists.
C++ Enumerated Constants
enum
RuleConstants |
{ |
##DELIMITER ',' |
##RULES |
%ID.Padded% = %Value.Padded% %Delimiter% // %Description% |
##END-RULES |
}; |
C++ Switch Statement
This example assumes that the value of 'index' contains the index of the rule. The
resulting "switch" statement definition contains a list of empty sections -
ready for the developer to use.
switch (index) |
{ |
##RULES |
case %ID% |
// %Description% |
break; |
##END-RULES |
} |
Visual Basic Enumerated Constants
Enum
RuleConstants |
##RULES |
%ID.Padded% = %Value.Padded% ' %Description% |
##END-RULES |
End Enum |
Visual Basic Select Case Statement
This example assumes that the value of 'Index' contains the index of the rule.
Select Case
Index |
##RULES |
case %ID% |
' %Description% |
##END-RULES |
End Select |