0

The program is broken into two parts: the lexical analyzer and the syntax analyzer plus semantic. The lexical analyzer should recognize the token using DFA and return the token and the value. The syntax and semantic part can be done by using any algorithm that can check for the correct syntax (from lectures Recursive Descent Parsing or predictive parsing compound with semantic).The language that will be used as source program (calculator) will contain only the operations add, subtract, multiply, divide, power and minus unary operation.The language declares two types of variable, integer and real.Your source program will be saved in a file as text and you have to read a file and group the token. You have to used space to separate between tokens.

I have managed the code for the reading and writing files, but couldnt proceed further.

[  /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.File;
import java.lang.IllegalStateException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.io.FileNotFoundException;     
import java.lang.SecurityException;       
import java.util.Formatter;               
import java.util.FormatterClosedException;
import java.util.InputMismatchException;

/**
 *
 * @author
 */
public class ReadFile 
{
    private Scanner input;
    private Formatter output;
    private char[] readResponse;
    private String lowerString;
    public void openFile()
       {
         try
            {
             input = new Scanner( new File( "inputtext.txt" ) );
            } 
	 catch ( FileNotFoundException fileNotFoundException )
            {
             System.err.println( "Error opening file." );
             System.exit( 1 );
            } 
       }
    public void createFile()
    {
        try
	    {
	     output = new Formatter( "outputText.txt" );
	    }
	catch ( SecurityException securityException )
	    {
	    System.err.println("You do not have write access to this file." );
            System.exit( 1 );
	    }		      
        catch ( FileNotFoundException filesNotFoundException )
            {
            System.err.println( "Error creating file." );
            System.exit( 1 );
            }
    }
    public void outputText()   
    {
    try 
	{        
            while ( input.hasNext() )
            {
                    lowerString = input.next().toLowerCase();
                    readResponse = lowerString.toCharArray();
            for (int index = 0; index < readResponse.length; index++) 
                {
                char c = readResponse[index];
                output.format("%s%n" , c);
                //output.format('\n');
                }   
            }
            
        }
    catch ( NoSuchElementException elementException )
        {
            System.err.println( "File improperly formed." );
            input.close();
            System.exit( 1 );
        }
      catch ( IllegalStateException stateException )
      {
          System.err.println( "Error reading from file." );
          System.exit( 1 );
      }
      catch ( SecurityException securityException )
	    {
	    System.err.println("You do not have write access to this file." );
            System.exit( 1 );
	    }		      
      }
    public void closeFile()
    {
        if ( input != null )
            input.close();
        if ( output != null )
            output.close();
    } 
    public static void main(String[] args)
    {
        ReadFile application = new ReadFile();
        application.openFile();
        application.createFile();
        application.outputText();
        application.closeFile();
    }

}
2
Contributors
1
Reply
4
Views
9 Years
Discussion Span
Last Post by jwenting
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.