[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Bison C++ Syntax
From: |
Hans Aberg |
Subject: |
Bison C++ Syntax |
Date: |
Sun, 8 Oct 2000 15:35:31 +0200 |
I have a suggest for future Bison C++ user interface. I once wrote a parser
by hand, and developed the syntax in that context. I also just made a
working example with Bison compiled as C++ and the Flex C++ class it can
generate.
Then the use is like this (skipping namespace for now), when the parser is
a small calculator that reads a single line, and one wants to put it into a
REP (read-evaluate-print) loop:
int main() {
Parser calculator; // Could have arguments.
double d;
while (cin) {
try {
std::cin >> calculator >> d; // Parser can be used as manipulator.
std::cout << d << endl;
}
catch (Exit& ex) { // Parser exits by throwing an Exit exception.
std::cout << ex.what() << endl;
return ex.status();
}
}
return EXIT_SUCCESS;
}
The class Parse can also have various functions that allows the user to
manipulate it, for example Parser::reset(), and such functions.
I have found this manipulator syntax very efficient, because it is easy to
use; the user does not need to know much in order to get it up and working:
My example calculator exits by that one types "exit", and it does error
recovery, so it is rather self-contained. It would be easy to flip it into
any other standard program that wants to read a double, if one instead
wants to allow full arithmetical expressions in input. So it is extremely
easy to use this syntax in order to enhance already existing C++ programs
that are in the need of a more advanced parsed input.
Hans Aberg
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Bison C++ Syntax,
Hans Aberg <=