'Making a parser using JavaCC, error handling

I am new to JavaCC and I am trying to write a parser for a specific simple programming language. I have a problem with dealing with errors, precisely ParserExceptions. I want to print specific error messages for specific violations. For example, every line of the code must start with a certain keyword, <KEYWORD: "KEY"> which consists of uppercase letters, but I also have a <FUNCNAME = (["A"-"Z"])+> token which also consists only of uppercase letters. I have defined earlier than , and right now I have:

void Start(): 
{ 
  try { 
  (
    (<KEYWORD> | {throw new ParseException("Keyword is expected");})
    function()
    <SPACE> 
    <EOL>
  )* 
  <EOF>
  } catch (ParseException e) {
    System.out.println(e.getMessage());
  }
}

I want to get "Keyword is expected" if the line of the code starts not with the keyword. However, I get:

Encountered " <FUNCNAME> "KIY "" at line 1, column 1.

How can I make the parser wait only for KEYWORD at the beginning of each line? Maybe I just don't understand the logic of all of that



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source