使用“键”作为规则元素标签

时间:2018-07-24 15:10:06

标签: c# antlr4

在我的项目中,我编写了一种用于处理键值对的语言。因此,我有一些规则来匹配多个键keysList和一个用于值valuesList的规则。现在我想在其他规则中使用它们

operationExpression
    :    keysList CompareOperator valuesList
    ;

哪个工作正常。

我认为最好将这些规则元素命名为keysvalues

operationExpression
    :    keys=keysList CompareOperator values=valueList
    ;

当我现在尝试编译我的项目时,我会抱怨成千上万个错误,抱怨缺少分号。当我查看生成的代码时,我发现Antlr为这些上下文提供了奇怪的名称。我希望它们分别是keysvalues,但它们似乎是每个C#关键字(值之间有一些@)。

public KeysListContext structexterndoushortfloatwhileoperatorrefprotectedreadonlycontinueelsecheckedlockcatchifcasenewusingstaticvoidinsizeofbytedoublesealedfinallythisuncheckedisparamsenumexplicitasnulltruefixedtrydecimalobjectimplicitinternalprivatevirtualboolconststringforinterfaceunsafelongoutswitchdelegateforeachdefaultulonggotopublicstackallocoverrideeventclasstypeofbreakfalsevolatileabstractuintintthrowcharnamespacesbyteshortreturnbase;
public ValuesListContext @struct@extern@do@ushort@float@while@operator@ref@protected@readonly@continue@else@checked@lock@catch@if@case@new@using@static@void@in@sizeof@byte@double@sealed@finally@this@unchecked@is@params@enum@explicit@as@null@true@fixed@try@decimal@object@implicit@internal@private@virtual@bool@const@string@for@interface@unsafe@long@out@switch@delegate@foreachkey@ulong@goto@public@stackalloc@override@event@class@typeof@break@false@volatile@abstract@uint@int@throw@char@namespace@sbyte@short@return@base;

每个其他名字似乎都很好。为什么Antlr会这样做,为什么不允许我将keysvalues用作规则元素标签?

2 个答案:

答案 0 :(得分:1)

这是一个错误:Issue 1125

似乎在某处地图上,用户定义的标识符可以与地图中已存在的某些键(包括keysvalues)发生冲突。

因此,在等待错误修复时,您必须使用其他名称。

答案 1 :(得分:0)

我认为这是一种告诉您使用保留关键字作为标签名称的方法。您已经发现其他名称可以很好地工作,因此请使用不与您的语言保留字冲突的名称。