Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7908327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:53:22+00:00 2026-06-03T11:53:22+00:00

A have a simple grammar written in yacc, which builds a tree from logical

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:53:24+00:00Added an answer on June 3, 2026 at 11:53 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple ANTLR grammar, which I have stripped down to its bare
I have simple SSIS package which reads data from flat file and insert into
I have a simple grammar consisting of mixed variables ( $(name) ) and variable-value
I have this grammar to match simple logical predicates in ANTLR. exp : or
I have an ANTLR grammar which creates an AST, and then I have written
I have a simple grammar which allows the user to define some objects with
I have written grammar for a language (sample code below) //this is a procedure
I have this simple grammar for a C# like syntax. I can't figure out
I have a simple grammar: grammar sample; options { output = AST; } assignment
I have some problems with a very simple yacc/lex program. I have maybe forgotten

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.