cppannotations/annotations/yo/concrete/bison.yo

58 lines
2.9 KiB
Text

The i(grammar specification file) required by ti(bisonc++) is comparable to
the specification file required by ti(bison). Differences are related to the
class nature of the resulting parser. Our calculator distinguishes real
numbers from integers, and supports a basic set of arithmetic operators.
tt(Bisonc++) should be used as follows:
itemization(
it() As usual, a grammar is defined. With tt(bisonc++) this is no
different, and tt(bisonc++) grammar definitions are for all practical
purposes identical to tt(bison)'s grammar definitions.
it() Having specified the grammar and (usually) some declarations
tt(bisonc++) can generate files defining the parser class and
the implementation of the member function tt(parse).
it() All class members (except those that are required for the proper
functioning of the member tt(parse)) must be
separately implemented. Of course, they should also be
declared in the parser class's header. At the very least the member
ti(lex) must be implemented. This member is called by tt(parse) to
obtain the next available token. However, tt(bisonc++) offers a
facility providing a standard implementation of the function
tt(lex). The member function
hi(error)tt(error(char const *msg))
is given a simple default implementation that may be modified by the
programmer. The member function tt(error) is called when tt(parse)
detects (syntactic) errors.
it() The parser can now be used in a program. A very simple example would
be:
verb(
int main()
{
Parser parser;
return parser.parse();
}
)
)
The tt(bisonc++) hi(bisonc++: grammar file) specification file has two
sections:
itemization(
it() The em(declaration section). In this section bison's tokens, and the
priority rules for the operators are declared. However, tt(bisonc++)
also supports several new declarations. These new declarations are
important and are discussed below.
it() The em(rules section). The i(grammatical rules) define the
grammar. This section is identical to the one required by tt(bison),
albeit that some members that were available in tt(bison) and
tt(bison++) are obsolete in tt(bisonc++), while other members can be
used in a wider context. For example, bf(ACCEPT) and bf(ABORT) can be
called from any member called from the parser's action blocks to
terminate the parsing process.
)
Readers familiar with tt(bison) may note that there is no
emi(header section) anymore. Header sections are used by bison to provide
for the necessary declarations allowing the compiler to compile the bf(C)
function generated by tt(bison). In bf(C++) declarations are part of or
already used by class definitions. Therefore, a parser generator generating a
bf(C++) class and some of its member functions does not require a header
section anymore.