-1

I am trying to write a parser which takes expressions as a input from file.

expressions can be A=B=10 or B=(C-A)-4 etc.

What i have tried so far is . I am reading a file IP.txt

import re

opert = '+-/*()_='
fileName = "input.txt"
f = open(fileName,'r')

variableDict = {}
lines = f.readlines()

for i in lines:

    for x in  re.finditer(r'[A-Z_]\w*', i):
        print x.group() # prints list containing all the alphabets.

    for z in  re.finditer(r'[0-9]\d*', i):
        print z.group() # prints list containing all the numbers.

    for c in i:
        if c in opert:
            print c # prints all the operators.

   # '_' has special meaning. '_' can only be used before numbers only like _1 or _12 etc
   #And i have parsed this also using
       print re.findall(r'[_][0-9]\d+',i) # prints the _digits combination.

Now the problem is i have struck at how should i proceed with expression evaluation. First some rule which i must mention about above inputs are. No line should be greater then 50 characters. Left most operator will always be '=' assignment operator. '=' always Preceded by variables[A-Z],operators are {'+','-','/','*','_'}, digits {0-9}.

How should i first extract the first variable then push it into python list then '=' operator,then either '(','A-Z' push it into stack and so on

Could someone help me with this problem. I am overwhelmed with problem..

If any one is not able to understand the description please goto this link

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Vaibhav Jain
  • 5,287
  • 10
  • 54
  • 114

1 Answers1

1

So, you asked about the stack problem, which of course you need for evaluation. I would do something like this:

import re #1
stack = [] #2 FIX: NOT NECESSARY (since fourth line returns a list anyway)
inputstr = "A=B=C+26-(23*_2 )-D" #3

stack =  re.findall(r'(?:[A-Z])|(?:[0-9]+)|(?:[/*+_=\(\)-])', inputstr) #4

while len(stack): #5
    print stack.pop() #6

First three lines are some init stuff only. After that, I would make a stack with regex in the fourth line. (?:[A-Z]) matches variable, (?:[0-9]+) matches number (which may have more than one digit) and (?:[/*+_=\(\)-]) matches all the operators. Braces are escaped, and - is on the end, so you don't have to escape it.

Fifth and sixth line prints the stack.

I used (?: ...) because I don't want to match either group. Hard to explain - just try to run it without ?: and you will see the effect.

ceruleus
  • 704
  • 3
  • 12
  • Thanks man i am trying your method..i hope it will solve my issue..Thanks again.. – Vaibhav Jain Mar 28 '13 at 13:38
  • You don't need to assign `stack` beforehand... Also, just explaining what the regex is would have been enough. – pradyunsg Mar 28 '13 at 13:47
  • You are right about line two - I'll fix it now. Of course I'm explaining regex that is enough - because with that regex he can make a stack for evaluation in only one line of the code. And whole the meaning of that line is actually that regex (rest of the code is not crucial for the functionality). – ceruleus Mar 28 '13 at 13:59