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 5976989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:18:23+00:00 2026-05-22T21:18:23+00:00

I am building a expression analyser from which I would like to generate database

  • 0

I am building a expression analyser from which I would like to generate database query code, I’ve gotten quite far but am stuck parsing BinaryExpressions accurately. It’s quite easy to break them up into Left and Right but I need to detect parenthesis and generate my code accordingly and I cannot see how to do this.

An example [please ignore the flawed logic :)]:

a => a.Line2 != "1" && (a.Line2 == "a" || a.Line2 != "b") && !a.Line1.EndsWith("a")

I need to detect the ‘set’ in the middle and preserve their grouping but I cannot see any difference in the expression to a normal BinaryExpression during parsing (I would hate to check the string representation for parenthesis)

Any help would be appreciated.

(I should probably mention that I’m using C#)

–Edit–
I failed to mention that I’m using the standard .Net Expression classes to build the expressions (System.Linq.Expressions namespace)

–Edit2–
Ok I’m not parsing text into code, I’m parsing code into text. So my Parser class has a method like this:

void FilterWith<T>(Expression<Func<T, bool>> filterExpression);

which allows you to write code like this:

FilterWith<Customer>(c => c.Name =="asd" && c.Surname == "qwe");

which is quite easy to parse using the standard .Net classes, my challenge is parsing this expression:

FilterWith<Customer>(c => c.Name == "asd" && (c.Surname == "qwe" && c.Status == 1) && !c.Disabled)

my challenge is to keep the expressions between parenthesis as a single set. The .Net classes correctly splits the parenthesis parts from the others but gives no indication that it is a set due to the parenthesis.

  • 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-05-22T21:18:24+00:00Added an answer on May 22, 2026 at 9:18 pm

    I haven’t used Expression myself, but if it works anything like any other AST, then the problem is easier to solve than you make it out to be. As another commentor pointed out, just put parentheses around all of your binary expressions and then you won’t have to worry about order of operations issues.

    Alternatively, you could check to see if the expression you are generating is at a lower precedence than the containing expression and if so, put parenthesis around it. So if you have a tree like this [* 4 [+ 5 6]] (where tree nodes are represented recursively as [node left-subtree right-subtree]), you would know when writing out the [+ 4 5] tree that it was contained inside a * operation, which is higher precedence than a + operation and thus requires than any of its immediate subtrees be placed in parentheses. The pseudo-code could be something like this:

    function parseBinary(node) {
        if(node.left.operator.precedence < node.operator.precedence)
            write "(" + parseBinary(node.left) + ")"
        else
            write parseBinary(node.left)
        write node.operator
        // and now do the same thing for node.right as you did for node.left above    
    }
    

    You’ll need to have a table of precedence for the various operators, and a way to get at the operator itself to find out what it is and thence what its precedence is. However, I imagine you can figure that part out.

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

Sidebar

Related Questions

This code works fine in C#: Expression.Lambda(LambdaBody); But none of the methods for building
Im having problems building a query with the linq to sql data query expression
I'm building an expression tree using discriminated unions. The below code: type IntExpression =
I'm building a LINQ expression tree but it won't compile because allegedly the local
I am building a report in reporting services. I would like the title of
I'm, quite frankly, completely clueless about Regular expression, more so building them. I am
I use the (query-replace-regexp from to) expression regularly when making large changes in a
I'm building a ExpressionConverter that allows me to convert expressions of type Expression<Func<A1, B1,
I am building a conditional formatting feature, where the user can enter an expression
I created a lambda expression inside my std::for_each call. In it there is code

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.