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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:19:07+00:00 2026-06-10T03:19:07+00:00

I need to parse a file that contains conditional statements, sometimes nested inside one

  • 0

I need to parse a file that contains conditional statements, sometimes nested inside one another.

I have a file that stores configuration data but the configuration data is slightly different depending on user defined options. I can deal with the conditional statements, they’re all just booleans with no operations but I don’t know how to recursively evaluate the nested conditionals. For instance, a piece of the file might look like:

...
#if CELSIUS
    #if FROM_KELVIN ; this is a comment about converting kelvin to celsius.
        temp_conversion = 1, 273
    #else
        temp_conversion = 0.556, -32
    #endif
#else
    #if FROM_KELVIN
        temp_conversion = 1.8, -255.3
    #else
        temp_conversion = 1.8, 17.778
    #endif
#endif
...

… Also, some conditionals don’t have an #else statement, just #if CONDITION statement(s) #endif.

I realize that this could be easy if the file were just written in XML or something else with a nice parser to begin with, but this is what I have to work with so I’m wondering if there’s any relatively simple way to parse this file. It’s similar to parenthesis matching so I imagine there would be some module for it but I haven’t found anything.

I’m working in python but I can switch for this function if it’s easier to solve this in another language.

  • 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-10T03:19:09+00:00Added an answer on June 10, 2026 at 3:19 am

    Since all of the conditions are binary and I know the values of all of them in advance (no need to evaluate them in order in order like a programming language), i was able to do it with a regular expression. This works better for me. It finds the lowest level conditionals (ones with no nested conditions), evaluates them and replaces them with the correct contents. Then repeats for the higher level conditionals and so on.

    import re
    
    conditions = ['CELSIUS', 'FROM_KELVIN']
    
    def eval_conditional(matchobj):
        statement = matchobj.groups()[1].split('#else')
        statement.append('') # in case there was no else statement
        if matchobj.groups()[0] in conditions: return statement[0]
        else: return statement[1]
    
    def parse(text):
        pattern = r'#if\s*(\S*)\s*((?:.(?!#if|#endif))*.)#endif'
        regex = re.compile(pattern, re.DOTALL)
        while True:
            if not regex.search(text): break
            text = regex.sub(eval_conditional, text)
        return text
    
    if __name__ == '__main__':
        i = open('input.txt', 'r').readlines()
        g = ''.join([x.split(';')[0] for x in i if x.strip()])
        o = parse(g)
        open('output.txt', 'w').write(o)
    

    Given the input in the original post, it outputs:

    ...
            temp_conversion = 1, 273
    
    ...
    

    which is what I need. Thanks to everyone for their responses, I really appreciate the help!

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

Sidebar

Related Questions

I have a ~2 MB html file that I need to parse that contains
I wanted to parse a text file that contains unstructured text. I need to
I have a file that I need to parse into an array but I
I have a 100Mb file with roughly 10million lines that I need to parse
I have a very large XML file that I need to parse so I
I have a csv file that contains a location field. I need to import
I need to parse a file that contains various XML files, i.e., <xml></xml> <xml></xml>
I have a Jar file named OuterJar.jar that contains another jar named InnerJar.jar this
The setup: I have a standard .php file (index.php) that contains two includes, one
I have a PDF file, which contains data that we need to import into

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.