1
votes

I'm trying Lexing Modes for the first time. I have a lexer grammar with a mode that I'm importing into my "main" grammar. I get this error when generating the java classes for the Grammar's lexer

'rule DESCRIPTION_FIELD contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output'

I followed this article My Lexer grammar is the following :

lexer grammar TestLexerGrammar;

DESCRIPTION_FIELD
:
    'DESCRIPTION:'-> pushMode(FREETEXTMODE)
;


mode FREETEXTMODE;

FREE_TEXT_FIELD_FORMAT
:
    STR+
;

fragment
STR
:
    (
        LETTER
        | DIGIT
    )
;

my main grammar:

grammar Grammar;
import TestLexerGrammar;


descriptionElement
:
    DESCRIPTION_FIELD freeTextFields
;

freeTextFields
:
    FREE_TEXT_FIELD_FORMAT+
;

so in the generated GrammarLexer.java I get an error : " FREETEXTMODE cannot be resolved to a variable "

Is this a wrong approach? and is there a possible way to trigger changing mode through a parsing rule?

1
The grammar you have proposed (after adding Letter, Digit rules) is fine. It worked for me...Divisadero
you are right, sorry I forgot to specify that I'm importing the lexer grammar into a parsing grammar and using the Token DESCRIPTION_FIELD into a rule (Editing the post)ps_messenger

1 Answers

1
votes

You can not use mode in grammars with import statement. There are related issues on github: Problems with lexical modes inside an imported grammar and No error/incorrect code generation when importing lexer grammar with modes into a combined grammar.

So, you should repair your main grammar and remove import statement by the following way:

parser grammar Grammar;
options { tokenVocab=TestLexerGrammar; }