-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFLParser.java
172 lines (165 loc) · 4.59 KB
/
FLParser.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.io.IOException;
import java.util.ArrayList;
/* The parser runs through the stream of tokens and checks for any syntactic errors that
* have occurred in the writing of the program. It will return true if the program is
* syntactically correct and false otherwise.
*/
public class FLParser
{
public static boolean parse(ArrayList<Pair> tokens) throws IOException
{
// This value will be returned depending on correctness.
boolean correct = true;
// Will hold the next token.
Pair next = new Pair();
// Will hold the type of the next token.
String type = "";
// Will count the number of sub and end keywords.
int subEnd = 0;
// Runs through every token in the stream and checks for syntax.
for(int i = 0; i < tokens.size(); i++)
{
// Gets the next token.
next = tokens.get(i);
// Get the type of the token.
type = next.getToken();
// Number
if(type.equals("-Number-"))
{
// Pushes the number onto the stack, requires no syntactic analysis.
}
// String
else if(type.equals("-String-"))
{
// Pushes a string onto the stack, requires no syntactic analysis.
}
// Character
else if(type.equals("-Character-"))
{
// Pushes a character onto the stack, requires no syntactic analysis.
}
// Label
else if(type.equals("-Label-"))
{
// Does not require any preceding or following tokens, always syntactically correct.
}
/* Keyword Jump
* The keyword jump must be followed by an identifier.
*/
else if(type.equals("-Jump-"))
{
// Move to the next token.
i++;
// Ensure there are tokens left, if not error.
if(i == tokens.size())
{
correct = false;
break;
}
// Get the values of the next tokens.
else
{
next = tokens.get(i);
type = next.getToken();
// If it is not an identifier give error, otherwise it is correct.
if(!type.equals("-Identifier-"))
{
correct = false;
break;
}
}
}
/* Keyword Branch
* Branch must be followed by an identifier or a keyword name.
*/
else if(type.equals("-Branch-"))
{
// Move to the next token.
i++;
// Ensure there are tokens left, if not error.
if(i == tokens.size())
{
correct = false;
break;
}
// Get the values of the next tokens.
else
{
next = tokens.get(i);
type = next.getToken();
// If it is non of the token types below than it is an error, otherwise it is correct.
if(!type.equals("-Identifier-") && !type.equals("-End-")
&& !type.equals("-Sub-") && !type.equals("-Jump-"))
{
correct = false;
break;
}
}
}
/* Keyword End
* End must be the closing section of a sub function. These functions can not be defined
* within each other. If it the counter goes below zero it means there is no sub function
* to end and an error is printed.
*/
else if(type.equals("-End-"))
{
// Ensure that this occurs at the end of a function.
subEnd = subEnd - 1;
// If there was no sub that belongs to this end it is a syntactic error.
if(subEnd < 0)
{
correct = false;
break;
}
}
/* Keyword Sub
* The keyword sub must be followed by an identifier that names the function. THe function
* body must also end with the keyword end. This section users a counter to ensure that end is
* found. No next subs are allowed.
*/
else if(type.equals("-Sub-"))
{
// Ensures that an end token is at the end of the function definition.
subEnd = subEnd + 1;
i++;
// Ensures there are tokens left to check and that there is no next subs.
if(i == tokens.size() || subEnd > 1)
{
correct = false;
break;
}
// Otherwise it gets the next token and checks if it is an identifier.
else
{
next = tokens.get(i);
type = next.getToken();
if(!type.equals("-Identifier-"))
{
correct = false;
break;
}
}
}
// Identifier
else if(type.equals("-Identifier-"))
{
// Pulls and pushes items off of and onto the stack, requires no syntactic analysis.
}
// Variable
else
{
/* A variable must be loaded onto the stack or it must be saved. Syntactically it does not
* need to be followed or preceded by any tokens. Semantically this is untrue but unnecessary
* to define at this point.
*/
}
}
// Ensures that all the subs have been ended, if they have not it is an error.
if(subEnd > 0)
{
correct = false;
}
// Returns whether the program is syntactically correct or not depending on what was found.
return correct;
}
}