[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Is there way to complete my Calculator with Bison or other GNU tools
From: |
David Fang |
Subject: |
Re: Is there way to complete my Calculator with Bison or other GNU tools? |
Date: |
Mon, 3 Jul 2006 21:52:59 -0400 (EDT) |
> I want to encapsulate a C++ class ,which can works like a calculator,its
> structure seems like this:
> Class Calc
> {
> //in yylex, result will be evaluated...
> ....
> };
> In an other word, Can yylex ,yyparse() be encapsulated in a C++ Class ?
Hi,
"Can...". Yes, but you will need to realize a few things. The C
code generated by [f]lex,yacc,bison use global variables to maintain the
state of the lexer buffers and parser state machines. If someone creates
multiple instances of your class, then each Parser (Calc) object will be
sharing the same variables, which make it unusable across multiple
objects.
You have many options at your disposal:
1) use flex/bison's C++ mode to generate proper lexer/parser classes
that encapsulate state in each object. (Don't have any experience
using those myself. Various online searches/sources discourage
thes use of those features, but they may have matured by now.)
2) manually hack up the C code generated by flex/yacc/bison to wrap
all state variables in C++ classes. (This is more of a hassle,
and requires understanding of the internals, but I've done this
myself and can offer advice in this direction.)
This can also be achieved by modifying the skeleton code
for versions that support custom skeletons. (Thought about it,
then quickly got lost reading through unfamiliar m4 sources.)
3) use something like a 'Singleton' pattern to prevent multiple instances
of the same class.
I'm sure others on this list can offer some advice too.
Fang