JLex/CUP issue

Mark <mark4@voila.fr>
6 Aug 2001 04:06:24 -0400

          From comp.compilers

Related articles
JLex/CUP issue mark4@voila.fr (Mark) (2001-08-06)
| List of all articles for this month |
From: Mark <mark4@voila.fr>
Newsgroups: comp.compilers
Date: 6 Aug 2001 04:06:24 -0400
Organization: Club-Internet (France)
Keywords: Java, parse, question
Posted-Date: 06 Aug 2001 04:06:23 EDT

Hi !
I've got a problem while trying to make JLex and CUP work
together. I've just taken a basic example and tried to read from a
file instead of the standard input. Here are the files :


<---------------------------- Test.lex ---------------------------->
package Test;


import java_cup.runtime.Symbol;
%%
%cup
%%
";" { return new Symbol(sym.SEMI); }
"+" { return new Symbol(sym.PLUS); }
"*" { return new Symbol(sym.TIMES); }
"(" { return new Symbol(sym.LPAREN); }
")" { return new Symbol(sym.RPAREN); }
[0-9]+ { return new Symbol(sym.NUMBER, new Integer(yytext())); }
[ \t\r\n\f] { /* ignore white space. */ }
. { System.err.println("Illegal character: "+yytext()); }
<-------------------------------------------------------------------->


<---------------------------- Test.cup ---------------------------->
package Test;
import java.io.*;
import java_cup.runtime.*;


parser code
{:
    public static void main(String args[]) throws Exception
    {
        new parser(new Yylex(new FileReader(args[0]))).parse();
      }
:}


terminal SEMI, PLUS, TIMES, LPAREN, RPAREN;
terminal Integer NUMBER;


non terminal expr_list, expr_part;
non terminal Integer expr;


precedence left PLUS;
precedence left TIMES;


expr_list ::= expr_list expr_part | expr_part;
expr_part ::= expr SEMI;
expr ::= NUMBER
                        | expr PLUS expr
                        | expr TIMES expr
                        | LPAREN expr RPAREN
                          ;
<-------------------------------------------------------------------->


The problem comes from the line :
                new parser(new Yylex(new FileReader(args[0]))).parse();
It causes a NullPointerException, but if I replace it with :
                new parser(new Yylex(System.in)).parse();
then everything works fine ! I really don't understand why.
If someone could help me with that bug, I would be very grateful.
Thanks in advance,
Mark


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.