python - PLY 使用动态令牌
问题描述
我正在编写一个程序,可以解析用 .tex 文件编写的数学论文。这是我想要的:
该程序应该检测数学论文中的开头、结尾、部分、小节、小节、定理、引理、定义、猜想、推论、命题、练习、符号和示例,并忽略其余内容以生成摘要.
一开始,程序应该保留所有字符,直到到达 token MT
。在这种情况下,杠杆应保留令牌并进入ig
模式。然后它应该忽略所有字符,除非它检测到定理/引理/定义/猜想/推论/示例/练习/符号/命题,在这种情况下,它会暂时进入INITIAL
模式并保留它或在这种情况下保留(子/子子)部分它应该暂时进入sec
模式。
\newtheorem{<name>}{<heading>}[<counter>]
和\newtheorem{<name>}[<counter>]{<heading>}
被检测为TH ptext THCC ptext THC ptext
和TH ptext THCS ptext THSC ptext THC
分别在哪里ptext
是一堆TEXT
.
import sys
import logging
from ply.lex import TOKEN
if sys.version_info[0] >= 3:
raw_input = input
tokens = (
'BT', 'BL', 'BD', 'BCONJ', 'BCOR', 'BE', 'ET', 'EL', 'ED', 'ECONJ', 'ECOR', 'EE', 'SEC', 'SSEC', 'SSSEC', 'ES', 'TEXT','ITEXT','BIBS','MT','BN','EN','BEXE','EEXE','BP','EP','TH','THCS','THSC','THCC','THC',
)
states = (('ig', 'exclusive'), ('sec', 'exclusive'), ('th', 'exclusive'), ('tht','exclusive'),('thc','exclusive'))
logging.basicConfig(
level = logging.DEBUG,
filename = "lexlog.txt",
filemode = "w",
format = "%(filename)10s:%(lineno)4d:%(message)s"
)
log = logging.getLogger()
th_temp = ''
thn_temp = ''
term_dic = {'Theorem':'','Lemma':'','Corollary':'','Definition':'','Conjecture':'','Example':'','Exercise':'','Notation':'','Proposition':''}
idb_list = ['','','','','','','','','']
ide_list = ['','','','','','','','','']
bb = r'\\begin\{'
eb = r'\\end\{'
ie = r'\}'
def finalize_terms():
global idb_list
global ide_list
if term_dic['Theorem'] != '':
idb_list[0] = bb + term_dic['Theorem'] + ie
ide_list[0] = eb + term_dic['Theorem'] + ie
if term_dic['Lemma'] != '':
idb_list[1] = bb + term_dic['Lemma'] + ie
ide_list[1] = eb + term_dic['Lemma'] + ie
if term_dic['Corollary'] != '':
idb_list[2] = bb + term_dic['Corollary'] + ie
ide_list[2] = eb + term_dic['Corollary'] + ie
if term_dic['Definition'] != '':
idb_list[3] = bb + term_dic['Definition'] + ie
ide_list[3] = eb + term_dic['Definition'] + ie
if term_dic['Conjecture'] != '':
idb_list[4] = bb + term_dic['Conjecture'] + ie
ide_list[4] = eb + term_dic['Conjecture'] + ie
if term_dic['Example'] != '':
idb_list[5] = bb + term_dic['Example'] + ie
ide_list[5] = eb + term_dic['Example'] + ie
if term_dic['Exercise'] != '':
idb_list[6] = bb + term_dic['Exercise'] + ie
ide_list[6] = eb + term_dic['Exercise'] + ie
if term_dic['Notation'] != '':
idb_list[7] = bb + term_dic['Notation'] + ie
ide_list[7] = eb + term_dic['Notation'] + ie
if term_dic['Proposition'] != '':
idb_list[8] = bb + term_dic['Proposition'] + ie
ide_list[8] = eb + term_dic['Proposition'] + ie
print(idb_list)
print(ide_list)
下面是一些解析函数:
def t_TH(t):
r'\\newtheorem\{'
t.lexer.begin('th')
return t
def t_th_THCS(t):
r'\}\['
t.lexer.begin('thc')
return t
def t_tht_THC(t):
r'\}'
if term_dic.has_key(thn_temp) == False:
print(f"{thn_temp} is unknown!")
elif len(th_temp) == 0:
print(f"No abbreviation for {thn_temp} is found!")
else:
term_dic[thn_temp] = th_temp
print(f"The abbreviation for {thn_temp} is {th_temp}!")
th_temp = ''
thn_temp = ''
t.lexer.begin('INITIAL')
return t
def t_th_THCC(t):
r'\}\{'
t.lexer.begin('tht')
return t
def t_thc_THSC(t):
r'\]\{'
t.lexer.begin('tht')
return t
@TOKEN(idb_list[0])
def t_ig_BT(t):
t.lexer.begin('INITIAL')
return t
@TOKEN(ide_list[0])
def t_ET(t):
t.lexer.begin('ig')
return t
def t_INITIAL_sec_thc_TEXT(t):
r'[\s\S]'
return t
def t_th_TEXT(t):
r'[\s\S]'
th_temp = th_temp + t.value()
return t
def t_tht_TEXT(t):
r'[\s\S]'
thn_temp = thn_temp + t.value()
return t
def t_ig_ITEXT(t):
r'[\s\S]'
pass
import ply.lex as lex
lex.lex(debug=True, debuglog = log)
以下是错误:
ERROR:
/Users/CatLover/Documents/Python_Beta/TexExtractor/texlexparse.py:154: No regular expression defined for rule 't_ET'
我不知道为什么使用@TOKEN 为 't_ET' 等定义的正则表达式不起作用。
解决方案
Ply 是一个解析器生成器。它获取您的解析器/词法分析器描述并从中编译解析器/词法分析器。您不能在解析期间更改语言的描述。
在这种特殊情况下,您最好编写一个流式(“在线”)扫描仪。但是,如果您想使用 Ply,那么最好不要尝试修改语法以忽略部分输入。只需解析整个输入并忽略您不感兴趣的部分。您可能会发现代码要简单得多。
推荐阅读
- tensorflow - 在模型定义中使用循环和 Lambda 层时发生 Keras load_model 错误
- android - 如何将 .docx 转换为 .txt 或从 android 中的 docx 文件中获取文本?
- javascript - canActive 角度如何从身份验证中获取值真假(用户登录与否)
- r - 按每个组中的子组汇总(&group by)
- configuration - 无法通过 LuCI 或 SSH 访问 OpenWrt
- javascript - 检查是否所有必填字段都填写在特定的 div 中
- java - spring mvc中不同的@ModelAttribute
- android - 如何将我的 XML 文件保存到特定文件夹?
- reactjs - 如何在 React Hooks 中使用 Antd 进行搜索
- amazon-web-services - Glue 是否需要安全配置才能从加密存储桶运行 Athena 查询?