0
votes

I am writing a program in java cc where i am making a compiler. I have written a code in .jj file where i have defined tokens and parser but at the end of the file the program is giving an error don't know why. Kindly help me to fix this.

void Start () : {}
{
  (
      INTEGER_CONSTANT
    | STRING_CONSTANT
    | LOGIC_CONSTANT
    | NOT
    | IF
    | END
    | SUB
    | LET
    | CALL
    | THEN
    | CASE
    | ELSE
    | INPUT
    | PRINT
    | SELECT
    | STATIC
    | IDENTIFIER
  )*
  <EOF>
}

I have the following error:

org.javacc.parser.ParseException: Encountered " "INTEGER_CONSTANT " "|" "| " "STRING_CONSTANT " "|" "| " "LOGIC_CONSTANT " "|" "| " "NOT " "|" "| " "IF " "|" "| " "END " "|" "| " "SUB " "|" "| " "LET " "|" "| " "CALL " "|" "| " "THEN " "|" "| " "CASE " "|" "| " "ELSE " "|" "| " "INPUT " "|" "| " "PRINT " "|" "| " "SELECT " "|" "| " "STATIC " "|" "| " "IDENTIFIER " ")" ") " "" " "" at line 91, column 7.

2
I think you just forgot to put angle brackets around the names of the token kinds. E.g., it should be <INTEGER_CONSTANT> instead of INTEGER_CONSTANT. - Theodore Norvell

2 Answers

1
votes

Assuming that you have the valid declarations for all the tokens(in lexical specification section) used in the parser method, you need to use these tokens inside angular brackets. See below

void Start () : {}
{
  (
      <INTEGER_CONSTANT>
    | <STRING_CONSTANT>
    | <LOGIC_CONSTANT>
    | <NOT>
    | <IF>
    | <END>
    | <SUB>
    | <LET>
    | <CALL>
    | <THEN>
    | <CASE>
    | <ELSE>
    | <INPUT>
    | <PRINT>
    | <SELECT>
    | <STATIC>
    | <IDENTIFIER>
  )*
  <EOF>
}

JavaCC parser rule declaration:

1.) Direct String :

In this case you can use the string directly inside the parser definition method but this needed to be enclosed within the double_quotes.

 void start() : {}
  { "a" }

2.) Using the token defined in the lexical specification section :

Here to define the rule using the lexical tokens that needed to be included within angular brackets.

 TOKEN:{
      < A : "a" >
     } 
     void start() : {}
      { <A>}

3.) Using in-line token defintion :

Here we can declare the token inside the parser rule definition. This is legal but make the grammar less readable.

void start() : {}
   { <A : "a" >}

4.) By calling another parser rule :

we can also define the rule by calling the another rule as below.

void start() : {}
  { A() }
void A() : {}
{ "a" }

Hope this will be useful...

-1
votes

Maybe it's because you used (...)* and maybe is (...)+ because with * you are saying 0 or many times. I think that's why the error.