ANTLR - 带空格的标识符 [英] ANTLR - identifier with whitespace

查看:30
本文介绍了ANTLR - 带空格的标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要可以包含空格的标识符.

i want identifiers that can contain whitespace.

grammar WhitespaceInSymbols;

premise :   ( options {greedy=false;} : 'IF' )  id=ID{
System.out.println($id.text);
};

ID  :   ('a'..'z'|'A'..'Z')+ (' '('a'..'z'|'A'..'Z')+)* 
;

WS  :   ' '+ {skip();}
;

当我使用IF 语句分析"进行测试时,我得到一个 MissingTokenException 和输出IF 语句分析".
我想,通过使用 greedy=false 我可以告诉 ANTLR 在 'IF' 之后退出并将其作为令牌.但 IF 是 ID 的一部分.有没有办法实现我的目标?我已经尝试了 greed=false 选项的一些变体,但没有成功.

When i test this with "IF statement analyzed" i get a MissingTokenException and the output "IF statement analyzed".
I thought, that by using greedy=false i could tell ANTLR to exit afer 'IF' and take it as a token. But instead the IF is part of the ID. Is there a way to achieve my goal? I already tried some variations of the greed=false-option, but without success.

推荐答案

我想,通过使用 greedy=false 我可以告诉 ANTLR 在 'IF' 之后退出并将其作为令牌.

I thought, that by using greedy=false i could tell ANTLR to exit afer 'IF' and take it as a token.

不,解析器对令牌的创建没有什么可说的:首先对输入进行令牌化,然后将解析器规则应用于这些令牌.所以设置 greedy=false 没有效果.

No, the parser has nothing to say about the creation of tokens: the input is first tokenized and then the parser rules are applied on these tokens. So setting greedy=false has no effect.

可以这样做(创建带有空格的 ID 标记),但这将是一个糟糕的解决方案,其中包含许多谓词,并且词法分析器中的一些自定义方法正在执行手动预测:你真的,真的不想要这个!更简洁的解决方案是在解析器中引入 id 规则,让它匹配一个或多个 ID 标记.

You can do this (creating ID tokens with white spaces), but it will be a horrible solution with many predicates, and a few custom methods in the lexer doing manual look-aheads: you really, really don't want this! A much cleaner solution would be to introduce a id rule in your parser and let it match one or more ID tokens.

grammar WhitespaceInSymbols;

premise
  :  IF id THEN EOF
  ;

id
  :  ID+
  ;

IF
  :  'IF'
  ;

THEN
  :  'THEN'
  ;

ID  
  :  ('a'..'z' | 'A'..'Z')+
  ;

WS  
  :  ' '+ {skip();}
  ;

会将输入的 IF 语句分析 THEN 解析为以下树:

would parse the input IF statement analyzed THEN into the following tree:

这篇关于ANTLR - 带空格的标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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