python代码问题(关于lexeme)

时间:2014-10-23 03:28:07

标签: python python-3.x

我正在编写一个python项目,然后我遇到一些问题,我无法运行我的程序。下面的代码是关于词法分析器的我的程序:(这个程序我已经在网上提到其他人写了)

> import re
> 
> token_pattern = r"""
> 
> (?P<IDENTIFIER> [a-zA-Z_][a-zA-Z0-9_]*)
> 
> | (?P<INT_LITERAL> [0-9]+)
> 
> | (?P<SEMICOLON>\;)
> 
> | (?P<LIFT_PARENTHESIS>[(])
> 
> | (?P<RIGT_PARENTHESIS>[)])
> 
> | (?P<ASSIGN_OP>[=])
> 
> | (?P<ADD_OP>[+])
> 
> | (?P<SUB_OP>[=])
> 
> | (?P<DIV_OP>[/])
> 
> | (?P<MUL_OP>[*])
> 
> | (?P<LT_OP>[<])
> 
> | (?P<GT_OP>[>])
> 
> | (?P<LTE_OP>[<=])
> 
> | (?P<GTE_OP>[>=])
> 
> | (?P<EQ_OP>[==])
> 
> | (?P<NEQ_OP>[!=])
> 
> | (?P<LEFT_BRACE>[{])
> 
> | (?P<RIGHT_BRACE>[}]
> 
> | (?P<LEFT_BRACKET>[[])
> 
> | (?P<RIGHT_BRACKET>[]])
> 
> | (?P<OR_OP>[||])
> 
> | (?P<AND_OP>[&&]) """
> 
> token_re = re.compile(token_pattern, re.VERBOSE) /"I dont know what
> this line means. "/
> 
> class TokenizerException(Exception):pass
> 
> def tokenize(text):

pos =0

while True:

    m = token_re.match(text, pos)
    if not m:break
    pos = m.end()
    tokname=m.lastgroup
    tokvalue=m.group(tokname)
    yield tokname, tokvalue
if pos != len(text):
    raise TokenizerException('tokenizer stopped at pos %r of %r' % ( pos, len(text)))


stuff = r'property.{general.name}.ip=)'


print ('stuff'.center(60, '='))

for tok in tokenize(stuff):
    print (tok)

当我运行程序时,它显示:

Traceback (most recent call last):
  File "C:/Python33/project2.py", line 28, in <module>
    token_re = re.compile(token_pattern, re.VERBOSE)

  File "C:\Python33\lib\re.py", line 214, in compile
    return _compile(pattern, flags)

  File "C:\Python33\lib\re.py", line 283, in _compile
    p = sre_compile.compile(pattern, flags)

  File "C:\Python33\lib\sre_compile.py", line 491, in compile
    p = sre_parse.parse(p, flags)

  File "C:\Python33\lib\sre_parse.py", line 747, in parse
    p = _parse_sub(source, pattern, 0)

  File "C:\Python33\lib\sre_parse.py", line 359, in _parse_sub
    itemsappend(_parse(source, state))

  File "C:\Python33\lib\sre_parse.py", line 697, in _parse
    raise error("unbalanced parenthesis")
sre_constants.error: unbalanced parenthesis

如何完全运行它?

1 个答案:

答案 0 :(得分:2)

您必须分解您的模式以确定问题所在。一个简单的机制是将模式平分并继续进行,直到您识别出有问题的部分。

正则表达式引擎抱怨你有不平衡的parens。

它可能来自寻找parens的模式:

(?P<LIFT_PARENTHESIS>[(])

LIFT”和“RIGT”parens应该逃脱paren本身,因为它很重要。

改为使用:

(?P<LIFT_PARENTHESIS>[\(])