A have a simple grammar written in yacc, which builds a tree from logical expressions, consisting of subexpressions, and AND (&&), OR (||), and NOT (!) operators. I will not provide the grammar itself here, because there is nothing special about it and it is similar to countless YACC tutorial examples.
However, I need to parse these logical expressions so that all the parentheses are expanded for NOT operator according to De Morgan’s laws. For example, I need to treat expression
!( ( A && B ) || C ) )
as
( !A || !B ) && !C
Is it possible to implement this by modifying existing yacc grammar?
It is possible to do this in YACC’s reduction action for the not (!) operator, while building ASTs. You can write any code you want in the semantic action. Instead of “blindly” assembling the tree node for not from its child (the usual code you’d find in such a reduction action), you could write code that inspected the child to see if had had the shape required, and if so, construct the de Morgan equivalent tree of each. The code to do this a bit messy because it has to climb up and down the tree, matching nodes and rearraning subtrees, but its just yuck and not bad. Note that De Morgan’s law arguably applies to children shaped both like ( A && B ) || C ) and ( A || B && C)), so you two major subcases to handle.
I agree with Len; you wouldn’t normally do this in the parser. Its purpose is to get you set up for more complicated activities, and unless DeMorganizing is the only purpose, you’ll need other code to process the AST in various was after parsing is complete, so why not leave all the processing until there?
Following that idea, my recent SO answer on eliminating configured-dead code simplifies symbolic boolean logic; it shows one way to transform boolean logic ASTs using pattern-matching source-to-source transformations. This approach avoids yucky tree inspecting/hacking code. It should be obvious how to write readable transforms that implement de morgans’ law with that technique (and in fact we have done son in the past).