【发布时间】:2017-11-16 16:40:10
【问题描述】:
我正在编写代码来解析基本计算机指令行。我的输入字符串是这样的ADD(input1,input2) DEL(input3), SUB(input1,input2) INS(input3)
我期待这样的结果:
<line>
<instruction>
<type>ADD</type>
<args>
<ITEM>input1</ITEM>
<ITEM>input2</ITEM>
</args>
</instruction>
<instruction>
<type>DEL</type>
<args>
<ITEM>input3</ITEM>
</args>
</instruction>
</line>
<line>
<instruction>
<type>SUB</type>
<args>
<ITEM>input1</ITEM>
<ITEM>input2</ITEM>
</args>
</instruction>
<instruction>
<type>INS</type>
<args>
<ITEM>input3</ITEM>
</args>
</instruction>
</line>
我的实际结果具有我正在寻找的一般结构,但是行和指令解析器似乎在错误的位置匹配,或者标签出现在错误的位置。
实际结果:
<line>
<line>
<instruction>
<type>ADD</type>
<args>
<ITEM>input1</ITEM>
<ITEM>input2</ITEM>
</args>
</instruction>
<instruction>
<type>DEL</type>
<args>
<ITEM>input3</ITEM>
</args>
</instruction>
</line>
<instruction>
<instruction>
<type>SUB</type>
<args>
<ITEM>input1</ITEM>
<ITEM>input2</ITEM>
</args>
</instruction>
<instruction>
<type>INS</type>
<args>
<ITEM>input3</ITEM>
</args>
</instruction>
</instruction>
</line>
结果转储
[[['OTE', ['output1']]], [['XIO', ['input2']], ['OTE', ['output2']]]]
- branch: [[['OTE', ['output1']]], [['XIO', ['input2']], ['OTE', ['output2']]]]
[0]:
[['OTE', ['output1']]]
- instruction: ['OTE', ['output1']]
- args: ['output1']
- type: 'OTE'
[1]:
[['XIO', ['input2']], ['OTE', ['output2']]]
- instruction: ['OTE', ['output2']]
- args: ['output2']
- type: 'OTE'
由于某种原因,line 匹配整个结构,第二行指令作为单个指令组匹配。我尝试在instruction 行上使用.setDebug() 函数,但是我不确定如何解释输出。我不明白为什么最后一个 line 应该作为指令匹配,因为它不遵循 Word(Word) 模式。
我的代码:
#!python3
from pyparsing import nestedExpr,alphas,Word,Literal,OneOrMore,alphanums,delimitedList,Group,Forward
theInput = r"ADD(input1,input2) DEL(input3), SUB(input1,input2) INS(input3)"
instructionType = Word(alphanums+"_")("type")
argument = Word(alphanums+"_[].")
arguments = Group(delimitedList(argument))("args")
instruction = Group(instructionType + Literal("(").suppress() + arguments + Literal(")").suppress())("instruction")
line = (delimitedList(Group(OneOrMore(instruction))))("line")
parsedInput = line.parseString(theInput).asXML()
print(parsedInput)
调试输出:
Match Group:({W:(ABCD...) Suppress:("(") Group:(W:(ABCD...) [, W:(ABCD...)]...) Suppress:(")")}) at loc 0(1,1)
Matched Group:({W:(ABCD...) Suppress:("(") Group:(W:(ABCD...) [, W:(ABCD...)]...) Suppress:(")")}) -> [['ADD', ['input1', 'input2']]]
Match Group:({W:(ABCD...) Suppress:("(") Group:(W:(ABCD...) [, W:(ABCD...)]...) Suppress:(")")}) at loc 18(1,19)
Matched Group:({W:(ABCD...) Suppress:("(") Group:(W:(ABCD...) [, W:(ABCD...)]...) Suppress:(")")}) -> [['DEL', ['input3']]]
Match Group:({W:(ABCD...) Suppress:("(") Group:(W:(ABCD...) [, W:(ABCD...)]...) Suppress:(")")}) at loc 30(1,31)
Exception raised:Expected W:(ABCD...) (at char 30), (line:1, col:31)
Match Group:({W:(ABCD...) Suppress:("(") Group:(W:(ABCD...) [, W:(ABCD...)]...) Suppress:(")")}) at loc 32(1,33)
Matched Group:({W:(ABCD...) Suppress:("(") Group:(W:(ABCD...) [, W:(ABCD...)]...) Suppress:(")")}) -> [['SUB', ['input1', 'input2']]]
Match Group:({W:(ABCD...) Suppress:("(") Group:(W:(ABCD...) [, W:(ABCD...)]...) Suppress:(")")}) at loc 50(1,51)
Matched Group:({W:(ABCD...) Suppress:("(") Group:(W:(ABCD...) [, W:(ABCD...)]...) Suppress:(")")}) -> [['INS', ['input3']]]
Match Group:({W:(ABCD...) Suppress:("(") Group:(W:(ABCD...) [, W:(ABCD...)]...) Suppress:(")")}) at loc 62(1,63)
Exception raised:Expected W:(ABCD...) (at char 62), (line:1, col:63)
我做错了什么?
【问题讨论】:
-
不要使用
asXML打印结果,请使用dump。 -
@PaulMcG 感谢您的帮助!我已将转储添加到原始问题中。
-
:) 你做了
print(line.parseString(theInput).dump),你必须做print(line.parseString(theInput).dump()) -
@PaulMcG 嘿,哎呀。问题已更新。
标签: python-3.x pyparsing