[英]Is there a way to make a Terminal match every NAME except for specific keywords?
I am using lark to parse some text and needed a way to match a NAME
that did not have certain keywords in it.我正在使用 lark 来解析一些文本,并且需要一种方法来匹配其中没有某些关键字的NAME
。 I have the keywords listed out in a terminal I am just not sure how to make the terminal I need using it.我在终端中列出了关键字我只是不确定如何制作我需要使用它的终端。
Here is the way I formatted my keywords这是我格式化关键字的方式
keywords: "var"
| "let"
| "type"
All help on this is appreciated!对此的所有帮助表示赞赏!
Lark has a built-in support for the concept of keywords. Lark 内置了对关键字概念的支持。 So, it is unlikely that you need to explicitly exclude keywords NAME
.因此,您不太可能需要明确排除关键字NAME
。
For example:例如:
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', [])])
Having said that, if you must, you can accomplish this by using a regexp:话虽如此,如果必须,您可以使用正则表达式来完成此操作:
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 keep in mind that "TERMINAL" is upper-case, and "rule" is lower-case, and they have behave differently in Lark, so it's important to keep the distinction in mind. PS 请记住,“TERMINAL”是大写的,“rule”是小写的,它们在 Lark 中的表现不同,所以记住区别很重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.