I want to create a token that accepts only even numbers, and I need to test it in javacc, I'm just starting to use it and I can't think of any way to test this token, can anyone help me, thanks!
TOKEN: //I want this token to only accept even numbers
{
<TOKENPARANUMEROSPARES: (["0"-"9"])* ("0"|"2"|"4"|"6"|"8") > {System.out.println("Soy un numero par");}
}
void llamarNumeroPar ():
{}
{
//I want my program to be able to launch a message from my main with the message shown below
< TOKENPARANUMEROSPARES > {System.out.println ("soy un numeroPar");}
}
To print something, what do you recommend? Do I do it from the main or what could I correct in this part of my code?
Let's say you have the following
parser.jjfile:You can now write a main class:
And... surprise: the output is "Even". This is because it has read "123456" which is indeed an even number.
Now add a second token
ODD:And modify the main class as follows:
This time, the output is as expected:
Consider defining a token
NUMBERand dealing with its parity in the Java code.