我的提取字符串的token函数是:
def t_STRING(t): r'\"([^\"]|\\.)*\"' t.value = t.value[1:-1] return t
有个办法是:
t.value=t.value.decode("string-escape")
但是这样并不能处理
\n, \t ,等复杂些的和自定义的转义
根据此文http://inst.eecs.berkeley.edu/~cs164/sp10/TA.d/flex-start-conditions.html,貌似应该用state.
怎么用ply来做这个事情呢 ?
ES = r"""(\\(['"\?\\abfnrtv]|[0-7]{1,3}|x[a-fA-F0-9]+))""" STR = r'\"([^"\\\n]|'+ES+')*\"' @TOKEN(STR) def t_STRING(t): t.value = t.value[1:-1] return t
来自 ANSIC grammer
利用下 json 的 loads 好啦