antlr - my lexer token action is not invoked -


i use antlr4 javascript target.

here sample grammar:

p : t ; t : [a-z]+ {console.log(this.text);} ; start: p ; 

when run generated parser, nothing printed, although input matched. if move action token p, gets invoked. why that?

actions ignored in referenced rules. original behavior of antlr 4, when lexer supported single action per token (and action must appear @ end of token).

several releases later limitation of one-action-per-rule lifted, allowing number of actions executed token. however, found many existing users relied on original behavior, , wrote grammars assuming actions in referenced rules ignored. many of these grammars used complicated logic in these rules, changing behavior severe breaking change prevent people using new versions of antlr 4.

rather break many existing antlr 4 lexers, decided preserve original behavior , execute actions appear in same rule matched token. newer versions allow place multiple actions in each rule though.

tl;dr: considered allowing actions in other rules execute, decided not because break lot of grammars written , used people.


Comments