Is it possible to test a lexer made in JFlex without writing a parser?

1.4k Views Asked by At

I am beginning to use JFlex and I want to try to write a lexer first, and then move onto the parser. However, it seems like there is no way to test your JFlex lexer without writing a parser in CUP as well.

All I want to do is write a lexer, give it an input file and then output the lexemes to check that it read everything correctly. Later I would like to output the tokens, but lexemes would be a good start.

1

There are 1 best solutions below

0
On

Yes It is possible to write a standalone scanner. You can read the details on this page. If you specify %standalone directive, it will add main method to the generated class. You can mention input files as command line arguments to run this program. jflex tar comes with an examples directory you can find one standalone example inside examples/standalone-maven/src/main/jflex directory. For quick reference I am posting one example code here

/**
   This is a small example of a standalone text substitution scanner 
   It reads a name after the keyword name and substitutes all occurences 
   of "hello" with "hello <name>!". There is a sample input file 
   "sample.inp" provided in this directory 
*/
package de.jflex.example.standalone;

%%

%public
%class Subst
%standalone

%unicode

%{
  String name;
%}

%%

"name " [a-zA-Z]+  { name = yytext().substring(5); }
[Hh] "ello"        { System.out.print(yytext()+" "+name+"!"); }