|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
|
||
|
|
Re: backtracking in JavaCCIn 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:
-- - J.Chris Findlay (c: |
||
|
|
RE: backtracking in JavaCCHi
There is no backtracking in JavaCC, just
lookahead.
One solution for your problem is to use semantic
lookahead.
Here under is an example.
Regards
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 >
}
|
||
|
|
Re: backtracking in JavaCCHello,
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:
|
| Free embeddable forum powered by Nabble | Forum Help |