2

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

3
  • could there be a BOM in the input file? msdn.microsoft.com/en-us/library/windows/desktop/… Commented Nov 29, 2016 at 14:02
  • As far as I can tell, no. Commented Nov 29, 2016 at 14:09
  • I used file - and got ASCII text (I'm using Ubuntu) Commented Nov 29, 2016 at 14:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.