Parsing BOOST SPIRIT - creating the correct AST tree

I need to analyze with Boost Spirit a sequence, for example

t1 JOIN t2 JOIN t3 JOIN ... JOIN tn

      

and the result should be an AST tree with semantics

((...((t1 JOIN t2) JOIN t3) JOIN ...) JOIN tn)

      

I tried using a rule like:

source = singleTable | (source >> JOIN >> singleTable);

      

but according to Boost Spirit's construction, the parsing process only uses the first part of the rule and parses only the first element ("t1") from the expression and leaves the rest of the unparsed sequence ("JOIN t2 JOIN t3 JOIN ... JOIN tn").

What is the best way to solve this problem?

I can rewrite the rule as

source = (singleTable >> JOIN >> source) | singleTable;

      

but in this case the created AST will look like

(t1 JOIN (t2 JOIN (t3 JOIN (... JOIN tn)...))).

      

Therefore, I will need an additional processing step to get the AST in the desired form.

Is there any other method that provides the correct AST after parsing?

+3


source to share


1 answer


* Beyond the Database Engines do not blindly create their AST. Most likely they are probably creating an unordered list of row sources ((appended to) to tables / views) and let the query optimizer figure out how to optimally schedule execution.

Barring the best example of your real AST, here's a rule that comes close:



 singleTable >> - ("JOIN" >> source)

      

+1


source







All Articles