6

我试图在我的 Windows 机器上编译 Pyparsing,但出现以下错误:

python setup.py build_ext --inplace
running build_ext
cythoning pyparsing.pyx to pyparsing.c

Error compiling Cython file:
------------------------------------------------------------
...
       If C{include} is set to true, the matched expression is also parsed (the
skipped text
       and matched expression are returned as a 2-element list).  The C{ignore}
       argument is used to define grammars (typically quoted strings and comment
s) that
       might contain false matches.
    """
    def __init__( self, other, include=False, ignore=None, failOn=None ):
                              ^
------------------------------------------------------------

pyparsing.pyx:2764:31: Expected an identifier, found 'include'

Error compiling Cython file:
------------------------------------------------------------
...
    def __init__( self, other, include=False, ignore=None, failOn=None ):
        super( SkipTo, self ).__init__( other )
        self.ignoreExpr = ignore
        self.mayReturnEmpty = True
        self.mayIndexError = False
        self.includeMatch = include
                           ^
------------------------------------------------------------

pyparsing.pyx:2769:28: Expected an identifier or literal
building 'pyparsing' extension
creating build
creating build\temp.win32-2.7
creating build\temp.win32-2.7\Release
C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe /c /nologo /Ox /MD /W
3 /GS- /DNDEBUG -IC:\Python27\include -IC:\Python27\PC /Tcpyparsing.c /Fobuild\t
emp.win32-2.7\Release\pyparsing.obj
pyparsing.c
pyparsing.c(1) : fatal error C1189: #error :  Do not use this file, it is the re
sult of a failed Cython compilation.
error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe"' fa
iled with exit status 2

我使用 Microsoft Visual C++ 2008 Express 版本进行了编译,使用的 Pyparsing 模块是最新版本。请问,有谁知道如何使这项工作?

4

2 回答 2

5

先生,我按照您说的做了,但仍然有编译时警告:

    python setup.py build_ext --inplace
    运行 build_ext
    cythoning pyparsing.pyx 到 pyparsing.c
    警告:pyparsing.pyx:413:8:无法访问的代码
    构建“pyparsing”扩展
    创建构建
    创建 build\temp.win32-2.7
    创建 build\temp.win32-2.7\Release
    C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe /c /nologo /Ox /MD /W
    3 /GS- /DNDEBUG -IC:\Python27\include -IC:\Python27\PC /Tcpyparsing.c /Fobuild\t
    emp.win32-2.7\Release\pyparsing.obj
    pyparsing.c
    C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe /DLL /nologo /INCRE
    MENTAL:NO /LIBPATH:C:\Python27\libs /LIBPATH:C:\Python27\PCbuild /EXPORT:initpyp
    arsing build\temp.win32-2.7\Release\pyparsing.obj /OUT:C:\Users\iProsper\Desktop
    \pyparsing\pyparsing.pyd /IMPLIB:build\temp.win32-2.7\Release\pyparsing.lib /MAN
    IFESTFILE:build\temp.win32-2.7\Release\pyparsing.pyd.manifest
       创建库 build\temp.win32-2.7\Release\pyparsing.lib 和对象 build\
    temp.win32-2.7\Release\pyparsing.exp
    C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\mt.exe -nologo -manifest build
    \temp.win32-2.7\Release\pyparsing.pyd.manifest -outputresource:C:\Users\iProsper
    \桌面\pyparsing\pyparsing.pyd;2

警告实际上在第 4 行:warning: pyparsing.pyx:413:8: Unreachable code。然后我将行更改为:

    def __getattr__(自我,姓名):
            如果为真:#name 不在 self.__slots__ 中:
                如果 self.__tokdict 中的名称:
                    如果名称不在 self.__accumNames 中:
                        返回 self.__tokdict[名称][-1][0]
                    别的:
                        return ParseResults([ v[0] for v in self.__tokdict[name] ])
                别的:
                    返回 ””
            别的:
                返回无

然后重新编译它。我注意到的一件事是,即使有警告和我所做的编辑,它们都编译成功,但是当我尝试使用以下代码对其进行测试时:

    从 pyparsing 导入 Word,alphas
    Greet = Word(alphas) + ',' + Word(alphas) + '!'
    greeting = greet.parseString('Hello, World!')
    打印问候语

我收到以下错误:

    回溯(最近一次通话最后):
      文件“C:\nwaomachux\build_pyparsing\checkout.py”,第 1 行,在
        从 pyparsing 导入 Word,alphas
      文件“pyparsing.pyx”,第 3414 行,在 init pyparsing (pyparsing.c:100064)
    AttributeError:“builtin_function_or_method”对象没有属性“ANY_VALUE”

我会进一步更改文件并将其发送给您检查它,但我只使用 Pyparsing 几个小时,所以我还没有掌握完整的源代码。

我不知道将withAttribute.ANY_VALUE = object()移动到def withAttribute( args, *attrDict):函数开头的效果是什么。请问,有什么办法解决吗?谢谢。

于 2013-11-15T08:07:33.060 回答
4

我怀疑问题是使用“包含”作为命名参数,因为include它是保留的 C 关键字。尝试编辑此__init__例程以:

def __init__( self, other, include_=False, ignore=None, failOn=None ):
    super( SkipTo, self ).__init__( other )
    self.ignoreExpr = ignore
    self.mayReturnEmpty = True
    self.mayIndexError = False
    self.includeMatch = include_
    self.asList = False
    if failOn is not None and isinstance(failOn, basestring):
        self.failOn = Literal(failOn)
    else:
        self.failOn = failOn
    self.errmsg = "No match found for "+_ustr(self.expr)

也就是说,只需在参数名称、参数列表和引用它的一行中添加一个“_”。这将使您的 Cython 编译的 pyparsing 与标准版本略有不同,但我没有看到大量使用此参数。

祝你好运,请发布到pyparsing wiki 主页讨论您的 Cython 化体验,以及有关这可能如何加快您的 pyparsing 过程的任何信息。

于 2013-11-14T13:28:16.107 回答