-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionParser.java
58 lines (39 loc) · 970 Bytes
/
ExpressionParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* ExpressionParser
*
* version 1
*
* August 3, 2019
*
* Copyright © 2019 Sabrina Kim, Martial Pastor, Keven Presseau-St-Laurent, Marco Tropiano, Diana Zitting-Rioux.
* All rights reserved.
*/
import java.util.ArrayList;
/**
* Expression Parser for the calculator
*
* @version 1
* @author Martial Pastor
*
*/
public class ExpressionParser {
public static float calculate(String expr) {
if(expr.equals("exit")) {
System.exit(0);
}
expr = expr.replace(" ", "");
expr += "end~";
//lexical analysis
Lexer lexer = new Lexer(expr);
lexer.runLexer();
ArrayList<Token> tokenStream = lexer.getTokenStream();
lexer.printTokenStream();
//syntax analysis
//System.out.print("\n\nSYNTAX ANALYSIS");
Parser parser = new Parser(tokenStream);
Node ast = parser.run_Parser();
//Semantic actions and expression computing
Visitor visitor = new Visitor(ast);
return visitor.calculate();
}
}