Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Hey Hi all, here is one Issue with JavaCC

843838Jan 3 2007 — edited Jan 3 2007
Hey All,

I have one javacc file with extenssion .jj ;
i want to parse one flat file which contains multiple records with fixed number of characters, i want that my parser class should return me an bean object for the record of that flat file.
I have confussion about the grammer which i have specified in my .jj file..
containt of my .jj file is as :>
how options {
  IGNORE_CASE = true;
  LOOKAHEAD=2;
  STATIC=false;
}

PARSER_BEGIN(TempScanner)
/**
 * TempScanner
 * @version %I%, %G%
 * Date June 30 2006
 * �Harbinger Systems Pvt. Ltd.
 */
package javaccParser;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.ArrayList;
 /**
* This class parses a CMS Plan Payment report and generates a bean out of it.
*/
public class TempScanner{
	private DemoClass demoObj;
	
	public static DemoClass importBean(String fileName) 
			throws FileNotFoundException,Exception {
		TempScanner scanner=new TempScanner(new FileInputStream(fileName));
		scanner.demoObj= new DemoClass();
		
		try{
			scanner.startImport();
		}catch(ParseException pe){
			 
			throw new Exception(pe.getMessage());
		}catch(TokenMgrError te){
			 
			throw new Exception(te.getMessage());
		}catch(Exception e){
			 
			throw new Exception(e.getMessage());
		}
		return scanner.demoObj;
	}
}

PARSER_END(TempScanner)

SKIP:
{
	"\r"|"\n"|" "
}

TOKEN:
{
	<NUM: ("-")?(["0"-"9"])([".",",","0"-"9"])*>
|	<ALPHA: (["a"-"z","A"-"Z"])>
|	<STR: (["a"-"z","A"-"Z","0"-"9",",",".","/","&","(",")"])+>
|	<USERID: "USER ID: " (["A"-"Z","a"-"z"](<ALPHA>){6})>
|	<USERNAME: "USER NAME: "(<ALPHA>){6}>
|	<DEPTID: "DEPT ID: "<NUM>>
}


void userId():
{ Token userId;
}
{
	userId=<USERID>{
		       demoObj.setUserId(userId.image);
	}
}


void userName():
{ Token userName;
}
{
	userName=<USERNAME>{
		       demoObj.setName(userName.image);
	}
}

void deptId():
{ Token deptId;
}
{
	deptId=<DEPTID>{
		     demoObj.setDept(deptId.image);
	}
	
}
void startImport():{}
{
	    userId()userName()deptId()
		<EOF>
}
 
Please anyone can go through it and please reply with enhancement/.

Thnks,
A. S. D.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 31 2007
Added on Jan 3 2007
1 comment
101 views