| 1 |
A formal grammar is a mathematical construct. To define the language for |
| 2 |
B(), you must write a file expressing the grammar in b() syntax: a |
| 3 |
em(B() grammar file). See chapter ref(GRAMMARFILES). |
| 4 |
|
| 5 |
A nonterminal symbol in the formal grammar is represented in b() input as |
| 6 |
an identifier, like an identifier in bf(C++). By convention, it should be in |
| 7 |
lower case, such as tt(expr), tt(stmt) or tt(declaration). |
| 8 |
|
| 9 |
The b() representation for a terminal symbol is also called a token |
| 10 |
type. Token types as well can be represented as bf(C++)-like identifiers. By |
| 11 |
convention, these identifiers should be upper case to distinguish them from |
| 12 |
nonterminals: for example, tt(INTEGER), tt(IDENTIFIER), tt(IF) or |
| 13 |
tt(RETURN). A terminal symbol that stands for a particular keyword in the |
| 14 |
language should be named after that keyword converted to upper case. The |
| 15 |
terminal symbol tt(error) is reserved for error recovery. See section |
| 16 |
ref(SYMBOLS), which also describes the current restrictions on the names of |
| 17 |
terminal symbols. |
| 18 |
|
| 19 |
A terminal symbol can also be represented as a character literal, just like a |
| 20 |
bf(C++) character constant. You should do this whenever a token is just a |
| 21 |
single character (parenthesis, plus-sign, etc.): use that same character in a |
| 22 |
literal as the terminal symbol for that token. |
| 23 |
|
| 24 |
The grammar rules also have an expression in b() syntax. For example, here is |
| 25 |
the b() rule for a bf(C++) return statement. The semicolon in quotes is a |
| 26 |
literal character token, representing part of the bf(C++) syntax for the |
| 27 |
statement; the naked semicolon, and the colon, are b() punctuation used in |
| 28 |
every rule. |
| 29 |
verb( |
| 30 |
stmt: |
| 31 |
RETURN expr ';' |
| 32 |
; |
| 33 |
) |
| 34 |
See section ref(RULES). |