構文解析!part 3

新しく、True or False機能もつけました。
まだ”==”文しか対応してませんが、我ながらいい出来です。

Source Code:

def factor(begin):
  global SyntaxFlag
  global i
  val = 0

  if begin[i] == "(":
    i+=1
    val = expression(begin)
    i+=1

  while begin[i].isdigit():
    SyntaxFlag=0
    val *= 10
    val += int(begin[i])
    i+=1
  return val

def mul(begin):
  
  global i
  val = factor(begin)

  while True:
    if begin[i] is "*":
      if begin[i+1] is "*":
        i += 2
        val **= mul(begin)#factor(begin)
      else:
        return val
    else:
      break
  return val 

def term(begin):
  
  global i
  val = mul(begin) #factor(begin)

  while True:
    if begin[i] is "*":
      i += 1
      val *= mul(begin)
    elif begin[i] is "/":
      i += 1
      val2 = mul(begin)
      if val < 0 or val2 < 0:
        val = func1(str(abs2(val,val2)))
      else:
        val /= 1.0*val2
        val = func1(str(val))
    elif begin[i] is "%":
      i += 1
      val %= mul(begin)
    else:
      break

  return val

def expression(begin):

  global i
  val = term(begin)

  while True:
    if begin[i] is "+":
      i += 1
      val += expression(begin)
    elif begin[i] is "-":
      i += 1
      val -= expression(begin)
    else:
      break
  return val

def judge(begin):

  global i
  stack =[]
  while begin[i] != "~":
    val = expression(begin)

    if begin[i] == "=":
      if begin[i+1] == "=":
        i+=2
        stack.append(val)
    else:
      stack.append(val)
      break
  if len(stack)==1:
    return val
  else:
    return True if  sum(stack)*1.0/len(stack)==stack[0] else False 


#-3/2 == -2
abs2 = lambda x,y: int(x*1.0/y)
func1 = lambda x: float(x[0:x.index('.')+3])
def main():
  while True:
    try:
      ex=""
      global SyntaxFlag
      SyntaxFlag=1
      tmp = (raw_input(">>")+"~").split()
      for j in tmp: ex+=j
      if True in [ex == x for x in ['exit~','quit~']]:
        print "End..." 
        exit(0)
      elif ex=="~": continue
      global i
      i = 0
      #print expression(ex)
      result= judge(ex)
      if SyntaxFlag:
        raise SyntaxError,'WTF are u "d"oin!!?'
      else:
        print(result) 
    except EOFError:
      break

if __name__ == '__main__':
  main()

Test Case:

1+1
123+321
100-50
100-123
123*3
8/2
100/3
(10+5)*2
(5-10)*3
(100/2)+(100/3)-(11*3)
2**3
(10**3)/1000
10**3/10**3
2**3**3
(2**3)**3
(  2 **     3) ** 3
1    + 3
3  ** 3
16%16
16%15
10*3==30
3**2+4**2 == 5**2
23421%3==43*2
2**3/4 == 10/5%2
HOGEHOGEWORLD

Response:

>>2
>>444
>>50
>>-23
>>369
>>4.0
>>33.33
>>30
>>-15
>>50.33
>>8
>>1.0
>>1.0
>>134217728
>>512
>>512
>>4
>>27
>>0
>>1
>>True
>>True
>>False
>>False
>>Traceback (most recent call last):
  File "ex2.py", line 122, in <module>
    main()
  File "ex2.py", line 115, in main
    raise SyntaxError,'WTF are u "d"oin!!?'
SyntaxError: WTF are u "d"oin!!?

ですねー。
次は"!="機能もつけます。はい。

広告を非表示にする