|
|
|
|
|
|
|
Syntax rules are the blueprints we use to build instructions in a program. They allow us to take the elements of a programming languagethe basic building blocks of the languageand assemble them into constructs, syntactically correct structures. If our program violates any of the rules of the languageby misspelling a crucial word or leaving out an important comma, for instancethe program is said to have syntax errors and cannot compile correctly until we fix them. |
|
|
|
| | |
|
|
|
|
Metalanguage is the word language with the prefix meta, which means beyond or more comprehensive. A metalanguage is a language that goes beyond a normal language by allowing us to speak precisely about that language. It is a language for talking about languages. |
|
|
|
| |
|
|
|
|
One of the oldest computer-oriented metalanguages is the Backus-Naur Form (BNF), which is named for John Backus and Peter Naur, who developed it in 1960. BNF syntax definitions are written out using letters, numbers, and special symbols. For example, a decimal (base-10) integer number in C++ must be at least one digit, it may or may not be more than one digit, and the first digit must be nonzero. The BNF definition of a decimal integer number in C++ is |
|
|
|
| |
|
|
|
|
<Decimallnteger> ::= <NonzeroDigit> ¦ <NonzeroDigit> <DigitSequence>
<NonzeroDigit> ::=1¦2¦3¦4¦5¦6¦7¦8¦9
<DigitSequence> ::= <Digit> ¦ <Digit> <DigitSequence>
<Digit> ::= 0¦ <NonzeroDigit> |
|
|
|
| |
|
|
|
|
where the symbol ::= is read is defined as, the symbol ¦ means or, the symbols <and> are used to enclose words called nonterminal symbols (symbols that still need to be defined), and everything else is called a terminal symbol. |
|
|
|
| |
|
|
|
|
The first line of the definition reads: A decimal integer is defined as a nonzero digit or a nonzero digit followed by a digit sequence. This line contains nonterminal symbols that must be defined. In the second line, the nonterminal symbol NonzeroDigit is defined as any one of the numeric characters 1 through 9, all of which are terminal symbols. The third line defines the nonterminal symbol DigitSequence as either a Digit or a Digit followed by another DigitSequence. The self-reference in the definition is a roundabout way of saying that a digit sequence can be a sequence of one or more digits. In the last line. Digit is defined as any one of the numeric characters 0 through 9. |
|
|
|
| |
|
|
|
|
BNF is an extremely simple language, but that simplicity leads to syntax definitions that can be long and difficult to read. An alternative metalanguage, the syntax diagram, is easier to follow. It uses arrows to indicate how symbols can be combined. Here are the syntax diagrams that define a decimal integer, a nonzero digit, and a digit in C++: |
|
|
|
|
|
|
|
|
|
(text box continues on next page) |
|
|
|
|
|