Skip to content

Commit 7880d59

Browse files
Merge branch 'tree_growing' into master
2 parents 6725ed8 + 148c1ea commit 7880d59

File tree

11 files changed

+957
-740
lines changed

11 files changed

+957
-740
lines changed

Starting_Point.java

Lines changed: 435 additions & 434 deletions
Large diffs are not rendered by default.

parser/Semantic_Actions.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package parser;
2+
3+
/**
4+
<b>
5+
Purpose: Semantic actions to be stored and used by {@link Tree_Assembler}}<br>
6+
Programmer: Gabriel Toban Harris<br>
7+
Date: 2021-08-04
8+
</b>
9+
*/
10+
11+
public enum Semantic_Actions
12+
{
13+
START, DECK, DECK_START, DECK_LIST, MORE_CARDS, CARD, CARD_NAME, PROBABILITY, PROBABILITY_START, SCENARIO_LIST, MORE_SCENARIOS, SCENARIO, SCENARIO_NAME, TREE, TREE_START, EXPR,
14+
UNARY_EXPR, UNARY_OPERATOR, PRIMARY_EXPR, CONDITION_CARD_START, CONDITION_CARD_END, CONDITION_SCENARIO_START, CONDITION_SCENARIO_END, CONDITION_EXPR_START, CONDITION_EXPR_END,
15+
BINARY_EXPR, BINARY_OPERATOR, DISPLAY, DISPLAY_START, DISPLAY_VALUE, SENTINEL_START, SENTINEL_END, SEMI_COLON, ASSIGN, SCENARIO_POP, CONDITION_CARD_POP, CONDITION_SCENARIO_POP,
16+
BINARY_POP_3,
17+
}

parser/Simulatation.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package parser;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import structure.Scenario;
6+
7+
/**
8+
<b>
9+
Purpose: final step which performs the actual simulation.<br>
10+
Programmer: Gabriel Toban Harris<br>
11+
Date: 2021-08-04
12+
</b>
13+
*/
14+
15+
public class Simulatation<T, U>
16+
{
17+
/**
18+
* Main deck which the hand will be generated from.
19+
*/
20+
private final ArrayList<T> DECK;
21+
22+
/**
23+
* Stores the generated scenarios.
24+
*/
25+
private final HashMap<String, Scenario<U>> FOREST;
26+
27+
/**
28+
* Constructor, note creates a shallow copy.
29+
*
30+
* @param DECK which hands will be created from
31+
* @param FOREST the {@link Scenario} objects which will be tested.
32+
*/
33+
public Simulatation(final ArrayList<T> DECK, final HashMap<String, Scenario<U>> FOREST)
34+
{
35+
//shallow copy do to intended internal use.
36+
this.DECK = DECK;
37+
this.FOREST = FOREST;
38+
}
39+
40+
/**
41+
* Performs simulation.
42+
*
43+
* @param HAND_SIZE of the hand which will be used in the simulation
44+
* @param TEST_HAND_COUNT number of times to run simulation
45+
* @return the result of the simulation
46+
*/
47+
public String simulate(final int HAND_SIZE, final int TEST_HAND_COUNT)
48+
{
49+
//TODO: finish
50+
throw new UnsupportedOperationException("Not finished.");
51+
}
52+
}

parser/Token.java

