python - 尝试在 Python 中解析旧语法
问题描述
我们有一个旧应用程序需要迁移到 AWS 中的新框架。在旧应用程序中,我们曾经有一些基于表达式的语法,用于识别客户端的权限、到期日期等。
我正在尝试将旧语法转换为逗号分隔语法的新语法。
我正在尝试使用pyparsing
库来实现这一点,但我觉得我在这里碰壁了。到目前为止,下面的代码给了我list
旧代码的细分,但是当旧代码中有嵌套循环时,我无法解析它。
旧代码
If (LAST_RUN_DATE>=dat('JUL 01, 90'))
If (pos(con('*',SUB_CODE,'*'),'*ABC*DEF*ASD*WQR*')>=1)
Calculate Client as 1
End If
End If
Python 转换
in_string = '''If (LAST_RUN_DATE>=dat('JUL 01, 90'))
If (pos(con('*',SUB_CODE,'*'),'*ABC*DEF*ASD*WQR*')>=1)
Calculate Client as 1
End If
End If'''
from pyparsing import *
#- define basic punctuation and data types
LBRACE,RBRACE,LPAREN,RPAREN,SEMI = map(Suppress,"{}();")
IF = Keyword("If")
END_IF = Keyword("End If")
after_if = Regex(r'(.*?)\n')
_if = Forward()
#- _if << Group(IF + Group(after_if))
_if << Group(Group(ZeroOrMore(IF)) + Group(ZeroOrMore(after_if)) + Group(ZeroOrMore(END_IF)))
#- parse the sample text
result = _if.parseString(in_string)
#- print out the tokens as a nice indented list using pprint
from pprint import pprint
pprint(result.asList())
### Output
[[['If'],
["(LAST_RUN_DATE>=dat('JUL 01, 90'))\n",
"If (pos(con('*',SUB_CODE,'*'),'*ABC*DEF*ASD*WQR*')>=1)\n",
'Calculate Client as 1\n',
'End If\n'],
['End If']]]
我已经参考了这个,也期待类似于这个的输出。 关联
解决方案
使用 Forward 绝对是在正确的轨道上。你要去的地方是你试图在 ZeroOrMore 中实现的地方,这是 Forward 的递归将为你做的事情。
代替:
_if << Group(Group(ZeroOrMore(IF)) + Group(ZeroOrMore(after_if)) + Group(ZeroOrMore(END_IF)))
和:
_if << Group(IF + after_if + Group(ZeroOrMore(_if | after_if)) + END_IF)
您还必须小心不要将 END_IF 视为 after_if。这将做到:
after_if = ~END_IF + Regex(r'(.*?)\n')
通过这些更改,我得到:
[['If',
"(LAST_RUN_DATE>=dat('JUL 01, 90'))\n",
[['If',
"(pos(con('*',SUB_CODE,'*'),'*ABC*DEF*ASD*WQR*')>=1)\n",
['Calculate Client as 1\n'],
'End If']],
'End If']]
您还可以考虑对after_if
vs 非 if 语句(目前都被视为 after_ifs)更明确一点:
condition = originalTextFor(nestedExpr("(", ")"))
_if << Group(IF + condition + Group(ZeroOrMore(_if | after_if)) + END_IF)
如果您的语法允许 if 条件跨越换行符。
另外,请查看 Yelp! 的转换代码的undebt项目(基于 pyparsing)。
推荐阅读
- ethereum - 使用以太坊节点获取所有新生成的 ERC721 合约
- youtube-api - 有没有办法获取 YouTube 用户的观看时间或观看历史记录?
- python - 如何修复 pygame 背景不工作?
- mysql - 带有连接和条件的 MYSQL SUM 字段
- python - 如何使用 PDFPlumber 从两列 PDF 中提取文本
- pandas - 在指定列上计算一次均值、n、min、max 和 stdev
- r - ggplot2 水平绘制 x 轴和 y 轴
- javascript - 为什么对自身使用 Array.prototype.push() 会产生无限嵌套的数组?
- python - Python,Tkinter,使用子进程 Popen,运行外部程序
- keras - keras 自定义连接和激活函数可训练参数