c++ - Construct logic statements through parsing a file -


i have been presented problem of reading input file contains logic statements, , required construct truth table determine whether ask matches any/all models determined. example of data may expect read in is:

(p & z => x) => ((p | d) & z)

please don't caught in example , whether makes sense, made show different compositions may presented with. multiple such statements can separated semicolons.

i have sorted out semicolon splitting without dramas, , have vector of strings containing each separate statement, each string presented above. without parenthesis being involved, believe determining statements rather straight forward, involvement required compute different sections before others. eg:

(p | d) = result , (result & x)

i have seen people discussing concept of using stack determine if open brackets closed properly, not believe appropriate situation not allow me determine statements inside set of parenthesis.

the current idea have use stack idea, , try determine "depth" of statement (essentially how far nested) , mark number each statement, believe sounds inelegant solution. have tips how should construct algorithm address problem?

you need build tree of expressions, variables leaves.

your expression become:

        =>        / \       /   \      /     \     =>      &    / \     / \   &   x   |   z  / \     / \ p   z   p   d 

once have built kind of representation, evaluation straightforward.

another approach, same result, reducing expression rpn (where can use stack idea):

p, z, &, x, =>, p, d, |, z, &, => 

as suggested in comments, can shunting yard algorithm.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -