ANTLR v3: order token to improve performance of tree walker

梦想的初衷 提交于 2019-12-25 02:37:12

问题


Is it somehow possible to specify the order of the tokens generated by ANTLR v3? My goal is to order the tokens in such a way that valid tokens in my rule "expression" follow each other in order that conditions in the tree walkers (which have about 90 different branches) can be simplified to one branch. Something like

if(LA18_0 >= ARRAY_ACCESS && LA18_0 <= VariableId){}

ANTLR assigns the values to the tokens in alphabetical order. That means, the token beginning with an "a" has the lowest value, token with "z" the highest.

An example to illustrate my problem. "abstract" (which is not a valid token in my rule "expression") has value 5 right after ARRAY_ACCESS (which has value 4) and thus the condition has to look like the following:

if(LA18_0 == ARRAY_ACCESS || (LA18_0 >= Assign && LA18_0 <= VariableId)){}

That's just an example, as I said, there are about 90 branches.

I could prefix those tokens to achieve my goal but that looks rather ugly and hampers the readability. Is there another way?


回答1:


The only way to guarantee that ANTLR uses particular constants for particular tokens is to write your own .tokens file (you'll see them produced ANTLR when you compile grammars). You can then use the tokenVocab option to specify the name of your custom tokens file.

That said, the ordering of tokens has never proven to be a significant performance issue in my experience. The C# port of ANTLR 3 approaches the performance issue by eliminating redundant range checks that appear on some code paths.



来源:https://stackoverflow.com/questions/22271958/antlr-v3-order-token-to-improve-performance-of-tree-walker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!