简体   繁体   English

有没有办法让终端匹配除特定关键字之外的每个名称?

[英]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.

相关问题 使用python 3x,是否有办法将文件中的行移动到特定列,并使每一行(最后一行除外)长度相等? - 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? 匹配除特定字符串外的所有内容 - Match everything except a specific string 有没有办法搜索和抓取特定关键字的数据? - Is there an way to search and scrape data for specific keywords? 将文本与关键字集 (NLP) 匹配的好方法是什么 - Whats a good way to match text to sets of keywords (NLP) 使用Python解析大型journalctl文件以匹配关键字的有效方法 - Efficient way to parse large journalctl file to match keywords using Python 如何匹配除某些字符以外的所有东西? 正则表达式 - How to match every thing except some character ? regex 在 pandas 中计算一组特定关键字出现次数的最有效方法是什么? - What is the most efficient way of counting occurrences of a bunch of specific keywords in pandas? 有没有办法匹配特定点进行通信? - Is there a way to match specific points for correspondence? 使用psutil杀死每个python进程名称,除了一个 - Kill every python process name with psutil except one 使用正则表达式匹配字符串,除了特定的字符串组合python - Match string using regular expression except specific string combinations python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM