Search code examples
flex-lexerjflex

JFlex : Reading from multiple files


I recently started using JFlex and came across the need to read from multiple input files in a specific order.When it finishes the first file i want Jflex to continue with its current state and the scanner to continue from the new file.

So far everything works fine when I have only 1 input file :

br = new BufferedReader(new FileReader("input1"));
Flexer scanner = new Flexer(br);
scanner.yylex();
br.close();
//lame attempt for second input(not working)
br = new BufferedReader(new FileReader("input2"));
scanner.yylex();
br.close();

Solution

  • JFlex doesn't appear to support yywrap(), which is how you do this in lex and flex, but the easy way to do this is as follows:

    InputStream in = new SequenceInputStream(...);
    BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
    Flexer scanner = new Flexer(br);
    

    where the ... provides you several ways to specify the input files, either as an Enumeration<? extends InputStream> or as a pair of InputStreams: note that you can use the latter recursively, e.g.

    new SequenceInputStream(s1, new SequenceInputStream(s2,s3));