Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

spirit_sql example

33 views
Skip to first unread message

Gerhard Wolf

unread,
Sep 11, 2015, 4:40:00 AM9/11/15
to
Hi,

with some changes (add classic to include and using statements)
i have managed to compile the:

http://boost-spirit.com/distrib/spirit_1_7_0/libs/spirit/example/application/sqlparser/spirit_sql.cpp
example.

its the first contact with spirit and it seems to be pretty complex.
The code seems to work as i understand it. But my first change request
would be to return more details of the failed parse, not only true or
false. A Position would be fine!
Any Tips?



Öö Tiib

unread,
Sep 11, 2015, 8:30:22 AM9/11/15
to
Basically you need to qualify some 'on_error<fail>' and some
expectation points ( by using > instead of >> ) and to get better
runtime errors you likely want to name the rules of yours like
'where_stmt.name("WHERE statement");'.

When you need examples then perhaps the example
http://www.boost.org/doc/libs/1_56_0/libs/spirit/doc/html/spirit/qi/tutorials/mini_xml___error_handling.html
is a good start.

By my impression:
* Spirit is lot nicer than to parse manually by iterating over character
buffers or by using regular expressions (but who does that anyway).
* Spirit is quite fast. It can parse integers faster than atoi
http://alexott.blogspot.com.ee/2010/01/boostspirit2-vs-atoi.html
* Spirit can seriously slow down compiling, better keep it away from
(commonly included) header files.
* Compiler error messages can be horrible with Spirit since it is using
C++ syntax as BNF. Parser generators provide far better diagnostics.
* Spirit has its limitations. About same as Flex/Bizon. One may need
ANTLR or Earley parser for complex grammar.


mark

unread,
Sep 11, 2015, 9:38:48 AM9/11/15
to
On 2015-09-11 10:39, Gerhard Wolf wrote:
>
> its the first contact with spirit and it seems to be pretty complex.
> The code seems to work as i understand it. But my first change request
> would be to return more details of the failed parse, not only true or
> false. A Position would be fine!
> Any Tips?

Yes, my tip is stay very far away from Spirit if you want to keep your
sanity. A recursive decent parser isn't really that much more code.
While the Spirit code may look simple at first glance, looks can be
very, very deceiving.

Once your parser gets a bit bigger, you will encounter completely
undecipherable error messages from deep within the bowels of insane
Boost template metaprogramming. A single error message can literally be
megabytes in size (IIRC, my record is 5MB). People have reported
inlining depths of several hundred levels deep. Good luck with trying to
debug your grammar, if your parser doesn't quite do what you expect.

IME, Spirit often produces poor code that's rather slow. E.g., my
straightforward JSON recursive decent parser is 50x faster than the 3
different Spirit JSON parsers I tried.

If you insist on using Spirit, make sure to compile your code with all
compilers you can get your hands on. You are almost guaranteed to hit
corner cases of the language.

mark

unread,
Sep 11, 2015, 12:58:46 PM9/11/15
to
On 2015-09-11 14:29, Öö Tiib wrote:
> * Spirit is quite fast. It can parse integers faster than atoi
> http://alexott.blogspot.com.ee/2010/01/boostspirit2-vs-atoi.html

You mean that buggy benchmark that only parses the int once?

---------------------------------------------------------
void test_spirit(int number_of_repeat) {
int val=0;
for(int i=0; i < number_of_repeat; i++) {
qi::parse(nstr,nstr+6,int_,val);
if (val != 123456)
std::cout << "Spirit Errror! val=" << val << std::endl;
}
}
---------------------------------------------------------

Note the missing error checking for the qi::parse result. In the first
iteration, nstr is advanced to the end of string and the rest of the
iterations parse an empty string.

Even then this benchmark is pointless since it only tests a hand-coded
int-parser that's part of Spirit and not an actual Spirit grammar:

boost/spirit/home/qi/numeric/detail/numeric_utils.hpp
0 new messages