[英]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)
所以我有幾個關鍵字,例如ep
, oldFunction
,我想轉換為新關鍵字。
它們可以嵌套。
\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.