December 10th, 2011, 10:25 AM
#1
Class Validator
I'm attempting to code a program that validates a file named as Class.txt, the contents of the file are: class test {
int i;
}
the thing is, I can't get past the first two words, class and test. Can anyone help me?
the code is as follows:
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Parser {
private FileReader content;
private int currentLineNumber = 1;
private int currentCharNumber = 0;
private int currentValue;
private char currentChar;
private String token;
public static void main( String[] args ) {
try {
new Parser( new File( "C:/Class.txt" ) ).parse();
} catch ( ParseException exc ) {
exc.printStackTrace();
} catch ( IOException exc ) {
exc.printStackTrace();
}
}
public Parser( File file ) throws IOException {
content = new FileReader( file );
}
public void parse() throws ParseException, IOException {
nextToken();
if ( token.equals( "class" ) ) {
System.out.println( token );
nextToken();
} else {
throw new ParseException( token, currentLineNumber, currentCharNumber );
}
identifier();
if ( token.equals( "{" ) ) {
System.out.println( token );
nextToken();
} else {
throw new ParseException( token, currentLineNumber, currentCharNumber );
}
type();
identifier();
if ( token.equals( "}" ) ) {
System.out.println( token );
nextToken();
} else {
throw new ParseException( token, currentLineNumber, currentCharNumber );
}
if ( token == null ) {
System.out.println( "file syntactically correct." );
}
}
private void type() throws ParseException, IOException {
if ( token.equals( "int" ) || token.equals( "string" ) || token.equals( "char" ) ) {
System.out.println( token );
nextToken();
} else {
throw new ParseException( token, currentLineNumber, currentCharNumber );
}
}
private void identifier() throws ParseException, IOException {
if ( token.matches( "^[a-zA-Z]+[a-zA-Z0-9]*" ) ) {
System.out.println( token );
nextToken();
} else {
throw new ParseException( token, currentLineNumber, currentCharNumber );
}
}
private void nextToken() throws IOException {
token = null;
while ( ( currentValue = content.read() ) != -1 ) {
currentChar = (char) currentValue;
if ( currentChar != ' ' && currentChar != '\n' && currentChar != ';' ) {
if ( token != null ) {
token += currentChar;
} else {
token = String.valueOf( currentChar );
}
currentCharNumber++;
} else {
if ( currentChar == ' ' ) {
currentCharNumber++;
break;
} else if ( currentChar == '\n' ) {
currentLineNumber++;
currentCharNumber = 1;
break;
}
}
}
}
}
public class ParseException extends Exception {
private String token;
private int line;
private int Char;
public ParseException( String token, int line, int Char ) {
this.token = token;
this.line = line;
this.Char = Char;
}
@Override
public String toString() {
return String.format(
"Error \"%s\" (line: %d, Char: %d)",
token, line, Char );
}
}
Attached Files
December 10th, 2011, 11:40 AM
#2
Re: Class Validator
Have you considered using a Doclet in conjunction with the JavaDoc program. JavaDoc parses java files and exposes the constituent parts to the plugin Doclet.
There are various on-line articles on writing Doclets such as this one
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width