[antlr-interest] Re: Problem with x: (A)(B)? ;

Lubos Vnuk lubos.vnuk at rts.at
Thu Feb 26 05:27:49 PST 2004


As I said I might be missing your point completely thus I apologize 
in advance in case my help is misleading.

I assure you that I have used your example to generate the parser and 
I have seen the issue in the source code you are talking about.

Using your rule (I removed the EOF alternative):
startRule :
       (x:A(B)?  
       |b:B  
       );

you will get:
		...
		try {      // for error handling
			{
			switch ( LA(1)) {
			case A:
			{
				x = LT(1);
				match(A);
				{
				switch ( LA(1)) {
				case B:
				{
					match(B);
					break;
				}
				case EOF:
				{
					break;
				}
				default:
				{
					throw new NoViableAltException
(LT(1), getFilename());
				}
				}
				}
				break;
			}
			case B:
			{
			...
			...


Your worry is the EOF case after the first B case, right? Here I can 
only repeat what I have written before. A (based on the grammar) can 
only by followed by B or EOF.

Now for instance if you add a rule:

topRule: (startRule)*;

You will apparently get a nondeterminism (ignore for now) but note 
the change in the genereted code for startRule:

		...
		try {      // for error handling
			{
			switch ( LA(1)) {
			case A:
			{
				x = LT(1);
				match(A);
				{
				if ((LA(1)==B)) {
					match(B);
				}
				else if ((LA(1)==EOF||LA(1)==A||LA(1)
==B)) {
				}
				else {
					throw new NoViableAltException
(LT(1), getFilename());
				}
				
				}
				break;
			}
			case B:
			{
			...
			...

I know this is not the best example but I hope it is clear now what 
ANTLR tries to do and why it does so.

HTH,
Lubos.



--- In antlr-interest at yahoogroups.com, "dotlessbraille" 
<easjolly at i...> wrote:
> I really appreciate your trying to help but request you to please 
> look at the generated Java. I understand that ANTLR will put in a 
> test for an EOF if I don't put one in.  However, this test is at 
the 
> highest level of alternatives.
> 
> The problem I am encountering is that ANTLR interprets the (B)? as 
> requiring either a B or an EOF directly after the A.  In other 
> words, it won't handle the case where there are two A's.  It works 
> fine if I use x:A(B)* which isn't what I want, however. 
> 
> startRule :
>       (x:A(B)? 
>       |b:B  
>       |eof:EOF 
>       );



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list