如何在ANTLR4中有时将关键字解析为普通单词 [英] How to parse keywords as normal words some of the time in ANTLR4
问题描述
我有一种语言,其中包含诸如hello
之类的关键字,它们只是某些类型的句子中的关键字.例如,在其他类型的句子中,这些单词应作为ID进行匹配.这是一个讲述故事的超简单语法:
I have a language with keywords like hello
that are only keywords in certain types of sentences. In other types of sentences, these words should be matched as an ID, for example. Here's a super simple grammar that tells the story:
grammar Hello;
file : ( sentence )* ;
sentence : 'hello' ID PERIOD
| INT ID PERIOD;
ID : [a-z]+ ;
INT : [0-9]+ ;
WS : [ \t\r\n]+ -> skip ;
PERIOD : '.' ;
我希望这些句子有效:
hello fred.
31 cheeseburgers.
6 hello.
但是最后一句话在该语法中不起作用.单词hello
是类型hello
的令牌,而不是类型ID
的令牌.词法分析器似乎抓住了所有的问候,并将它们转换为该类型的标记.
but that last sentence doesn't work in this grammar. The word hello
is a token of type hello
and not of type ID
. It seems like the lexer grabs all the hellos and turns them into tokens of that type.
这是一种疯狂的方式来解释我想要的东西:
Here's a crazy way to do it, to explain what I want:
sentence : 'hello' ID PERIOD
| INT crazyID PERIOD;
crazyID : ID | 'hello' ;
但是用我的真实语言来说,有很多像hello这样的关键字要处理,所以,是的,这似乎很疯狂.
but in my real language, there are a lot of keywords like hello to deal with, so, yeah, that way seems crazy.
是否有一种合理,紧凑,与目标语言无关的方式来处理此问题?
Is there a reasonable, compact, target-language-independent way to handle this?
推荐答案
处理关键字的标准方法:
A standard way of handling keywords:
file : ( sentence )* EOF ;
sentence : key=( KEYWORD | INT ) id=( KEYWORD | ID ) PERIOD ;
KEYWORD : 'hello' | 'goodbye' ; // list others as alts
PERIOD : '.' ;
ID : [a-z]+ ;
INT : [0-9]+ ;
WS : [ \t\r\n]+ -> skip ;
根据在ID
规则之前列出的KEYWORD
规则,可以解决KEYWORD
和ID
规则之间的模棱两可的情况.
The seeming ambiguity between the KEYWORD
and ID
rules is resolved based on the KEYWORD
rule being listed before the ID
rule.
在解析器SentenceContext中,将生成TerminalNode变量key
和id
,并且在解析时将有效地保存匹配的标记,从而轻松进行位置识别.
In the parser SentenceContext, TerminalNode variables key
and id
will be generated and, on parsing, will effectively hold the matched tokens, allowing easy positional identification.
这篇关于如何在ANTLR4中有时将关键字解析为普通单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!