用 unicode 编写语法规则名称 [ANTLR 4] [英] write a grammar rule name in unicode [ANTLR 4]

查看:31
本文介绍了用 unicode 编写语法规则名称 [ANTLR 4]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然是 ANTLR 4 的初学者,我想知道是否有办法用 unicode 编写语法规则名称.例如,以下规则很好:

I am still a beginner in ANTLR 4 and I was wondering if there is a way to write a grammar rule name in unicode. For example, the following rule is fine:

atomExp 返回 [double value]: n=Number {$value = Double.parseDouble($n.text);}|'(' exp=additionExp ')' {$value = $exp.value;};

但是,假设我想编写相同的规则,但不是将其名称写为 "atomExp" ,而是将名称写为阿拉伯语单词 "تعبير"

However, let's say I want to write the same rule but instead of writing its name as "atomExp" , I want to write the name as an Arabic word "تعبير"

تعبير 返回 [double value]: n=Number {$value = Double.parseDouble($n.text);}|'(' exp=additionExp ')' {$value = $exp.value;};

但是当我尝试以这种方式编写它时,出现没有可行的替代方案"错误.有人可以解决我的问题吗?提前致谢

but when I try to write it that way I get "no viable alternative" error. Can someone solve my problem please. Thanks in advance

推荐答案

查看时 ANTLR4 的词法分析器语法,可以看到词法分析器和解析器名称支持某些 Unicode 字符:

When looking at the lexer grammar for ANTLR4, you can see that lexer and parser names support certain Unicode chars:

/** Allow unicode rule/token names */
ID  :   NameStartChar NameChar*;

fragment
NameChar
    :   NameStartChar
    |   '0'..'9'
    |   '_'
    |   '\u00B7'
    |   '\u0300'..'\u036F'
    |   '\u203F'..'\u2040'
    ;

fragment
NameStartChar
    :   'A'..'Z'
    |   'a'..'z'
    |   '\u00C0'..'\u00D6'
    |   '\u00D8'..'\u00F6'
    |   '\u00F8'..'\u02FF'
    |   '\u0370'..'\u037D'
    |   '\u037F'..'\u1FFF'
    |   '\u200C'..'\u200D'
    |   '\u2070'..'\u218F'
    |   '\u2C00'..'\u2FEF'
    |   '\u3001'..'\uD7FF'
    |   '\uF900'..'\uFDCF'
    |   '\uFDF0'..'\uFFFD'
    ; // ignores | ['\u10000-'\uEFFFF] ;

INT : [0-9]+
       ;

但您的 ID تعبير 似乎不符合 ID 规则的 NameChar* 部分.

But it appears that your ID تعبير does not comply with the NameChar* part of the ID rule.

这篇关于用 unicode 编写语法规则名称 [ANTLR 4]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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