javacup

wrong output in parser generator CUP


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


Solution

  • 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
        ;