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

标签 python parsing grammar pyparsing recursive-descent

我想做一个 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/

相关文章:

python - 调整具有特定值的数组的大小

Haskell 优先级 : Lambda and operator

parsing - Haskell 中针对简单语言 AST 的良好类型设计

json - 在golang中的json unmarshal上获取可为空的对象

functional-programming - 在计算器语法中为括号添加优先级

ANTLR4:词法分析器规则:任何字符串,只要它不包含这两个并排的字符?

python - 将纯文本标题和图像导出到 Excel

Python 数组不工作

python - python 3.7中的无序字典

java - 在查询参数中获取 "%"值的 charconversion 异常?