1

我有一个令牌,我想让 2 个作业有效,并且我试图找出最好的方法。

例如我有

TOSTRING = 'tostring'

但我也希望 'toString' 像这样有效:

TOSTRING = 'toString'

实现这一目标的最佳方法是什么?

编辑:我想让它输出到 *.token 文件为

TOSTRING=9
'toString'=9
'tostring'=9

我使用该语言的代码使用此结构并将 TOSTRING='tostring' 放入 token{ } 部分会生成此结构。即使是具有单个赋值的词法分析器规则也会这样做。但是当我有多个分配时,令牌不会用于“toString”或“tostring”

4

2 回答 2

4

In general, don't use the tokens section as you lose some control of the lexer. Always use real lexer rules. The tokens section just automatically adds lexer rules anyway. There is no difference except you start to run in to the limitations when you want more than just a simple string.

If you want case independence, then see the article here:

How do I get Case independence?

But implement it via the override of LA() (described there) and not the 'A'|'a' methods, which will generate lots of code you don't need. If it is JUST this camel case then:

TOSTRING
    : 'to' ('s' | 'S') 'tring'
    ;
于 2012-12-06T03:52:16.303 回答
3

最快的方法是定义词法分析器规则TOSTRING以接受两者:

TOSTRING
    : 'tostring'   //alternative #1, lower-case 's'
    | 'toString'   //alternative #2, upper-case 'S'
    ;

或等价物:

TOSTRING
    : 'to' ('s' | 'S') 'tring'
    ;
于 2012-12-05T21:09:34.837 回答