Results 1 to 17 of 17
  1. #1
    Fadly Massere is offline Member
    Join Date
    Nov 2013
    Location
    Jakarta
    Posts
    12
    Rep Power
    0

    Default Lexical analyzer or just count character

    the main goal is read a text file that named "input.txt". The contains "input.txt" just a statement like this : A=B*(C+D)-15+8*CD
    Write code in java, that can write an output but in another text file that called "output.txt".So, the output not in command line. output.txt explained like this :

    The Contains of input.txt is :

    operator :
    ( total : 1
    ) total : 1
    * total : 2
    + total : 2
    - total : 1
    / total : 0
    = total : 1

    variabel :
    A total : 1
    B total : 1
    C total : 1
    D total : 1
    CD total : 1

    digit
    15 : 1

    Here Is my code for read file
    Java Code:
    import java.io.*;
    public class compiler_AhmadFadlyDj_2010470121 {
    public String bacaFile(String namaFile) //To read a txt.file
    {
    String returnValue= "";
    FileReader file = null;
    String line = "";
    try{
    file = new FileReader(namaFile);
    BufferedReader reader = new BufferedReader(file);
    and then this my code for write the output.txt
    Java Code:
    public void TulisFile(String namaFile, String s ) // This is method to write File 
    {
        FileWriter output = null;
        try
         {
           output = new FileWriter(namaFile);
           BufferedWriter writer = new BufferedWriter(output);
           writer.write(s); 
           writer.flush(); 
           writer.close();}
    CD is a unitary character (or string perhaps ?),
    15 is also counted as a single entity..
    Totally I'm lost how to write that output...
    What I'm supposed to do ?

    and this the main method
    Java Code:
    public static void main(String[] args) {
        compiler_AhmadFadlyDj_2010470121 test = new compiler_AhmadFadlyDj_2010470121();
        String input = test.bacaFile("input.txt");
        System.out.println(input);
        test.TulisFile("output.txt", input);
       }
    }
    Last edited by Fadly Massere; 11-16-2013 at 05:34 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Lexical analyzer or just count character

    You haven't explained how the program is to parse the contents of the input file to create the output file.

    For testing, put the expression in a String in the program and leave out the code for reading it.
    Java Code:
    String theExpression = " A=B*(C+D)-15+8*CD";
    Also for testing print the results on the console so you can quickly and easily see what the code is doing.

    When the logic of the program can create the desired output, then work on reading the input from a file and writing the output to a file. Do not mix the logic for solving the problem with the code for reading and writing files.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Fadly Massere is offline Member
    Join Date
    Nov 2013
    Location
    Jakarta
    Posts
    12
    Rep Power
    0

    Default Re: Lexical analyzer or just count character

    Quote Originally Posted by Norm View Post
    You haven't explained how the program is to parse the contents of the input file to create the output file.

    For testing, put the expression in a String in the program and leave out the code for reading it.
    Java Code:
    String theExpression = " A=B*(C+D)-15+8*CD";
    Also for testing print the results on the console so you can quickly and easily see what the code is doing.

    When the logic of the program can create the desired output, then work on reading the input from a file and writing the output to a file. Do not mix the logic for solving the problem with the code for reading and writing files.
    Thanks dude, I know the method that i will use. Parsing with split.
    My new code like this
    Java Code:
      public void TulisFile(String namaFile, String s ) // This is method to write File
    {
         String theExpression = "A=B*(C+D)-15+8*CD";
         String[] array = theExpression.split("");
         
         FileWriter output = null;
         try
         {
             for (int i=0; i < array.length; i++)
    	 {
    	 output = new FileWriter(namaFile);
             BufferedWriter writer = new BufferedWriter(output);
    
             writer.write(array[i]);
    
             writer.flush();
             writer.close();
    	 }
         }
    needs help to solve this

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Lexical analyzer or just count character

    It looks to me like want to count the tokens in the input expression. Check out the Map interface and some of its implementations (e.g. HashMap). Try a few examples and you will probably see how to do this.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Lexical analyzer or just count character

    Quote Originally Posted by jim829 View Post
    It looks to me like want to count the tokens in the input expression. Check out the Map interface and some of its implementations (e.g. HashMap). Try a few examples and you will probably see how to do this.
    A Map can do part of the job but it is of no use when you're reading an identifier (name) or number. Also, if valid tokens have at least two characters, you have to be careful, but it still can be done.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Fadly Massere is offline Member
    Join Date
    Nov 2013
    Location
    Jakarta
    Posts
    12
    Rep Power
    0

    Default Re: Lexical analyzer or just count character

    So, Hashmap can solve this problem ?

    #thanks before for all of you are from Indonesia...

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Lexical analyzer or just count character

    Quote Originally Posted by Fadly Massere View Post
    So, Hashmap can solve this problem ?
    No it can not; it can only solve part of your problem (it is good for your token set, reserved words or any other finit set of tokens).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Lexical analyzer or just count character

    Look at the StreamTokenizer class. It will do some of the parsing.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Fadly Massere is offline Member
    Join Date
    Nov 2013
    Location
    Jakarta
    Posts
    12
    Rep Power
    0

    Default Re: Lexical analyzer or just count character

    Quote Originally Posted by Norm View Post
    Look at the StreamTokenizer class. It will do some of the parsing.
    approximately like this :

    to write file
    Java Code:
       public void TulisFile(String namaFile, String s ) // This is method to write File
    {
         String theExpression = "A=B*(C+D)-15+8*CD";
         try	 
            {
    			    
    			
    			FileOutputStream out = new FileOutputStream("output.txt");
    			ObjectOutputStream oout = new ObjectOutputStream(out);
    				 
    				 
    				 oout.writeUTF(theExpression);
    			     oout.flush(); 
    
                     
    				ObjectInputStream ois = new ObjectInputStream(new FileInputStream("output.txt"));
    				
    				 
    				 Reader r = new BufferedReader (new InputStreamReader(ois));
    				 StreamTokenizer st = new StreamTokenizer(r);
    				 
    
    				 
                     System.out.println("The contains input.txt adalah :");
    				 boolean eof = false;
    				 do 
    				   {
    					  int token = st.nextToken();
    						switch (token)
    					{
    					   case StreamTokenizer.TT_EOF:
    						 System.out.println("The End of Dying");
    						 eof = true;
    						 break;
    					   case StreamTokenizer.TT_EOL:
    						 System.out.println("End of Line");
    						 break;
    					   case StreamTokenizer.TT_WORD:
    						 System.out.println("Operand :" + st.sval);
    						  break;
    					  case StreamTokenizer.TT_NUMBER:
                             System.out.println("Digit : " + st.nval);
                             break;
                             
    					   default :
    						 System.out.println( " Operator : " + (char) token  );
    							if (token == '!') {
    							eof = true;
    						}
    
    					}
    				   } while (!eof);
    stuck, why java is so hard ?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Lexical analyzer or just count character

    What is the program's output? Where are you stuck?

    Look at using an instance of the StringReader class in the StreakTokenizer class's constructor. No need to write to and file and read it back.
    Last edited by Norm; 11-17-2013 at 08:09 PM.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Fadly Massere is offline Member
    Join Date
    Nov 2013
    Location
    Jakarta
    Posts
    12
    Rep Power
    0

    Default Re: Lexical analyzer or just count character

    Quote Originally Posted by Norm View Post
    What is the program's output? Where are you stuck?

    Look at using an instance of the StringReader class in the StreakTokenizer class's constructor. No need to write to and file and read it back.
    the output is

    ==> Operand : A
    ==> Operator : =
    ==> Operand : B
    ==> Operator : *
    ==> Operator : (
    ==> Operand : C
    ==> Operator : +
    ==> Operand : D
    ==> Operator : )
    ==> Digit : -15.0
    ==> Operator : +
    ==> Digit : 8.0
    ==> Operator : *
    ==> Operand : CD

    The all code is looked like this
    Java Code:
    import java.io.*;
    
    public class compiler_AhmadFadlyDj_2010470121 {
    public String bacaFile(String namaFile) //To read a txt.file
    {
        String returnValue = "";
    	File file = new File(namaFile);
    		FileInputStream fis = null;
     		try {
    			fis = new FileInputStream(file);
     
    			int content;
    			while ((content = fis.read()) != -1) {
    				// convert to char and display it
    				System.out.print((char) content);
    			}
     
    		} 
    }
    
    
    public void TulisFile(String namaFile, String s ) // This is method to write File
    {
         String theExpression = "A=B*(C+D)-15+8*CD";
       
           try	 
            {
              FileOutputStream out = new FileOutputStream("output.txt");
    	  ObjectOutputStream oout = new ObjectOutputStream(out);
    				 
    	 oout.writeUTF(theExpression);
    	 oout.flush(); 
    
    	ObjectInputStream ois = new ObjectInputStream(new FileInputStream("output.txt"));
    	
    	 Reader r = new BufferedReader (new InputStreamReader(ois));
    	 StreamTokenizer st = new StreamTokenizer(r);
    		 
                 System.out.println("The contains input.txt adalah :");
    		 boolean eof = false;
    		 do 
    		   {
    		  int token = st.nextToken();
    	          switch (token)
    		    {
    		      case StreamTokenizer.TT_EOF:
    		         System.out.println("Dan Derita Pun Berakhir...!!!");
    		         eof = true;
    		      break;
    		      
    		      case StreamTokenizer.TT_EOL:
    		        System.out.println("End of Line");
    		      break;
    		     
    		      case StreamTokenizer.TT_WORD:
    		        System.out.println("==> Operand  : " + st.sval);
    	             break;
    		
    		      case StreamTokenizer.TT_NUMBER:
                            System.out.println("==> Digit    : " + st.nval);
                          break;
                             
    		       default :
    			 System.out.println( "==> Operator : " + (char) token  );
    		          if (token == '!') {
    				eof = true;
    			      }
    
    			    }  
    			  } while (!eof);
    						 
    				 }catch (Exception ex){
    					ex.printStackTrace();
    				 }
    				 } 
    			
    
    public static void main(String[] args) {
        compiler_AhmadFadlyDj_2010470121 test = new compiler_AhmadFadlyDj_2010470121();
        String input = test.bacaFile("input.txt");
        System.out.println(input);
        test.TulisFile("output.txt", input);
       }
    }
    Stuck...

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Lexical analyzer or just count character

    That output looks like the StreamTokenizer has done the parsing you were looking for.
    Stuck
    Where are you having problems?
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Fadly Massere is offline Member
    Join Date
    Nov 2013
    Location
    Jakarta
    Posts
    12
    Rep Power
    0

    Default Re: Lexical analyzer or just count character

    see, how to print the result looked like this :

    operator :
    ( total : 1
    ) total : 1
    * total : 2
    + total : 2
    - total : 1
    / total : 0
    = total : 1

    variabel :
    A total : 1
    B total : 1
    C total : 1
    D total : 1
    CD total : 1

    digit
    15 : 1
    8 : 1

    my output is redicilious.
    how bout -15, it's count a digit ?
    it should be : "-" is a operator and "15" is a digit...

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Lexical analyzer or just count character

    What data is being printed in that print out? Where and how can the program collect that data it needs to make that print out?
    From post#11
    he output is

    ==> Operand : A
    ==> Operator : =
    ==> Operand : B
    ==> Operator : *
    ==> Operator : (
    ==> Operand : C
    ==> Operator : +
    ==> Operand : D
    ==> Operator : )
    ==> Digit : -15.0
    ==> Operator : +
    ==> Digit : 8.0
    ==> Operator : *
    ==> Operand : CD
    Can the data be collected from where that print out came from?

    The - in -15.0 is a unary operator. Its usage is different. It goes with the number vs a binary operator that takes two operands and generates a result.
    Last edited by Norm; 11-19-2013 at 07:01 PM.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Lexical analyzer or just count character

    Quote Originally Posted by Fadly Massere View Post
    see, how to print the result looked like this :

    operator :
    ( total : 1
    ) total : 1
    * total : 2
    + total : 2
    - total : 1
    / total : 0
    = total : 1

    variabel :
    A total : 1
    B total : 1
    C total : 1
    D total : 1
    CD total : 1

    digit
    15 : 1
    8 : 1

    my output is redicilious.
    how bout -15, it's count a digit ?
    it should be : "-" is a operator and "15" is a digit...
    Tokenizers are too stupid to parse expression grammars correctly; have you read my blog entries? (see the top right corner of this reply for a link); one article describes a rather complete infix expression parser and evaluator while another article takes the lazy approach, i.e. it leaves all the details to the embedded Javascript engine that comes bundled with Java >= 6.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Lexical analyzer or just count character

    Quote Originally Posted by Fadly Massere View Post
    stuck, why java is so hard ?
    The ease or difficulty of Java is irrelevant. What is challenging, depending on one's experience level, is the task at hand.

    Regards,
    Jim
    Last edited by jim829; 11-20-2013 at 12:42 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,096
    Rep Power
    6

    Default Re: Lexical analyzer or just count character

    Java isn't hard in itself - software engineering is hard. If you treat that as a problem rather than a fun challenge, it is time to go look for another line of work. Because if there is one thing that the past 20 years have shown, it is that no matter how much this field and its tools evolve it STAYS hard. No matter how much experience you gain and how good you get, there is ALWAYS something new that you have to learn.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. JPlex - lexical analyzer generator
    By kjkrum in forum Java Software
    Replies: 0
    Last Post: 05-08-2012, 05:16 AM
  2. Character Count
    By drucey in forum New To Java
    Replies: 8
    Last Post: 11-22-2010, 08:02 PM
  3. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  4. how to count the given character?
    By ashin in forum Advanced Java
    Replies: 1
    Last Post: 10-14-2009, 04:28 PM
  5. Replies: 5
    Last Post: 11-22-2008, 01:34 PM

Posting Permissions

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