1

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

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)

所以我有几个关键字,比如ep, oldFunction,我想转换成一个新的关键字。
它们可以嵌套。

\oldFunction{\ep}{\ep}

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

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

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

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

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

4

1 回答 1

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

于 2013-03-31T16:35:07.483 回答