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

Ruth Karl ruth.karl at gmx.de
Wed Jun 6 05:20:40 PDT 2007


Hi Matthew,

thanks a lot for your reply in the first place, it helped a lot to go on 
with my work.
Still I wonder, how to use predicates then in the grammar, in fact I 
copied the tagmode predicate from Oliver Zeigermanns xml parser example 
(http://www.antlr.org/wiki/display/ANTLR3/1.+Lexer) therefore - and 
because similar examples are described in the book - I trusted that it 
would work...
But as Jim Idle pointed out, predicates can not be executed with 
ANTLWorks interpreter anyway...

Have a nice day, and thanks again,

Ruth

Diehl, Matthew J schrieb:
> 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