-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler2.java
More file actions
144 lines (124 loc) · 3.95 KB
/
Copy pathcompiler2.java
File metadata and controls
144 lines (124 loc) · 3.95 KB
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
import java.util.*;
// Token types
enum TokenType {
KEYWORD,
IDENTIFIER,
LITERAL,
OPERATOR,
SYMBOL
}
class Token {
TokenType type;
String value;
public Token(TokenType type, String value) {
this.type = type;
this.value = value;
}
}
class Lexer {
String sourceCode;
List<Token> tokens;
public Lexer(String sourceCode) {
this.sourceCode = sourceCode;
this.tokens = new ArrayList<>();
tokenize();
}
private void tokenize() {
Set<String> keywords = Set.of("if", "else", "while", "for");
Set<String> operators = Set.of("+", "-", "*", "/", "=", ">", "<");
Set<String> symbols = Set.of("(", ")", "{", "}", ";");
int i = 0;
while (i < sourceCode.length()) {
char c = sourceCode.charAt(i);
if (Character.isWhitespace(c)) {
i++;
continue;
}
if (Character.isLetter(c)) {
StringBuilder sb = new StringBuilder();
while (i < sourceCode.length() && (Character.isLetterOrDigit(sourceCode.charAt(i)) || sourceCode.charAt(i) == '_')) {
sb.append(sourceCode.charAt(i++));
}
String word = sb.toString();
if (keywords.contains(word)) {
tokens.add(new Token(TokenType.KEYWORD, word));
} else {
tokens.add(new Token(TokenType.IDENTIFIER, word));
}
}
// Numeric Literals
else if (Character.isDigit(c)) {
StringBuilder sb = new StringBuilder();
while (i < sourceCode.length() && Character.isDigit(sourceCode.charAt(i))) {
sb.append(sourceCode.charAt(i++));
}
tokens.add(new Token(TokenType.LITERAL, sb.toString()));
}
// Operators
else if (operators.contains(String.valueOf(c))) {
tokens.add(new Token(TokenType.OPERATOR, String.valueOf(c)));
i++;
}
// Symbols
else if (symbols.contains(String.valueOf(c))) {
tokens.add(new Token(TokenType.SYMBOL, String.valueOf(c)));
i++;
}
// Unknown characters
else {
System.err.println("Unrecognized character: " + c);
i++;
}
}
}
}
// Parser class
class Parser {
List<Token> tokens;
int currentIndex;
public Parser(List<Token> tokens) {
this.tokens = tokens;
this.currentIndex = 0;
}
public void parse() {
while (currentIndex < tokens.size()) {
Token token = tokens.get(currentIndex);
switch (token.type) {
case KEYWORD -> parseKeyword(token);
case IDENTIFIER -> parseIdentifier(token);
case LITERAL -> parseLiteral(token);
case OPERATOR -> parseOperator(token);
case SYMBOL -> parseSymbol(token);
}
currentIndex++;
}
}
private void parseKeyword(Token token) {
System.out.println("Keyword: " + token.value);
}
private void parseIdentifier(Token token) {
System.out.println("Identifier: " + token.value);
}
private void parseLiteral(Token token) {
System.out.println("Literal: " + token.value);
}
private void parseOperator(Token token) {
System.out.println("Operator: " + token.value);
}
private void parseSymbol(Token token) {
System.out.println("Symbol: " + token.value);
}
}
// Main compiler class
public class compiler2{
public static void main(String[] args) {
String sourceCode = """
if (x > 5) {
y = x * 2;
}
""";
Lexer lexer = new Lexer(sourceCode);
Parser parser = new Parser(lexer.tokens);
parser.parse();
}
}