[antlr-interest] Newbie Q: detect rule based on previous rule

Sandeep Gupta sandy.pec at gmail.com
Wed Sep 20 22:10:24 PDT 2006


I am dont know much about compilers or grammars per se. I have started
learning and for an example was writing a sample grammar to parse DTDs. Now
I am facing a problem in writing the grammar, of which a few rules are as

fragment comment    :    '<!--' commentText CommentEnd    ;

fragment commentText    :     ~(CommentEnd)*    ;

CommentEnd    :    '-->'    ;

Question 1: To read whole of the text between the tokens for CommentStart
and CommentEnd, I have tried using (options {greedy=false;} : .)* but this
fails and throws a NoViableAltException after parsing the first comment it
encounters. But this way the comments are parsed completely? If someone
could explain me the difference in both the approaches.

Question 2: While writing the grammar I sometimes find a situation where I
want that ANTLR shoudl only see this rule as valid, if a previous rule has
just been parsed through. Like for a rule (to parse quoted strings) like

fragment quotedString     :    ('"' ((~('<' | '&' | '"' )) )* '"')    ;

ANTLR says

 [10:21:25] dtd.g:210:43: Decision can match input such as
"{CommentEnd..Letter, CharReference..'%', '\''..'NOTATION'}" using multiple
alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input

Now the thing here is like I know that CommentEnd can only come if there has
been a CommentStart. How can I code this.There have been a lot of terms
being used here in the mailing lists like Syntactic predicates, lexer modes
etc. Is it anything related to it. If someone could refer me a good book to
start with to learn the concepts (except the tutorials on ANTLR, already
going through them).

Any suggestions would be most most welcome.

Sandeep Gupta
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060921/8813297f/attachment.html 

More information about the antlr-interest mailing list