0

I have written following code and it was working fine.But One of the case it is failing.I tried but not able to fix this issue.

#!/usr/bin/env py
import itertools
import sys
import sympy
import re
import pprint
def extract_next_state(s):
    p = re.compile('(\()|(\))')
    depth = 0
    startindex = None
    start_point = False
    for m in p.finditer(s):
        if m.group(1):          # (
            depth += 1
            print "depth (", depth
            if not start_point:
                startindex = m.start()
                start_point = True
        elif m.group(2):          # )
            depth -= 1
            print "depth )", depth
            if depth == 0:
                return s[startindex:m.end()]
    return s[startindex:] + ')' * depth

if __name__ == "__main__":
    #data = ['next_state=(~SE&((~B2&D)|(B2&lq))|(SE&SI))']
    #data = ['next_state=((~SE&((A1&A2)|(B1&B2)))|(SE&SI)))']
    #data = ['next_state=((((~SE&((A1&A2)|(B1&B2)))|(SE&SI)))']
    data = ['next_state=(D1&S&!SE)|(!S&(!SE&D0))|(SE&SI))']
    data_1 = data[0].split(',')
    com = None
    for item in data_1:
        if item.find('next_state=')!= -1:
            item_list = item.split('=')
            item_op = extract_next_state(item_list[1])
            print item_op

output:

(D1&S&!SE)

Expected :

(D1&S&!SE)|(!S&(!SE&D0))|(SE&SI)

10
  • 2
    Regular expressions can't be used for nested constructs, like for example parentheses inside parentheses. Commented Nov 15, 2013 at 9:18
  • @JoachimPileborg, that depends on the regex flavor. In PCRE it's as simple as: (?:[^()]|\((?R)\))*. Commented Nov 15, 2013 at 9:25
  • @Qtax I don't know if I would call that simple though. :) Commented Nov 15, 2013 at 9:27
  • Are you trying to make a logic parser? Why not use something like pyparsing and save yourself a load of hassle. I can post an answer with the code to do this if you want? Commented Nov 15, 2013 at 9:34
  • In very specific case, I need this. I am not trying to write any parser. Commented Nov 15, 2013 at 9:40

1 Answer 1

2

You checking for depth == 0 as condition for returning from extract_next_state(). That is extract_next_state() returns once the matching closing parenthesis to the first opening parenthesis is found. The rest of the string is then not checked for any further parenthesis of course.

It's hard to recommend a solution without knowing what rules for "next_state" or the grammar for allowd expressions. But from the last line extract_next_state it seems you wish to close any open parenthesis. So a possible solution may be:

def extract_next_state(s):
    p = re.compile('(\()|(\))')
    depth = 0
    startindex = None
    endindex = None
    for m in p.finditer(s):
        if m.group(1):          # (
            depth += 1
            print "depth (", depth
            if not startindex:
                startindex = m.start()
        elif m.group(2):          # )
            depth -= 1
            print "depth )", depth
            if depth == 0:
                endindex = m.end()
    return s[startindex:(endindex if depth == 0 else None)] + ')' * depth

If with the last closing parenthesis all pairs are matched, the rest of the string is discarded, else a matching number of closing parentheses will be added.

Sign up to request clarification or add additional context in comments.

1 Comment

sometime simple thing people make complicated like I did. accept your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.