0

I have several large logical expressions (length upto 300K) of the form -

( ( ( 1 ) and ( 5933 or 561 or 1641 ) ) or ( ( 71 or 1 or 15 or 20 ) and ( 436 ) ) or ( ( 398 or 22 or 33 ) ) )

that are parsed using Boost Spirit (as shown in the example here - Boolean expression (grammar) parser in c++)

The parsing takes more than a minute for each expression.

I would like to do the parsing offline which results in an expression represented by -

typedef boost::variant <var, 
 boost::recursive_wrapper<unop <op_not> >, 
 boost::recursive_wrapper<binop<op_and> >,
 boost::recursive_wrapper<binop<op_or> >
> expr;

This expression needs to be propagated to multiple machines for real-time evaluation against inputs. These machines cannot be made to spend the necessary time for the initial parsing.

Is it possible to propagate the parsed expression in the boost::variant representation above via Boost Interprocess managed_mapped_file? I have tried doing this via a unique_ptr of the expression object and was able to write to a memory mapped file, but evaluation against this object on the other hand resulted in a segmentation fault.

Note that I have also tried boost serialization, which fails for very large expressions.

Looking forward to any advice around this.

Thanks!

user1474341
  • 125
  • 1
  • 12
  • The answer is yes. "just" parse into whatever can be in a managed mapped file (which is... a lot of options). It sounds like you don't need a mapped file (you could use `managed_external_buffer` or indeed your own bespoke buffer with custom types). Please include an example of what you're trying to parse/share, and what the goal is. (E.g. "My goal is to parse a large JSON file and store it in a map for efficient retrieval. Other machines can copy the parse result so they don't have to repeat the parsing process." Add extra requirements (like: handling mutations)) – sehe Feb 15 '23 at 11:45
  • @sehe Thanks for getting back. I am parsing large logical expressions using your excellent example here as a reference - https://stackoverflow.com/questions/8706356/boolean-expression-grammar-parser-in-c . Thus, I need to be able to save the parsed result which is a boost::variant with boost recursive wrappers into the memory mapped file. Would that be possible? – user1474341 Feb 15 '23 at 15:47
  • Please edit the question with the relevant context. I'm sceptic that avoiding parsing actually helps performance. I'd guess that sharing the expressions in text form is more effective (and actually the point of the text representation in the first place!). I'll have a look whether I see a technical path to what you describe regardless. – sehe Feb 15 '23 at 21:57
  • Thanks @sehe. I have updated the question - please let me know if more context is needed. – user1474341 Feb 15 '23 at 22:59
  • "Note that I have also tried boost serialization, which fails for very large expressions" - erm how _large_ do your expressions get? (do you have an example?) – sehe Feb 16 '23 at 01:01
  • For the purpose of test, I am using a random expression generator of the form ( ( 1 and 2) or (3 and 4) or (5 and 6) ) up to a pre-defined recursion depth. The serialization works up to a depth of ~ 2700, beyond that it fails. – user1474341 Feb 16 '23 at 17:09
  • I'm sure you can shoot me an example (gist or pastebin) that demonstrates "too large for serialization"? – sehe Feb 16 '23 at 21:27
  • https://coliru.stacked-crooked.com/a/20390d57118c4f33 - This is a test program where you can see that the serialization fails as the expression depth increases to ~ 2900 - probably due to stack overflow on the recursive_wrapper – user1474341 Feb 16 '23 at 23:14
  • Hah. I **just** generated one myself http://stackoverflow-sehe.s3.amazonaws.com/66371383-1466-4a17-b97c-3c6e8cde65a6/input.txt. That's at a recursion of mere 7 though :) – sehe Feb 16 '23 at 23:14
  • 1
    Ah! Apologies - I was a bit late in sending the example.. – user1474341 Feb 16 '23 at 23:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251939/discussion-between-sehe-and-user1474341). – sehe Feb 16 '23 at 23:19

0 Answers0