Ignoring whitespace (in certain parts) in Antlr4

不打扰是莪最后的温柔 提交于 2019-12-04 20:02:10
CoronA

Define ID '@' ID as lexer token rather than as parser token.

A  : AID '(' IDList ')' ';' ;

AID : [a-zA-Z]+ '@' [a-zA-Z]+;

Other options

  • enable/disable whitespaces in your token stream, e.g. here
  • enable/disable whitespaces with lexer modes (may be a problem because lexer modes are triggered on context, which is not easy to determine in your case)

Even though you are skipping WS, lexer rules are still sensitive to the existence of the whitespace characters. Skip simply means that no token is generated for consumption by the parser. Thus, the lexer Addr rule explicitly does not permit any interior whitespace characters.

Conversely, the a and idList parser rules never see interior whitespace tokens so those rules are insensitive to the occurrence of whitespace characters occurring between the generated tokens.

grammar Foo;

program : a* EOF ; // EOF will require parsing the entire input

a  : Addr LParen IDList RParen Semi ;
idList : ID (Comma ID)* ;  // simpler equivalent construct

Addr : ID '@' ID 
ID : [a-zA-Z]+ ;
WS : [ \t\r\n]+ -> skip ;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!