0

I have some problem when i use the JAVA parser generator CUP, and I don't know why, could somebody help me?

here is the cup file:

import java_cup.runtime.*;    

/* Terminals (tokens returned by the scanner). */    
terminal FCONST;    
terminal IDENTIFIER;    
terminal STRING_DEFINITION;    
terminal ASSIGN;    
terminal OPEN_SQUARE_BRACKET;    
terminal CLOSE_SQUARE_BRACKET;

/* Non-terminals */    
non terminal program;    
non terminal explicit_value;    
non terminal const_array_list_value;

/* Top level rules */    
program ::=
    FCONST IDENTIFIER ASSIGN explicit_value
    ;

explicit_value ::=
    OPEN_SQUARE_BRACKET const_array_list_value CLOSE_SQUARE_BRACKET
    |
    STRING_DEFINITION:e 
    {:
        System.out.printf("explicit_value %s \n", e);
    :}
    ;

const_array_list_value ::=
    explicit_value
    |
    const_array_list_value explicit_value
    ;

and when parse the "const aaa = ["a", "b", "c"]", the output is:

explicit_value b

explicit_value c

explicit_value c

esrrhs
  • 1
  • 2

1 Answers1

0

I find it is CUP bug, and I change my code to bison.

%{

import java.io.*;

%}

%pure_parser

%error_verbose

%token FCONST;
%token IDENTIFIER;
%token STRING_DEFINITION;
%token ASSIGN;
%token OPEN_SQUARE_BRACKET;
%token CLOSE_SQUARE_BRACKET;
%token SPLIT;


%%

program:    FCONST IDENTIFIER ASSIGN explicit_value
       ;

explicit_value :
    OPEN_SQUARE_BRACKET const_array_list_value CLOSE_SQUARE_BRACKET
    |
    STRING_DEFINITION
    {
        System.out.printf("explicit_value %s \n", ((ParserVal)($1)).sval);
    }
    ;

const_array_list_value :
    explicit_value
    |
    const_array_list_value explicit_value
    ;
esrrhs
  • 1
  • 2
  • 1
    Do you have any explanation or evidence of the bug? Also I know this is your own question, but solving it in bison doesn't solve it in cup. – Ryan Leach Jun 30 '17 at 03:02
  • I am not sure it is cup's bug, but when I use bison the output is correct, and you can see the grammar is the same like cup – esrrhs Jul 05 '17 at 08:06