Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

urgent please help...look at code

 
pradeep arum
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am trying to build a lexical analyser here,but when i try to do the below code ,i think i am unable to find an END OF FILE character ,please help
thanks
//i had only the text yy in the in.txt file
//it goes into infinite loop,is there a problem with seek()?
public class MARK {
public static void main(String[] args) throws IOException {
RandomAccessFile in = new RandomAccessFile("in.txt","r");
int c,i=0,flag_T=0,j=0;
char cc;
long pos=0;
while ((c = in.read()) != -1){
System.out.println("ch="+(char)c);
char ch=(char)c;
while (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9'){
pos=in.getFilePointer();
c=in.read();
}
in.seek(pos);
System.out.println("2");
}//while
}//main
}//MARK
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this code, it terminates OK with output
ch=y
2
what is the infinite loop you mentioned ?

Originally posted by pradeep arum:
Hi all,
I am trying to build a lexical analyser here,but when i try to do the below code ,i think i am unable to find an END OF FILE character ,please help
thanks
//i had only the text yy in the in.txt file
//it goes into infinite loop,is there a problem with seek()?
public class MARK {
public static void main(String[] args) throws IOException {
RandomAccessFile in = new RandomAccessFile("in.txt","r");
int c,i=0,flag_T=0,j=0;
char cc;
long pos=0;
while ((c = in.read()) != -1){
System.out.println("ch="+(char)c);
char ch=(char)c;
while (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9'){
pos=in.getFilePointer();
c=in.read();
}
in.seek(pos);
System.out.println("2");
}//while
}//main
}//MARK
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a logical error. If your file contains "yy", this program will halt. But it contains, for example, "yy\n", you'll get an infinite loop. Imagine that you've read both y's. In the inner while loop, you read the "\n" and break out of the loop. "pos" has been set to immediately before the newline. Now, you start the outer loop again. The condition of the outer loop reads the "\n" character again. Because it's not a character or digit, the inner while loop is not entered. Therefore, we fall through to the seek() call, resetting the file so that the "\n" will be read again, next. There's your infinite loop!
 
pradeep arum
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ernest,
this is going into an infinite loop ,in.txt contains abcde ,please help,anybody.
pradeep

import java.io.*;
public class MARK {
public static void main(String[] args) throws IOException {
RandomAccessFile in = new RandomAccessFile("in.txt","r");
int c,i=0,flag_T=0,j=0;
char cc;
long pos=0;
while ((c = in.read()) != -1){
System.out.println("ch4="+(char)c);
char ch=(char)c;
while ((c >= 'a' && c <= 'z' )|| (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')){
c=in.read();
System.out.println("ch="+(char)c);
}
pos=in.getFilePointer();
in.seek(pos-1);//here i am trying to go one character back to get to
// the EOF character
System.out.println("2");
}//while
}//main
}//MARK
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please learn how to use *code* tags.
Why are you trying to use two loops to do this? That confuses me. Which loop are you stuck in? Adding print statements will help determine that. Knowing that may help you solve the problem.
Look up 'System.getProperty("line.separator")' for something like this (notice the nice *code* tags):
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think i am unable to find an END OF FILE character

I think I side-tracked myself. You are asking about "end of file". Your outer loop handles that correctly.

This will leave when it sees that end of file.
The problem is that you have another loop inside of this (I think).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic