Antlr Lexer 排除某个模式 [英] Antlr Lexer exclude a certain pattern

查看:26
本文介绍了Antlr Lexer 排除某个模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Antlr Lexer 中,我如何实现这样的令牌解析:

In Antlr Lexer, How can I achieve parsing a token like this:

包含任何非空格字母但不包含.{"的单词.我能想到的最好方法是使用语义谓词.

A word that contains any non-space letter but not '.{' inside it. Best I can come up with is using a semantics predicate.

WORD: WL+   {!getText().contains(".{")};
WL: ~[ \n\r\t];

我有点担心使用语义谓词,尽管因为这里的 WORD 将被词法数百万次我认为放置语义谓词会影响性能.

I'm a bit worried to use semantics predicate though cause WORD here will be lexed millions of times I would think to put a semantics predicate will hit the performance.

这来自我需要解析如下内容的要求:

This is coming from the requirement that I need to parse something like:

TOKEN_ONE.{TOKEN_TWO}

而 TOKEN_ONE 可以包含 .和 { 在它的字母中.

while TOKEN_ONE can include . and { in its letter.

我使用的是 Antlr 4.

I'm using Antlr 4.

推荐答案

您需要将谓词评估限制为紧跟在输入中的 . 之后的情况.

You need to limit your predicate evaluation to the case immediately following a . in the input.

WORD
  : ( ~[. \t\r\n]
    | '.' {_input.LA(1)!='{'}?
    )+
  ;

这篇关于Antlr Lexer 排除某个模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