Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top