Skip to content

Instantly share code, notes, and snippets.

@beta
Created December 2, 2015 12:20
Show Gist options
  • Save beta/e609fd6b8a54b89d8ee1 to your computer and use it in GitHub Desktop.
Save beta/e609fd6b8a54b89d8ee1 to your computer and use it in GitHub Desktop.
SyntaxAnalyzer
package me.kyouko.syntaxanalyzer;
import java.util.ArrayList;
import java.util.List;
public class SyntaxAnalyzer {
String input;
List<Token> tokens;
int pointer;
public SyntaxAnalyzer(String input) {
this.input = input;
this.pointer = 0;
}
public void startAnalyzing() {
parseInputIntoTokens();
procE();
System.out.println("Analysis succeeded.");
}
private void parseInputIntoTokens() {
tokens = new ArrayList<Token>();
String[] tags = input.replace(">", "").split("<");
for (String tag : tags) {
String[] parts = tag.replace(", ", ",").split(",");
if (parts[0].toLowerCase().equals("id")) {
tokens.add(new Token(Token.Type.ID));
} else if (parts[0].toLowerCase().equals("num")) {
tokens.add(new Token(Token.Type.NUM));
} else if (parts[0].toLowerCase().equals("pa")) {
tokens.add(new Token(Token.Type.PARENTHESIS, parts[1]));
} else if (parts[0].toLowerCase().equals("op")) {
tokens.add(new Token(Token.Type.OPERATOR, parts[1]));
}
}
}
private Token getCurrentToken() {
if (pointer >= tokens.size()) {
return null;
} else {
return tokens.get(pointer);
}
}
private void procE() {
System.out.println("Processing production E.");
procT();
if (getCurrentToken() != null &&
(getCurrentToken().isOperator() &&
(getCurrentToken().argEquals("+") ||
getCurrentToken().argEquals("-")))) {
this.pointer++;
procE();
}
}
private void procT() {
System.out.println("Processing production T.");
procF();
if (getCurrentToken() != null &&
(getCurrentToken().isOperator() &&
(getCurrentToken().argEquals("*") ||
getCurrentToken().argEquals("/")))) {
this.pointer++;
procT();
}
}
private void procF() {
System.out.println("Processing production F.");
if (getCurrentToken() == null) {
error();
} else {
if (getCurrentToken().isLeftParenthesis()) {
this.pointer++;
procE();
if (getCurrentToken().isRightParenthesis()) {
this.pointer++;
} else {
error();
}
} else if (getCurrentToken().isId() || getCurrentToken().isNum()) {
this.pointer++;
} else {
error();
}
}
}
private void error() {
System.err.println("Error!");
}
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("Missing argument.");
showUsage();
System.exit(1);
}
new SyntaxAnalyzer(args[0]).startAnalyzing();
}
private static void showUsage() {
System.out.println("Usage:");
System.out.println(" SyntaxAnalyzer <input>");
}
}
package me.kyouko.syntaxanalyzer;
public class Token {
public enum Type {
ID, NUM, PARENTHESIS, OPERATOR
}
private Type type;
private Object arg;
public Token() {}
public Token(Type type) {
assert (type == Type.ID || type == Type.NUM);
this.type = type;
}
public Token(Type type, Object arg) {
assert (type == Type.PARENTHESIS || type == Type.OPERATOR);
this.type = type;
this.arg = arg;
}
public boolean isId() {
return (this.type == Type.ID);
}
public boolean isNum() {
return (this.type == Type.NUM);
}
public boolean isParenthesis() {
return (this.type == Type.PARENTHESIS);
}
public boolean isLeftParenthesis() {
return (this.isParenthesis() && this.argEquals("("));
}
public boolean isRightParenthesis() {
return (this.isParenthesis() && this.argEquals(")"));
}
public boolean isOperator() {
return (this.type == Type.OPERATOR);
}
public boolean argEquals(String arg) {
return (this.arg != null && (this.arg.equals(arg)));
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public String getArgAsString() {
return (String) arg;
}
public Integer getArgAsInteger() {
return (Integer) arg;
}
public Character getArgAsCharacter() {
return (Character) arg;
}
public void setArg(Object arg) {
this.arg = arg;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment