• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Interpreter: Parsar Program

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the parser class, I need to use parseProgram() to take a filename as a parameter, opens and read the contents of the file, and returns an ArrayList of Statement objects.
Since I am new to Java, I do not know where to start from.
I also like to know is the ParseLine method so far is right.

Thanks,

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Parser
{

//Here is I struggle, I have no idea how to use parseProgram to store into an array list
public static ArrayList<Statement> parseProgram(String filename)
throws IOException
{

}

// parseLine() takes a line from the input file and returns a Statement
// object of the appropriate type. This will be a handy method to call
// within your parseProgram() method.
private static Statement parseLine(String line)
{
// Scanners can be used to break Strings into pieces, too!
Scanner scanner = new Scanner(line);
// The next() method returns the next word out of the String.
// use the first word to decide on a statement type.
String statementType = scanner.next();

Statement statement = null;
29.

if (statementType.equals("LET"))
{
char variable = scanner.next().charAt(0);
int value = scanner.nextInt();

statement = new LetStatement(variable, value);
}

return statement;
}
}
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Will you please UseCodeTags next time? Also, please add some indentations when using code tags. I would have added the code tags for you but without indentation it won't make much sense.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic