backtracking in JavaCC

View: New views
4 Messages — Rating Filter:   Alert me  

Parent Message unknown backtracking in JavaCC

by David Portabella Clotet :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

One quick question;
Does JavaCC implement backtracking?
According to the JavaCC FAQ website, lookahead does not implement backtracking.

so I cannot parse this input "ABAAAB" with the following program (see attached the full code):
void Entry() : {}
{
( LOOKAHEAD(2) (<A> | <B>) )+
<B> 
}

note: I understand that I could rewrite this program to make it work without backtracking;
however this is a simplified program illustrating the problem that I have with a more complex one which does need backtracking.


In the regular expression world, backtracking is so simple: this regex would do the job "[AB]+B".
Is there a way to tell JavaCC to consider backtracking?

Many thanks,
DAvid Portabella




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...

Test.jj (604 bytes) Download Attachment

Re: backtracking in JavaCC

by J.Chris Findlay :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In JavaCC one often uses semantic lookahead instead of backtracking.  Not sure if it will help with what you have or not..

On Wed, Jul 8, 2009 at 8:38 AM, David Portabella Clotet <david@...> wrote:
Hello,

One quick question;
Does JavaCC implement backtracking?
According to the JavaCC FAQ website, lookahead does not implement backtracking.

so I cannot parse this input "ABAAAB" with the following program (see attached the full code):
void Entry() : {}
{
( LOOKAHEAD(2) (<A> | <B>) )+
<B> 
}

note: I understand that I could rewrite this program to make it work without backtracking;
however this is a simplified program illustrating the problem that I have with a more complex one which does need backtracking.


In the regular expression world, backtracking is so simple: this regex would do the job "[AB]+B".
Is there a way to tell JavaCC to consider backtracking?

Many thanks,
DAvid Portabella



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...