Lines changed: 89 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,89 @@
1-
package parser;
2-
3-
/**
4-
<b>
5-
Purpose: Types of Tokens to be created by Lexical Analyzer.<br>
6-
Programmer: Gabriel Toban Harris <br>
7-
Date: 2021-07-26, 2021-7-30
8-
</b>
9-
*/
10-
11-
public class Token
12-
{
13-
/**
14-
* Types to be used to identify token.
15-
*
16-
* SPECIAL_SEPERATOR is an enum solely to divide the properly formed tokens from the error tokens. Thus should not be used outside of this enum.
17-
* Thus < SPECIAL_SEPERATOR is properly formed, > SPECIAL_SEPERATOR is error token, and == SPECIAL_SEPERATOR is itself.
18-
*
19-
*/
20-
public static enum Lexeme_Types
21-
{
22-
DECK_START, ID, SEMI_COLON, PROBABILITY_START, SENTINEL_START, SENTINEL_END, ASSIGN, TREE_START, NOT, CONDITION_CARD_START, CONDITION_CARD_END,
23-
CONDITION_SCENARIO_START, CONDITION_SCENARIO_END, CONDITION_EXPR_START, CONDITION_EXPR_END, OR, AND, XOR, DISPLAY_START, TRUE, FALSE, LINE_COMMENT, BLOCK_COMMENT,
24-
SPECIAL_SEPERATOR, UNKNOWN_CHARACTER_ERROR, ID_ERROR, BLOCK_COMMENT_ERROR;
25-
26-
/**
27-
* Compares SPECIAL_SEPERATOR with INPUT. Expecting it to be -1, so that value < 0 is true.
28-
*
29-
* @param INPUT value to be compared against, expected not to be {Lexeme_Types.SPECIAL_SEPERATOR}
30-
* @return comparison < 0
31-
*/
32-
public static boolean is_error(final Lexeme_Types INPUT)
33-
{
34-
return Lexeme_Types.SPECIAL_SEPERATOR.compareTo(INPUT) < 0;
35-
}
36-
37-
}
38-
39-
private final Token.Lexeme_Types TYPE;
40-
private final long LINE_NUMBER;
41-
private final String LEXEME;
42-
43-
/**
44-
* Constructor of Token.
45-
*
46-
* @param TYPE of LEXEME
47-
* @param LINE_NUMBER location in source file
48-
* @param LEXEME content of lexeme which is then trimmed by {@link String#trim()}
49-
*/
50-
public Token(final Token.Lexeme_Types TYPE, final long LINE_NUMBER, final String LEXEME)
51-
{
52-
this.TYPE = TYPE;
53-
this.LINE_NUMBER = LINE_NUMBER;
54-
this.LEXEME = LEXEME.trim();
55-
}
56-
57-
//getters
58-
/**
59-
* @return the LEXEME TYPE
60-
*/
61-
public Token.Lexeme_Types get_type()
62-
{
63-
return this.TYPE;
64-
}
65-
66-
/**
67-
* @return the LINE_NUMBER
68-
*/
69-
public long get_line_number()
70-
{
71-
return this.LINE_NUMBER;
72-
}
73-
74-
/**
75-
* @return the LEXEME
76-
*/
77-
public String get_lexeme()
78-
{
79-
return this.LEXEME;
80-
}
81-
82-
/*
83-
* String version of class.
84-
*/
85-
public String toString()
86-
{
87-
return "[Lexeme TYPE: " + this.get_type() + ", LEXEME: " + get_lexeme() + ", line number: " + this.get_line_number() + "]";
88-
}
89-
}
1+
package parser;
2+
3+
/**
4+
<b>
5+
Purpose: Types of Tokens to be created by {@link Tokenizer}.<br>
6+
Programmer: Gabriel Toban Harris <br>
7+
Date: 2021-07-26, 2021-7-30
8+
</b>
9+
*/
10+
11+
public class Token
12+
{
13+
/**
14+
* Types to be used to identify token.
15+
*
16+
* SPECIAL_SEPERATOR is an enum solely to divide the properly formed tokens from the error tokens. Thus should not be used outside of this enum.
17+
* Thus < SPECIAL_SEPERATOR is properly formed, > SPECIAL_SEPERATOR is error token, and == SPECIAL_SEPERATOR is itself.
18+
*
19+
*/
20+
public static enum Lexeme_Types
21+
{
22+
DECK_START, ID, SEMI_COLON, PROBABILITY_START, SENTINEL_START, SENTINEL_END, ASSIGN, TREE_START, NOT, CONDITION_CARD_START, CONDITION_CARD_END,
23+
CONDITION_SCENARIO_START, CONDITION_SCENARIO_END, CONDITION_EXPR_START, CONDITION_EXPR_END, OR, AND, XOR, DISPLAY_START, TRUE, FALSE, LINE_COMMENT, BLOCK_COMMENT,
24+
SPECIAL_SEPERATOR, UNKNOWN_CHARACTER_ERROR, ID_ERROR, BLOCK_COMMENT_ERROR;
25+
26+
/**
27+
* Compares SPECIAL_SEPERATOR with INPUT. Expecting it to be -1, so that value < 0 is true.
28+
*
29+
* @param INPUT value to be compared against, expected not to be {Lexeme_Types.SPECIAL_SEPERATOR}
30+
* @return comparison < 0
31+
*/
32+
public static boolean is_error(final Lexeme_Types INPUT)
33+
{
34+
return Lexeme_Types.SPECIAL_SEPERATOR.compareTo(INPUT) < 0;
35+
}
36+
37+
}
38+
39+
private final Token.Lexeme_Types TYPE;
40+
private final long LINE_NUMBER;
41+
private final String LEXEME;
42+
43+
/**
44+
* Constructor of Token.
45+
*
46+
* @param TYPE of LEXEME
47+
* @param LINE_NUMBER location in source file
48+
* @param LEXEME content of lexeme which is then trimmed by {@link String#trim()}
49+
*/
50+
public Token(final Token.Lexeme_Types TYPE, final long LINE_NUMBER, final String LEXEME)
51+
{
52+
this.TYPE = TYPE;
53+
this.LINE_NUMBER = LINE_NUMBER;
54+
this.LEXEME = LEXEME.trim();
55+
}
56+
57+
//getters
58+
/**
59+
* @return the LEXEME TYPE
60+
*/
61+
public Token.Lexeme_Types get_type()
62+
{
63+
return this.TYPE;
64+
}
65+
66+
/**
67+
* @return the LINE_NUMBER
68+
*/
69+
public long get_line_number()
70+
{
71+
return this.LINE_NUMBER;
72+
}
73+
74+
/**
75+
* @return the LEXEME
76+
*/
77+
public String get_lexeme()
78+
{
79+
return this.LEXEME;
80+
}
81+
82+
/*
83+
* String version of class.
84+
*/
85+
public String toString()
86+
{
87+
return "[Lexeme TYPE: " + this.get_type() + ", LEXEME: " + get_lexeme() + ", line number: " + this.get_line_number() + "]";
88+
}
89+
}

