Created
August 10, 2025 13:45
-
-
Save DanielAugusto191/9f8c8fcd2afcb2f10b97eda31be45a0a to your computer and use it in GitHub Desktop.
Random lexer in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.File; | |
| import java.io.FileNotFoundException; | |
| import java.util.Scanner; | |
| import java.util.Vector; | |
| enum tokenType { | |
| ADD, | |
| SUB, | |
| MULT, | |
| SLASH, | |
| NUMBER, | |
| STRING, | |
| BOOL, | |
| NIL, | |
| }; | |
| class objType { | |
| tokenType type; | |
| public int num; | |
| public String str; | |
| public boolean bool; | |
| objType(int pnum) { | |
| type = tokenType.NUMBER; | |
| this.num = pnum; | |
| } | |
| objType(String pstr) { | |
| type = tokenType.STRING; | |
| this.str = pstr; | |
| } | |
| objType(boolean pbool) { | |
| type = tokenType.BOOL; | |
| this.bool = pbool; | |
| } | |
| objType() { | |
| type = tokenType.NIL; | |
| } | |
| } | |
| class Token { | |
| public tokenType type; | |
| String lexeme; | |
| objType literal; | |
| Token(tokenType pType, String pLexeme, objType pLiteral) { | |
| this.type = pType; | |
| this.lexeme = pLexeme; | |
| this.literal = pLiteral; | |
| } | |
| }; | |
| class Lexer { | |
| Lexer() { | |
| } | |
| public Vector<Token> read(String source) { | |
| Vector<Token> tokens = new Vector<>(); | |
| int start = 0; | |
| for (int i = 0; i < source.length();) { | |
| char c = source.charAt(i); | |
| start = i; | |
| ++i; | |
| switch (c) { | |
| case '+': | |
| tokens.add(new Token(tokenType.ADD, "+", new objType())); | |
| break; | |
| case '-': | |
| tokens.add(new Token(tokenType.SUB, "-", new objType())); | |
| break; | |
| case '*': | |
| tokens.add(new Token(tokenType.MULT, "*", new objType())); | |
| break; | |
| case '/': | |
| tokens.add(new Token(tokenType.SLASH, "/", new objType())); | |
| break; | |
| case '\r': | |
| case '\t': | |
| case ' ': | |
| break; | |
| default: | |
| String s = ""; | |
| s = source.substring(start, i); | |
| tokens.add(new Token(tokenType.NUMBER, s, new objType(Integer.parseInt(s)))); | |
| } | |
| } | |
| return tokens; | |
| } | |
| } | |
| public class a { | |
| public static void main(String[] args) { | |
| if (args.length < 1 || args.length > 1) { | |
| return; | |
| } | |
| System.out.println("File name: " + args[0]); | |
| String source = ""; | |
| try { | |
| File fileObj = new File(args[0]); | |
| Scanner reader = new Scanner(fileObj); | |
| while (reader.hasNextLine()) | |
| source += reader.nextLine(); | |
| reader.close(); | |
| } catch (FileNotFoundException e) { | |
| e.printStackTrace(); | |
| } | |
| System.out.println(source); | |
| Lexer L = new Lexer(); | |
| Vector<Token> tokens = L.read(source); | |
| for (int i = 0; i < tokens.size(); ++i) { | |
| System.out.println("Token " + i + ": " + tokens.get(i).lexeme); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment