![](/img/trans.png)
[英]Using python 3x, is there a way to shift lines in a file to a specific column and make every line (except the last line) of equal length?
[英]Is there a way to make a Terminal match every NAME except for specific keywords?
我正在使用 lark 來解析一些文本,並且需要一種方法來匹配其中沒有某些關鍵字的NAME
。 我在終端中列出了關鍵字我只是不確定如何制作我需要使用它的終端。
這是我格式化關鍵字的方式
keywords: "var"
| "let"
| "type"
對此的所有幫助表示贊賞!
Lark 內置了對關鍵字概念的支持。 因此,您不太可能需要明確排除關鍵字NAME
。
例如:
l = Lark("""
%import common (LETTER, DIGIT)
NAME: LETTER (LETTER | DIGIT)*
keywords: "var"
| "let"
| "type"
start: NAME | keywords
""", parser="lalr")
print(l.parse("hello")) # Tree('start', [Token('NAME', 'hello')])
print(l.parse("let")) # Tree('start', [Tree('keywords', [])])
話雖如此,如果必須,您可以使用正則表達式來完成此操作:
l = Lark("""
%import common (LETTER, DIGIT)
NAME: /(?!(let|type|var))/ LETTER (LETTER | DIGIT)*
start: NAME
""")
print(l.parse("hello")) # Tree('start', [Token('NAME', 'hello')])
print(l.parse("let")) # Exception, terminal not defined
PS 請記住,“TERMINAL”是大寫的,“rule”是小寫的,它們在 Lark 中的表現不同,所以記住區別很重要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.