[antlr-interest] Grammar problem, probably silly....

Diehl, Matthew J matthew.j.diehl at intel.com
Tue Jun 5 11:20:57 PDT 2007


The problem with your grammar is that you were using lexer::member
variables to hold the state of 'tagMode', and so in each different lexer
rule their 'tagMode' would automatically be set to false.  If you set it
to lexer::header and made a public static class with tagMode in it (or
in C I guess a global variable), you would run into problems because
lexer::header appears before the includes, which will throw errors, so
you'd have to create your global variables by hand and edit the lexer
file and put them after the includes.  Otherwise, one work around is
below:  (notice that if you don't include (~('/'|'<'|'>'))+ you'll run
into problems with TEXT eating up either of those characters (even
though greedy is set to false and they are declared above TEXT).

        grammar JSP;

        options {
            output = AST;
        }
        

        // Parser rules
        jsp    :    oroot (content)* croot EOF
            ;
        oroot     :     OPENTAG JSPROOT CLOSETAG   
            ;
        croot    :    OPENTAG SLASH JSPROOT CLOSETAG
            ;
        content    :     TEXT
            ;



        // Lexer rules
        OPENTAG     :    '<' 
            ;
        CLOSETAG     :   '>' 
            ;
        JSPROOT
        	:	'jsp:root'
        	;
        SLASH   
        	:	'/'
        	;
        TEXT
      	options{greedy=false;}
      		: (~('<'|'>'| '/'))+
      		;
WS	:	(' ' | '\t' | '\n' | '\r') { $channel=HIDDEN; }
	;

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Ruth Karl
Sent: Tuesday, June 05, 2007 10:10 AM
To: ANTR Interest
Subject: [antlr-interest] Grammar problem, probably silly....

Hi,

I have been trying quite a while now, but I keep getting a 
MismatchedTokenException with the following simplified test grammar:

        grammar JSP;

        options {
            language = CSharp;
            output = AST;
        }


        @members {
             boolean xmlDoc = false;
              boolean outputEnabled = false;
        }



        @lexer::members {
                boolean tagMode = false;
        }

        // Parser rules
        jsp    :    oroot (content)* croot EOF
            ;
        oroot     :     OPENTAG OROOT CLOSETAG   
            ;
        croot    :    OPENTAG '/jsp:root' CLOSETAG
            ;
        content    :     TEXT
            ;



        // Lexer rules
        OPENTAG     :     '<' { tagMode = true; }
            ;
        CLOSETAG     :     '>' { tagMode = false; }
            ;
        TEXT    :    {!tagMode}?=> (~'<')+
        ;
        OROOT    :    'jsp:root'
         ;  

The exception occurs when I print

        <jsp:root>ljlj</jsp:root>

in the interpreter and tell it to start from jsp rule.
it says (4!=5), according to the generated files this is OROOT=5 and 
OPENTAG=4.
Could anyone please help me with that???

Thanks a lot!

Ruth


More information about the antlr-interest mailing list