Search code examples
c++boostboost-spirit

Boost.Spirit Alternative Parser parallelization


I'm parsing a buffer using x3 rule that has many alternative sub-rules. Actually, I have data from different GPS devices and my main parser looks like this :

 auto gps_r = device1_r | device2_r | device3_r;

 bool ok = x3::parse(...,gps_r,..);

I understand I may implement invoking of x3::parse() in parallel for input data and for each device rule. But that may not be applicable for some recursive parsing (say SAX DOM parsing).

My question is more theoretical : are there any attempts to make Alternative Parser being async (say, using boost.coroutines2) to make parsing in parallel?


Solution

  • Usually there is no room for parallelization as composed grammars tries to perform as less lookahead and backtrack as possible. In this case any benefits from parallel parsing will be outweighed by thread spawning and synchronization overhead.

    If your grammar actually branches from the beginning like in the shown example - you can rewrite it to run multiple x3::parse in parallel.

    There is also problem in current Spirit alternate parser implementation, as it does not flatten the expression tree (while it is a binary tree, usually it is very unbalanced).