2
我是Python新手,pyparsing
,我在做一個邏輯表達式求值器。評估WFF邏輯表達式時Pyparsing錯誤?
該公式必須是WFF。 WFF的BNF是:
<alpha set> ::= p | q | r | s | t | u | ...
(the arbitrary finite set of propositional variables)
<form> ::= <alpha set> | ¬<form> | (<form>V<form>) | (<form>^<form>)
| (<form> -> <form>) | (<form> <-> <form>)
我的代碼是:
'''
Created on 17/02/2012
@author: Juanjo
'''
from pyparsing import *
from string import lowercase
def fbf():
atom = Word(lowercase, max=1) #aphabet
op = oneOf('^ V => <=>') #Operators
identOp = oneOf('([ {')
identCl = oneOf(') ] }')
form = Forward() #Iniciar de manera recursiva
#Grammar:
form << ((Group(Literal('~') + form)) | (Group(identOp + form + op + form + identCl)) | (Group(identOp + form + identCl)) | (atom))
return form
entrada = raw_input("Input please: ") #userinput
print fbf().parseString(entrada)
問題是,當我使用這些表情:a^b
和aVb
。
解析器應該返回一個錯誤,但沒有錯誤;相反,它返回a
。其實,a
之後的任何符號都會被忽略。
這些形式的WFF版本是:(a^b)
和(aVb)
兩個正常工作。我認爲問題出在原子定義上。
我在做什麼錯?