Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm making Lexical Analyzer and i got stuck in handling Comments .

I'm actually trying to handle that if "//" appears in the file.txt then that line should be ignored and loop must move to next line for more tokens , actually I'm reading the words from file and if white space occurs then that word should be separately stored in arraylist so that I can later compare the keywords with the identifiers etc.

Here is the code:
Java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;



public class lex {



	public static void main(String[] args) throws FileNotFoundException {
	
		List<String> al = new ArrayList<String>();		
			
			FileReader fin = new FileReader("C:\\Users\\PHLAMBY\\Desktop\\file.txt");
			Scanner s = new Scanner(fin);		  
		    
			 s.useDelimiter(" ");
			 
			int lineNum;
		    String A ;
		    
		    for(lineNum=1;s.hasNextLine();lineNum++){
				
					 A=s.next();
				 if(!A.contains("//")){					
					 al.add(A);
				 }
				 else
					 break;
		    }
		  
		  
		  for(int i =0 ;i<al.size(); i++){
			 System.out.println(al.get(i));
		  }	 		    
	}}


This is the text file :

{ }* +- ==abstract

// extends testing etc etc

Output :
{
}*
+-

Now, I'm not getting proper output there should be last keyword "==abstract" print but it is not seperating and getting printed and comment is not properly handled.

Thanks in advance!
Posted
Comments
Richard MacCutchan 27-Nov-14 12:46pm    
I just tried this and it worked fine.
User04454648 28-Nov-14 1:08am    
@Richard then why it is not showing "==abstract" on output ? as it is not in "//" line.
Richard MacCutchan 28-Nov-14 3:18am    
Here is the output from my test run of your code:


C:\Users\Richard\Documents\Java>java Zed
{
}*
+-
==abstract


C:\Users\Richard\Documents\Java>

It may be that my input file was not exactly the same as yours.

1 solution

Your use of the delimiter setting means that newlines will not be treated as token separators. Also the break statement terminates the loop, which may not be what you want. The code should be something like:
Java
Scanner s = new Scanner(fin);
int lineNum;
String A ;
for(lineNum=1; s.hasNext(); lineNum++) {
    A=s.next();
    if(!A.contains("//")){
        al.add(A);
    }
    else {
        s.nextLine(); // skip comment lines
    }
}

Using nextLine, as above, ensures that the loop continues to the end of the file.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900