Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
Dear all I am trying to implement the first two phases of a compiler;
tokenization and sematic analysis. I am stuck in an infinite loop and have not yet found a solution.
 
Basic idea
1.Read an input file character by character.
2.Store each character in character array
3.Call a function check to see if the character is a delimter
3a.if not a delimiter concatenation is performed such that a token is formed
3b.if a delimiter, first put the concatenated string into a string array and then put the delimiter.
4.Repeat step 1-3 until a space is found because a space determines end of line.
5.Repeat step 1-4 until the character array length reaches max.
 
HERE IS THE CODE:
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.lang.*;
public class FileCharacter {
   static  int token_index=-1;
   public static String [] token;
	
	public static void check(char [] char1_copy)
	{
	int k;
	token=new String [char1_copy.length+1];
	String temp="";
	char l=char1_copy[0];
int counter=-1;
	try
	{
	for(k=0;k<char1_copy.length-1;)>
		{
		
		{
		l= char1_copy[k];
		while((int) l!=32&&counter<=k)
		{
			
			
			l= char1_copy[k];
			if(((int)l>=65&&(int)l<=90)||((int)l>=97&&(int)l<=122))
			{
			temp=temp+l;
			System.out.println("kcharacter:"+k);
			k++;
			}
			
			else if((int)l==61||(int)l==44||(int)l==34)
			{
			System.out.println("other than"+k);	
			if(temp!="")
					{
					token_index=token_index+1;
					token[token_index]=temp;
					System.out.println(token_index+token[token_index]);	
			
					}
			token_index=token_index+1;
			String a=Character.toString(l);
			token[token_index]=a;
			System.out.println(token_index+token[token_index]);	
			temp="";
			k++;
			}
			
		}
	
	}
	}		
 
}
catch(Exception e)
	{
	System.out.println("mewo:");
	for(int i=0;i<token.length;i++)>
	{
	System.out.println(i+":"+token[i]);	
	}
	}	
		
}		 
  public static void main(String[] args) {
    File file = new File(args[0]);
    
    if (!file.exists()) {
      System.out.println(args[0] + " does not exist.");
      return;
    }
    if (!(file.isFile() && file.canRead())) {
      System.out.println(file.getName() + " cannot be read from.");
      return;
    }
    try {
      	FileInputStream fis = new FileInputStream(file);
      	char current;
      	int i=-1;
	while (fis.available() > 0) {
	current = (char) fis.read();
        	 i++;
	 System.out.println(i+":"+current);
	}
	 fis = new FileInputStream(file);
      	char [] char1=new char[i+1];
	int j=0;			 
	while(fis.available()>0){
	current = (char) fis.read();
        	char1[j]=current;
	j++;
               }
	check(char1);
	                     	
         }
	
     catch (IOException e) {
      e.printStackTrace();
    }
  }
}
 
 
INPUT FILE CONSIST OF, AS OF NOW
ch="y"
a=b
Posted 31-Dec-12 7:55am
Edited 31-Dec-12 16:49pm
v3
Comments
Sergey Alexandrovich Kryukov at 31-Dec-12 14:09pm
   
Did you mean "two phases" (2), in the question title? If this is typo, please fix.
—SA
manisha gupta at 31-Dec-12 21:51pm
   
yes ,yes I did was a typo....is my bad!
Sergey Alexandrovich Kryukov at 31-Dec-12 23:53pm
   
OK, thanks for fixing.
 
Also, I removed your "solution" post. You should not use "Add solution here" to post a part of your question, supplementary material or something. This kind of post is reserved only for the cases when you provide some help in response to some question in this forum. Posting inappropriate material as "solution" cannot help you, but it may cause down-votes or reports for abuse, which you don't want.
 
You should add you sample code (or whatever it was) to your question using "Improve question" (above). Also, sandwich the code fragments with the tags <pre lang="Java">. Don't forget to escape HTML entities. The common mistake is failure to escape < and >. A link "encode" on top of edit area helps here.
 
Happy New Year!
 
—SA

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

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100