使用pyparsing解析表达式列表 [英] Parse a list of expressions using pyparsing
问题描述
我正在尝试使用pyparsing解析简单的基本程序:
I'm trying to use pyparsing to parse simple basic program:
import pyparsing as pp
pp.ParserElement.setDefaultWhitespaceChars(" \t")
EOL = pp.LineEnd().suppress()
# Identifiers is a string + optional $
identifier = pp.Combine(pp.Word(pp.alphas) + pp.Optional("$"))
# Literals (number or double quoted string)
literal = pp.pyparsing_common.number | pp.dblQuotedString
line_number = pp.pyparsing_common.integer
function = pp.Forward()
operand = function | identifier | literal
expression = pp.infixNotation(operand, [
(pp.oneOf("* / %"), 2, pp.opAssoc.LEFT),
(pp.oneOf("+ -"), 2, pp.opAssoc.LEFT),
])
assignment = identifier + "=" + expression
# Keywords
PRINT = pp.CaselessKeyword("print")
FOR = pp.CaselessKeyword("for")
TO = pp.CaselessKeyword("to")
STEP = pp.CaselessKeyword("step")
NEXT = pp.CaselessKeyword("next")
CHRS = pp.CaselessKeyword("chr$")
statement = pp.Forward()
print_stmt = PRINT + pp.ZeroOrMore(expression | ";")
for_stmt = FOR + assignment + TO + expression + pp.Optional(STEP + expression)
next_stmt = NEXT
chrs_fn = CHRS + "(" + expression + ")"
function <<= chrs_fn
statement <<= print_stmt | for_stmt | next_stmt | assignment
code_line = pp.Group(line_number + statement + EOL)
program = pp.ZeroOrMore(code_line)
test = """\
10 print 123;
20 print 234; 567;
25 next
30 print 890
"""
print(program.parseString(test).dump())
除了print子句,我还有其他所有工作.
I do have everything else working except the print clause.
这是输出:
[[10, 'print', 123, ';', 20, 'print', 234, ';', 567, ';', 25, 'next', 30, 'print', 890]]
[0]:
[10, 'print', 123, ';', 20, 'print', 234, ';', 567, ';', 25, 'next', 30, 'print', 890]
基于一些建议,我修改了解析器,但是由于某种原因,sitll解析器泄漏到下一行.
Based on some suggetion I modified my parser but sitll for some reason parser leaks to next row.
如何定义要正确打印的项目列表?
How to define list of items for printing properly?
推荐答案
这里发生了一件微妙的事情,使用 ParserElement.setDefaultWhitespaceChars
时,它具有更好的文档记录.这只会更新在 之后调用 setDefaultWhitespaceChars
创建的表达式的pyparsing空格字符.像 dblQuotedString
这样的内置表达式和 pyparsing_common
中的表达式都是在导入时定义的,因此可以跳过一组标准的空白字符,其中包括'\ n'.如果使用 expr.copy()
或仅使用普通的 expr()
创建它们的新副本,则将获得使用已更新的空白字符的新表达式.
There is a subtle thing going on here, and it bears better documenting when using ParserElement.setDefaultWhitespaceChars
. This only updates the whitespace characters for pyparsing expressions created after calling setDefaultWhitespaceChars
. The built-in expressions like dblQuotedString
and the expressions in pyparsing_common
are all defined at import time, and so get the standard set of whitespace characters to skip, which includes '\n'. If you create new copies of them using expr.copy()
or just plain expr()
, you will get new expressions, which use the updated whitespace characters.
更改:
literal = pp.pyparsing_common.number | pp.dblQuotedString
line_number = pp.pyparsing_common.integer
收件人:
literal = pp.pyparsing_common.number() | pp.dblQuotedString()
line_number = pp.pyparsing_common.integer()
我认为您的泄漏问题将得到解决.
And I think your leakage issues will be resolved.
这篇关于使用pyparsing解析表达式列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!