繁体   English   中英

解析乳胶的简单扩展:语法,递归,pyParsing?

[英]Parsing a simple extension of latex: grammar, recursive descent, pyParsing?

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

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}

我有一个'tab'一致关键字,例如theorem ,它包含内容。
此选项卡包含可嵌套的按键作品。

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

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

然后,还有其他所有的乳胶构造,我不会碰,只留在那里。

我研究了pyParsing和codeTalker
codeTalker是上下文无关的语法,我不知道我的描述语法是否是上下文无关的。
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”块的缩进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM