Plex
Plex

Reputation: 21

CUP parser returns syntax error for a valid input

I'm trying to write a very simple parser. I'm using JFlex with Java CUP. Here's my code:

LEX file:

import java_cup.runtime.*;


%%

%class Lexer
%line
%column
%cup


%{   
    /*********************************************************************************/
    /* Create a new java_cup.runtime.Symbol with information about the current token */
    /*********************************************************************************/
    private Symbol symbol(int type)               {return new Symbol(type, yyline, yycolumn);}
    private Symbol symbol(int type, Object value) {return new Symbol(type, yyline, yycolumn, value);}

%}



%%


<YYINITIAL> {
<<EOF>>             { return symbol(sym.EOF); }
"|"                 { return symbol(sym.PIPE); } 

}

CUP file:

import java_cup.runtime.*;


terminal PIPE;

non terminal myrule;   

myrule  ::=  PIPE {: RESULT = 42; :};

Main.java

import java.io.FileReader;

public class Main {

    public static void main(String[] args) throws Exception {
        CUP_FILECup parser = new CUP_FILECup(new Lexer(new FileReader(args[0])));
        parser.debug_parse();
    }

}

As you can see, I tried to make it as simple as I could yet, I get the following error for the input file containing only one character: "|".

Syntax error at character 0 of input

But clearly we defined a proper derivation for "|".

Why is it happening?

EDIT:
- "start with myrule;" doesn't help

Upvotes: 2

Views: 6750

Answers (1)

Shane Lu
Shane Lu

Reputation: 1104

Could you please try FileInputStream instead of FileReader?

(If it works, you may want to take a look at https://stackoverflow.com/a/5155255/1415645)

And you could also try only the lexer first.

Upvotes: 1

Related Questions