Created
May 20, 2021 10:42
-
-
Save JakWai01/6b67e3bffc63aa09da218456cf6c9385 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package appl_alg_interpreter; | |
import java.io.File; | |
public class ExpressionParser { | |
static ProgramScanner psc; | |
static String NAME = "[a-zA-Z_][a-zA-Z_0-9]*"; | |
static String NUMBER = "(^|\\s+)((\\-?[0-9]+(\\.[0-9]*)?)|(-?\\.[0-9]+))($|\\s+)"; | |
static String OPERATOR = "[\\+\\-\\*/]"; | |
static class Node { | |
public String value; | |
public Node left; | |
public Node right; | |
public Node(String value, Node left, Node right) { | |
this.value = value; | |
this.left = left; | |
this.right = right; | |
} | |
public static Node expr(ProgramScanner psc) throws Exception { | |
String token = psc.nextTokenOrNull(NUMBER); | |
if (token != null) { | |
return new Node(token, null, null); | |
} | |
token = psc.nextTokenOrNull("\\("); | |
Node left = Node.expr(psc); | |
token = psc.nextToken(OPERATOR); | |
String value = token; | |
Node right = Node.expr(psc); | |
token = psc.nextTokenOrNull("\\)"); | |
return new Node(value, left, right); | |
} | |
} | |
public static void main(String[] args) { | |
try { | |
psc = new ProgramScanner(new File("appls/appl_algs/Test.apl")); | |
Node result = Node.expr(psc); | |
System.out.println("Success"); | |
System.out.println(result.left.value); | |
System.out.println(result.right.value); | |
System.out.println(result.value); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well done :)