forked from darmiel/Compilerbau23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParantheseParserMain.java
33 lines (28 loc) · 1.14 KB
/
ParantheseParserMain.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
import java.io.OutputStreamWriter;
public class ParantheseParserMain {
public static void main(String[] args) throws Exception {
compiler.Lexer lexer = new compiler.Lexer();
OutputStreamWriter outStream = new OutputStreamWriter(System.out, "UTF-8");
lexer.init("(())");
parseS(lexer);
if (lexer.lookAhead().m_type == compiler.TokenIntf.Type.EOF) {
outStream.write("ACCEPTED");
} else {
outStream.write("FAILED");
}
outStream.flush();
}
public static void parseS(compiler.Lexer lexer) throws Exception {
compiler.Token currentToken = lexer.lookAhead();
if (currentToken.m_type == compiler.TokenIntf.Type.LPAREN) {
// S : (S)
lexer.expect(compiler.TokenIntf.Type.LPAREN);
parseS(lexer);
lexer.expect(compiler.TokenIntf.Type.RPAREN);
} else if (currentToken.m_type == compiler.TokenIntf.Type.RPAREN || currentToken.m_type == compiler.TokenIntf.Type.EOF) {
// S : epsilon
} else {
lexer.throwCompilerException("invalid paranthese expression", "");
}
}
}