Literals
[This content is no longer valid. For the latest information on "M", "Quadrant", SQL Server Modeling Services, and the Repository, see the Model Citizen blog.]
Grammars consist of a complex hierarchy of rules. Ultimately, however, these rules resolve to sequences of literals.
A literal specifies a string of one or more characters that can appear in the input stream. Common examples of literals are strings of letters ("Name") and numbers ("2009"). Literals may also contain escape sequences such as "\r" (return), "\l" (line feed), and "\t" (tab). Literals may also contain Unicode characters, such as "\u000D".
One way to specify a literal is by enclosing its characters inside double or single quotes, as seen in the prior examples. You can also specify "verbatim text literals" (for example, @"123") that can extend over multiple lines.
Examples of Literals
Literals can appear in a wide variety of rules.
Token Literals
Literals often appear in token rules, and are used in the process of tokenizing the input stream. Following are a few examples.
token TypeLit = "TYPE";
token NameLit = "Name=";
token LF = "\l";
token CR = "\r";
token Space = "\u0020";
Literals in Syntax Rules
Literals can also appear in syntax rules, as seen in the following example.
syntax ExportDirective = "export" SimpleNames ";";
In this example, two literals appear directly: "export" and the ";" character. Such literals are treated by MGrammar as "anonymous tokens": even though the literals do not appear in token rules, the parser treats them as tokens and uses them as such when tokenizing the input stream.
Note the reference to the SimpleNames
rule in the example. Ultimately that rule either specifies literals directly or points to other rules, which eventually specify literals.