--
- J.Chris Findlay
  (c:

RE: backtracking in JavaCC

by Mazas Marc :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi
 
There is no backtracking in JavaCC, just lookahead.
One solution for your problem is to use semantic lookahead.
Here under is an example.
Regards
 

Marc MAZAS

Phone : +33 (0)4 72 18 47.19
Mobile : +33 (0)6 89 86 50 56

 

options
{
STATIC = false;
DEBUG_LOOKAHEAD = true;
}
 
PARSER_BEGIN(Test)
package foo.bt1;
import java.io.*;
import java.util.*;
 
public class Test
{
public static void main(String args []) throws Exception
{
Reader r = new StringReader("ABAAAB");
Test interpreter = new Test(new TestTokenManager(new SimpleCharStream(r)));
interpreter.Entry();
}
}
 
PARSER_END(Test)
 
TOKEN :
{
< A : "A" >
| < B : "B" >
}
 
void AorBnotBandEOF() :
{}
{
(
{
// kind : < EOF > -> 0, < A > -> 1, < B > -> 2 : see TestConstants
System.out.println("t1k : "+ getToken(1).kind);
System.out.println("t2k : "+ getToken(2).kind);
System.out.println("t3k : "+ getToken(3).kind);
}
// semantic lookahead : check that next tokens are not < B > and < EOF >
LOOKAHEAD({ getToken(1).kind != TestConstants.B || getToken(2).kind != TestConstants.EOF })
(
< A >
| < B >
)
)+
}
 
void Entry() :
{}
{
AorBnotBandEOF() < B > < EOF >
}
 


De : J.Chris Findlay [mailto:j.chris.findlay@...]
Envoyé : mercredi 8 juillet 2009 00:02
À : users@...
Objet : Re: [JavaCC] backtracking in JavaCC

In JavaCC one often uses semantic lookahead instead of backtracking.  Not sure if it will help with what you have or not..

On Wed, Jul 8, 2009 at 8:38 AM, David Portabella Clotet <david@...> wrote:
Hello,

One quick question;
Does JavaCC implement backtracking?
According to the JavaCC FAQ website, lookahead does not implement backtracking.

so I cannot parse this input "ABAAAB" with the following program (see attached the full code):
void Entry() : {}
{
( LOOKAHEAD(2) (<A> | <B>) )+
<B> 
}

note: I understand that I could rewrite this program to make it work without backtracking;
however this is a simplified program illustrating the problem that I have with a more complex one which does need backtracking.


In the regular expression world, backtracking is so simple: this regex would do the job "[AB]+B".
Is there a way to tell JavaCC to consider backtracking?

Many thanks,
DAvid Portabella



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...



--
- J.Chris Findlay
  (c:

Re: backtracking in JavaCC

by David Portabella Clotet-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

Thanks for the answers!

++++++++++++++++++++
TOKEN : { <A: "A"> | <B: "B"> }

void Entry() : {} {
  ( LOOKAHEAD( {getToken(2).kind != EOF} )   (<A> | <B>) )+
  <B> <EOF>
}
++++++++++++++++++++

However with this LOOKAHEAD, I have the feeling that I am telling JavaCC how to do its job.
Is there a reason (possible an ambiguity?) why JavaCC could not discover this by itself? other parses do that.
It is ok for this simplified case, but for my current case it becomes difficult.


Regards,
DAvid


On Wed, Jul 8, 2009 at 11:08 AM, Mazas Marc <mmazas@...> wrote:
Hi
 
There is no backtracking in JavaCC, just lookahead.
One solution for your problem is to use semantic lookahead.
Here under is an example.
Regards
 

Marc MAZAS

Phone : +33 (0)4 72 18 47.19
Mobile : +33 (0)6 89 86 50 56

 

options
{
STATIC = false;
DEBUG_LOOKAHEAD = true;
}
 
PARSER_BEGIN
(Test)
package
foo.bt1;
import
java.io.*;
import
java.util.*;
 
public
class Test
{
public static void main(String args []) throws Exception
{
Reader r =
new StringReader("ABAAAB");
Test interpreter =
new Test(new TestTokenManager(new SimpleCharStream(r)));
interpreter.Entry();
}
}
 
PARSER_END
(Test)
 
TOKEN
:
{
< A : "A" >
|
< B : "B" >
}
 
void
AorBnotBandEOF() :
{}
{
(
{
// kind : < EOF > -> 0, < A > -> 1, < B > -> 2 : see TestConstants
System.out.println(
"t1k : "+ getToken(1).kind);
System.out.println(
"t2k : "+ getToken(2).kind);
System.out.println(
"t3k : "+ getToken(3).kind);
}
// semantic lookahead : check that next tokens are not < B > and < EOF >
LOOKAHEAD({ getToken(1).kind != TestConstants.B || getToken(2).kind != TestConstants.EOF })
(
< A >
|
< B >
)
)+
}
 
void
Entry() :
{}
{
AorBnotBandEOF()
< B > < EOF >
}
 


De : J.Chris Findlay [mailto:j.chris.findlay@...]
Envoyé : mercredi 8 juillet 2009 00:02
À : users@...
Objet : Re: [JavaCC] backtracking in JavaCC

In JavaCC one often uses semantic lookahead instead of backtracking.  Not sure if it will help with what you have or not..

On Wed, Jul 8, 2009 at 8:38 AM, David Portabella Clotet <david@...> wrote:
Hello,

One quick question;
Does JavaCC implement backtracking?
According to the JavaCC FAQ website, lookahead does not implement backtracking.

so I cannot parse this input "ABAAAB" with the following program (see attached the full code):
void Entry() : {}
{
( LOOKAHEAD(2) (<A> | <B>) )+
<B> 
}

note: I understand that I could rewrite this program to make it work without backtracking;
however this is a simplified program illustrating the problem that I have with a more complex one which does need backtracking.


In the regular expression world, backtracking is so simple: this regex would do the job "[AB]+B".
Is there a way to tell JavaCC to consider backtracking?

Many thanks,
DAvid Portabella



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...



--
- J.Chris Findlay
  (c: