Search code examples
parsingjavacc

How To Check Which Regular Expression Is Applied in JavaCC


I've the following regular expressions

TOKEN:{
  <CONSTANT:(<DIGIT>)+>
  | <ID:<LETTER>(<LETTER> | <DIGIT>)*>
  | <#DIGIT:["0"-"9"]>
  | <#LETTER:["a"-"z","A"-"Z","_"]>
}

Now I'd like to know how to check if the current token is ID or CONSTANT

public class eg1 {
  public static void main(String args[]) throws ParseException {
    eg1 parser = new eg1(System.in);
    Token token = parser.getNextToken();

    if(token is ID) System.out.print("Token is ID");
    else System.out.print("Token is CONSTANT");   
  }
}

How to express (token is ID) in JavaCC?

Thanks very much.


Solution

  • You have int ID defined in eg1Constants.java and that compares to token.kind, so you are looking for

    if (token.kind == eg1Constants.ID) System.out.print("Token is ID");

    For details, consult the JavaCC FAQ.