parser/Tree_Assembler.java

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package parser;
2+
3+
import java.io.PrintWriter;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import structure.Evaluable;
7+
import structure.Scenario;
8+
9+
/**
10+
<b>
11+
Purpose: assembles the parts of a configuration file for simulating.<br>
12+
Programmer: Gabriel Toban Harris<br>
13+
Date: 2021-08-04
14+
</b>
15+
*/
16+
17+
//TODO: figure out how to effectively handle VERBOSE, as would like to not recheck it, maybe just use lambda as function pointer.
18+
public class Tree_Assembler<T, U>
19+
{
20+
/**
21+
* Determines whether to output files, true for make files and false for no file creation.
22+
*/
23+
public final boolean VERBOSE;
24+
25+
/**
26+
* Derivation of the input.
27+
*/
28+
private StringBuilder derivation;
29+
30+
/**
31+
* stack like structure used by {@link Tree_Assembler#parse(Token)}
32+
*/
33+
private ArrayList<Semantic_Actions> semantic_stack;
34+
35+
/**
36+
* Represents stack like structure for building the {@link Scenario} and deck list, used by {@link Tree_Assembler#parse(Token)}
37+
*/
38+
private ArrayList<Evaluable<U>> syntactical_stack;
39+
40+
/**
41+
* Main deck which the hand will be generated from.
42+
*/
43+
private final ArrayList<T> DECK;
44+
45+
/**
46+
* Stores the generated scenarios.
47+
*/
48+
private final HashMap<String, Scenario<U>> FOREST;
49+
50+
/**
51+
* file to output syntactical errors to.
52+
*/
53+
private PrintWriter syntactical_error_output;
54+
55+
/**
56+
* file to output derivation
57+
*/
58+
private PrintWriter syntactical_derivation_output;
59+
60+
/**
61+
* Default constructor.
62+
*/
63+
public Tree_Assembler()
64+
{
65+
this.VERBOSE = false;
66+
this.DECK = new ArrayList<T>(40); //predicted average deck size
67+
this.FOREST = new HashMap<String, Scenario<U>>(10); //predicted average number of scenarios
68+
this.finish_construction();
69+
}
70+
71+
/**
72+
* Parameterized constructor. Which is implicitly setting {@link #VERBOSE} to true.
73+
*
74+
* @param EXPECTED_DECK_SIZE helps with memory management, but can be wrong
75+
* @param EXPECTED_SCENARIO_COUNT helps with memory management, but can be wrong
76+
* @param SYNTACTICAL_ERROR_OUTPUT {@link #syntactical_error_output}
77+
* @param SYNTACTICAL_DERIVATION_OUTPUT {@link #syntactical_derivation_output}
78+
*/
79+
public Tree_Assembler(final int EXPECTED_DECK_SIZE, final int EXPECTED_SCENARIO_COUNT, final PrintWriter SYNTACTICAL_ERROR_OUTPUT, final PrintWriter SYNTACTICAL_DERIVATION_OUTPUT)
80+
{
81+
this.VERBOSE = true;
82+
this.DECK = new ArrayList<T>(EXPECTED_DECK_SIZE);
83+
this.FOREST = new HashMap<String, Scenario<U>>(EXPECTED_SCENARIO_COUNT);
84+
this.derivation = new StringBuilder(Semantic_Actions.START.name());
85+
this.syntactical_error_output = SYNTACTICAL_ERROR_OUTPUT;
86+
this.syntactical_derivation_output = SYNTACTICAL_DERIVATION_OUTPUT;
87+
this.finish_construction();
88+
}
89+
90+
/**
91+
* True output of this object.
92+
*
93+
* @return culmination of this class, destroy the object afterwards
94+
*/
95+
public Simulatation<T, U> creat_result()
96+
{
97+
return new Simulatation<T, U>(this.DECK, this.FOREST);
98+
}
99+
100+
/**
101+
* Method for parsing tokens into an abstract syntax tree. Uses all data members.
102+
* Make sure to call {@link #finish_semantic_stack} once all the {@link Token} are are parsed.
103+
* @param INPUT current top token being looked at
104+
* @throws EmptySemanticStackException when internal semantic_stack is empty yet there is another token to parse.
105+
* @throws TerminalMatchException {@link #match_subroutine}
106+
*/
107+
public void parse(final Token INPUT) //throws EmptySemanticStackException, TerminalMatchException
108+
{
109+
//TODO: write
110+
}
111+
112+
/**
113+
* Centralize shared constructor code. Should only be called once by the constructor and never again.
114+
*/
115+
private void finish_construction()
116+
{
117+
this.semantic_stack = new ArrayList<Semantic_Actions>();
118+
this.semantic_stack.add(Semantic_Actions.START); // starting symbol
119+
this.syntactical_stack = new ArrayList<Evaluable<U>>();
120+
}
121+
}

0 commit comments

Comments
 (0)