我正在使用蜡眼解析器生成器为某些语法生成 python 解析器。不幸的是,当我尝试解析任何文本时,几乎每次都会遇到相同的错误:
parse error: failed to match 'whitespace' at line=1, col=10, pos=10
或者
parse error: failed to match 'ws' at line=1, col=10, pos=10
我想这是我对语法的定义有问题,但我几乎尝试了所有可以弥补的方法,但仍然是错误的。
这是python代码:
import waxeye
import robLang
text = ' block is red '
def analyze(input):
ast = robLang.Parser().parse(input)
if isinstance(ast, waxeye.ParseError):
return ast
else:
return sum(ast.children[0])
result = analyze(text)
print result
和语法定义:
statement <- ws thing ws isWord wse adjective ws ?(adjectives)
thing <- objectType ?(ws name)
objectType <- 'pyramid'|'ball'|'block'|'boot'|'leg'|'degree'
name <- [a-zA-Z] *[a-zA-Z0-9_-]
isWord <- 'is'
adjectives <- *(adjective ws)
colour <- 'red'|'green'|'blue'|'black'|'white'|'yellow'|'orange'|'purple'
|'cyan'|'magenta'
size <- 'small'|'big'
moralDirection <- 'good'|'bad'
adjective <- colour|size|moralDirection
number <- +[0-9] ?('.' +[0-9]) ws
ws <: *[ \t\n\r]