【问题标题】:Parsing a simple extension of latex: grammar, recursive descent, pyParsing?解析latex的一个简单扩展:语法、递归下降、pyParsing?
【发布时间】:2013-03-31 13:22:07
【问题描述】:

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

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,其中包含内容。
此选项卡包含 keyworks 可以嵌套。

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

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

然后,还有所有其他的乳胶结构,我不碰,就放在那里。

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

【问题讨论】:

    标签: python parsing grammar pyparsing recursive-descent


    【解决方案1】:

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

    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”块的缩进。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多