options{
STATIC=false;
}
PARSER_BEGIN(FLW)
import java.io.*;
public class FLW{
public static void main(String ar[]){
CustomerDTO dtos = getDTO("xxxx 40,dddddddddd eeeeeeeeeer");
System.out.println("First Name: "+dtos.getFName());
System.out.println("Last Name: "+dtos.getLName());
System.out.println("Address : "+dtos.getAddress());
}
static CustomerDTO getDTO(String inString){
Reader reader = new StringReader(inString);
FLW parser = new FLW(reader);
StringBuffer buf = new StringBuffer();
try{
return parser.parse();
}
catch(Exception e){
System.out.println("exception");
e.printStackTrace();
}
return null;
}
}
PARSER_END(FLW)
TOKEN:{<SPACE:" ">}
TOKEN:{<#COMMA:",">}
TOKEN:{<FIRST_NAME:(<LETTER>){4}>}
TOKEN:{<ADDRESS:(<NUMBER>){2}(<COMMA>){1}(<LETTER>){10}>}
TOKEN:{<LAST_NAME:(<LETTER>){11}>}
TOKEN:{<#LETTER:["a"-"z","A"-"Z"]>}
TOKEN:{<#NUMBER:["0"-"9"]>}
CustomerDTO parse():
{
Token fName;
Token lName;
Token address;
CustomerDTO cusDTO = new CustomerDTO();
}
{
(
((fName=<FIRST_NAME>
{cusDTO.setFName(fName.image);}))
<SPACE>
(address=<ADDRESS>)
{cusDTO.setAddress(address.image);}
<SPACE>
(lName=<LAST_NAME>)
{cusDTO.setLName(lName.image);}
)
{return cusDTO;}
}
Note that you have to first install JavaCC and also create the CustomerDTO which is in the default class path in the above example and store this in a file named xxx.jj. What the above code does is basically break down the string message passed in the main method and put the relevant data in the relvant attributes of the DTO. Note that JavaCC automatically hanldes EOF(End of file). Hope this helps anyone who is looking at how to use JavaCC for such scenario.