Antlr4 – 获取标记名称

huangapple go评论78阅读模式
英文:

Antlr4 - Get token name

问题

以下是您要翻译的代码部分:

// 差异化`complexType`和`builtinType`,可以查看哪个不为null。
// 但是,如果我想区分`bool`和`u8`,我该怎么做呢?
// 这个问题可以回答我的问题,但是它是针对Antlr3的。

请注意,这段代码是关于如何在ANTLR中区分boolu8类型的问题。

英文:

I have following (reduced) grammar:

grammar Test;
IDENTIFIER: [a-z]+ [a-zA-Z0-9]*;
WS: [ \t\n] -> skip;
compilationUnit:
	field* EOF;
field:
	type IDENTIFIER;
type:
	(builtinType|complexType) ('[' ']')*;
builtinType:
	'bool' | 'u8';
complexType:
	IDENTIFIER;

And following program:

import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;

public class Main{
	public static void main(String[] args){
		TestLexer tl=new TestLexer(CharStreams.fromString("u8 foo bool bar complex baz complex[][] baz2"));
		TestParser tp=new TestParser(new CommonTokenStream(tl));
		TestParser.CompilationUnitContext cuc=tp.compilationUnit();
		System.out.println("CompilationUnit:"+cuc);
		for(var field:cuc.field()){
			System.out.println("Field: "+field);
			System.out.println("Field.type: "+ field.type());
			System.out.println("Field.type.builtinType: "+field.type().builtinType());
			System.out.println("Field.type.complexType: "+field.type().complexType());
			if(field.type().complexType()!=null)
				System.out.println("Field.type.complexType.IDENTIFIER: "+field.type().complexType().IDENTIFIER());
		}
	}
}

To differentiate complexType and builtinType, I can look, which is not-null.
But, if I want to distinguish between bool and u8, how can I do that?
This question would answer my question, but it is for Antlr3.

答案1

得分: 2

要么使用替代标签

builtinType
 : 'bool' #builtinTypeBool
 | 'u8'   #builtinTypeU8
 ;

和/或在词法分析器中定义这些标记:

builtinType
 : BOOL
 | U8
 ;

BOOL : 'bool';
U8   : 'u8';

这样,您可以更轻松地检查访问者/监听器中的标记类型:

YourParser.BuiltinTypeContext ctx = ...
        
if (ctx.start.getType() == YourLexer.BOOL) {
  // 它是一个 BOOL 标记
}
英文:

Either use alternative labels:

builtinType
 : 'bool' #builtinTypeBool
 | 'u8'   #builtinTypeU8
 ;

and/or define these tokens in the lexer:

builtinType
 : BOOL
 | U8
 ;

BOOL : 'bool';
U8   : 'u8';

so that you can more easily inspect the token's type in a visitor/listener:

YourParser.BuiltinTypeContext ctx = ...
        
if (ctx.start.getType() == YourLexer.BOOL) {
  // it's a BOOL token
}

huangapple
  • 本文由 发表于 2020年8月5日 16:15:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63261051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定