gpt4 book ai didi

python - 解析 Latex : grammar, 递归下降的简单扩展,pyParsing?

转载 作者:太空宇宙 更新时间:2023-11-03 19:02:19 24 4
gpt4 key购买 nike

我想做一个 latex 语法的小扩展。
有一些纯 latex 方法可以避免这种解析练习,我知道它们。
本题的目标是解决以下解析问题。

If \ep is small                    --> If \epsilon is small  

\theorem --> \begin{theorem}
(tab) lorem ipsum --> (tab) lorem ipsum
(tab) lorem ipsum --> (tab) lorem ipsum
(no tab) Some text --> \end{theorem}
Some text

A function \oldFunction{x}{y} --> A function \newFunction{x}{y}

Some other text with latex construct like \frac{1}{2} (not part of the grammar)

所以我有一些关键字,例如 epoldFunction,我想转换为新关键字。
它们可以嵌套。

\oldFunction{\ep}{\ep}

我有一个“选项卡”一致的关键字,例如 theorem,其中包含内容。
该选项卡包含的按键可以嵌套。

\theorem  
(tab) \lemma
(tab) (tab) \oldFunction{\ep}{\ep}

此外,\ep\theorem 关键字可以混合使用,就像上一行一样。

然后,还有所有其他 latex 结构,我不碰它们,就留在那里。

我研究了 pyParsing 和 codeTalker .
codeTalker是上下文无关语法,我不知道我的describe语法是否是上下文无关的。
pyParsing 可以做到,我查看了文档,但我不明白如何应用它。
这是我第一次遇到解析问题。

最佳答案

看起来你根本可以不使用解析库。我在想:

newstuff = {r'\b\ep\b':r'\epsilon',r'\b\other\b':r'\notherthings'}
fixed = []
intheorem = False
for line in source:
for k,v in newstuff:
line = re.sub(k, v, line)
if not line.startswith('\t') and intheorem:
fixed.append('\end{theorem}')
intheorem = False
if line.startswith('\theorem')
line = '\begin{theorem}'
intheorem = True
fixed.append(line)
if intheorem:
fixed.append('\end{theorem}')

这有道理吗?在每一行中,对所有特殊名称进行正则表达式替换,并跟踪特殊“\theorem” block 的缩进。

关于python - 解析 Latex : grammar, 递归下降的简单扩展,pyParsing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15729778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com