Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Sierra

Thread: Display Line of Code in Output (Probably very simple but driving me crazy!)

  1. #1
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default Display Line of Code in Output (Probably very simple but driving me crazy!)

    Hello everyone. I am writing a code for school that is supposed to be a lexical analyzer of code. He gave us a sample brick and then we were supposed to have it broken up. I can break it up and put everything in the correct spots except display the location (line in code view). Would anyone be able to assist me with this? Any help would be greatly appreciated thanks!

    Java Code:
    import java.util.*;
    public class LexAnalysis {
    	
    	public static void main(String[] args)
    	{
    		java.util.Locale.setDefault(new java.util.Locale("en"));
    		System.out.println("We will now lexically analyze the following code: ");
    		System.out.println();
    		
    		String a="001  private static void interchange(int i, int [] array)";
    		String b="003  int temp;";
    		String c="004  temp=array[i];";
    		String d="005  array[i]=array[i+1];";
    		String e="006  array[i+1]=temp;";
    		System.out.println(a);
    		System.out.println("002  {");
    		System.out.println(b);
    		System.out.println(c);
    		System.out.println(d);
    		System.out.println(e);
    		System.out.println("007  }");
    		
    		System.out.println();
    		
    		System.out.println("Location \tToken \t\tData Type \t Operation \t Reserved");
    		System.out.println();
    		String array[]={a+b+c+d+e};
    		String keywords[]={"void", "private", "int", "static", "class"};
    		
    		String operations[]={"+", "-", "&", "||", "[", "]","="};
    		
    		for (String w: array)
    		{
    			for(String x:keywords)
    			{
    				if (w.contains(x))
    				{
    					System.out.println("303\t\t" + x + "\t \t\t\t\t\t x");
    				}				
    			}
    			for (String y: operations)
    			{
    				if(w.contains(y))
    				{
    				System.out.println("303\t\t" + y + "\t\t\t\t x");
    				}
    			}
    			
    			if (w.contains("temp"))
    			{
    				System.out.println("303\t\ttemp \t\tinteger \t");
    			}
    				
    		}
    	}
    }
    OUTPUT

    We will now lexically analyze the following code:

    001 private static void interchange(int i, int [] array)
    002 {
    003 int temp;
    004 temp=array[i];
    005 array[i]=array[i+1];
    006 array[i+1]=temp;
    007 }

    Location Token Data Type Operation Reserved

    303 void x
    303 private x
    303 int x
    303 static x
    303 + x
    303 [ x
    303 ] x
    303 = x
    303 temp integer


    The idea is to get the location be the line of the code. Any ideas on how to go about this? I tried google and nothing has worked

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Display Line of Code in Output (Probably very simple but driving me crazy!)

    How about
    int myLocation = myString.indexOf(String otherString)
    DMarsh12 likes this.
    I like likes!

Similar Threads

  1. Simple java conversion driving me crazy!!
    By New2J@v@ in forum New To Java
    Replies: 7
    Last Post: 06-14-2012, 04:27 AM
  2. Driving me crazy!!!! Please help!!
    By Quizzle23 in forum New To Java
    Replies: 33
    Last Post: 02-24-2011, 09:00 AM
  3. The null message is driving me crazy
    By Yakg in forum New To Java
    Replies: 5
    Last Post: 12-02-2010, 08:03 PM
  4. image does not refresh-driving me crazy
    By jambon in forum AWT / Swing
    Replies: 1
    Last Post: 04-09-2010, 05:25 PM
  5. THIS PROGRAM IS DRIVING ME CRAZY!!! help fixing it
    By syntrax in forum New To Java
    Replies: 2
    Last Post: 12-18-2009, 05:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •