diff options
| author | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-07-02 11:55:17 -0700 |
|---|---|---|
| committer | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-07-02 11:55:17 -0700 |
| commit | 6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 (patch) | |
| tree | ed97e39ec77c5231ffd2c394493e68d00ddac5a4 /Homework/cs5300/project-one/p1-scanner/scanner/TableReader.java | |
| download | misc-undergrad-6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6.tar.gz misc-undergrad-6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6.zip | |
Diffstat (limited to 'Homework/cs5300/project-one/p1-scanner/scanner/TableReader.java')
| -rw-r--r-- | Homework/cs5300/project-one/p1-scanner/scanner/TableReader.java | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/Homework/cs5300/project-one/p1-scanner/scanner/TableReader.java b/Homework/cs5300/project-one/p1-scanner/scanner/TableReader.java new file mode 100644 index 0000000..a074547 --- /dev/null +++ b/Homework/cs5300/project-one/p1-scanner/scanner/TableReader.java @@ -0,0 +1,160 @@ +/* + * Do not modify this file. + */ +package scanner; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +/** + * TableParser provides all functionality for parsing the table definition file. + * + * Do not modify this file. + */ +public final class TableReader { + + // CharCat class + // Used in the classifier table + public class CharCat { + + final private char c; + final private String category; + + public CharCat(char c, String category) { + this.c = c; + this.category = category; + } + + public char getC() { + return c; + } + + public String getCategory() { + return category; + } + + } + + // Transition class + // Used in the transition table + public class Transition { + + final private String fromState; + final private String category; + final private String toState; + + public Transition(String fromState, String category, String toState) { + this.fromState = fromState; + this.category = category; + this.toState = toState; + } + + public String getFromStateName() { + return fromState; + } + + public String getCategory() { + return category; + } + + public String getToStateName() { + return toState; + } + } + + // TokenType class + // Used in the token type table + public class TokenType { + + final private String state; + final private String type; + + public TokenType(String state, String type) { + this.state = state; + this.type = type; + } + + public String getState() { + return state; + } + + public String getType() { + return type; + } + } + + private enum WhichTable { + + CLASS, TRANS, TOKEN + }; + + private ArrayList<CharCat> classifier; + private ArrayList<Transition> transitions; + private ArrayList<TokenType> tokens; + + public TableReader(String filename) throws IOException { + parse(filename); + } + + public Iterable<CharCat> getClassifier() { + return classifier; + } + + public Iterable<Transition> getTransitions() { + return transitions; + } + + public Iterable<TokenType> getTokens() { + return tokens; + } + + public void parse(String fn) throws FileNotFoundException, IOException { + this.classifier = new ArrayList<>(); + this.transitions = new ArrayList<>(); + this.tokens = new ArrayList<>(); + + WhichTable table = WhichTable.CLASS; + try (BufferedReader br = new BufferedReader(new FileReader(fn))) { + String line; + while ((line = br.readLine()) != null) { + final int idx = line.indexOf("//"); + if (idx > -1) { + line = line.substring(0, idx); + } + line = line.trim(); + if (!line.isEmpty()) { + if (line.equals("ClassifierTable")) { + table = WhichTable.CLASS; + } else if (line.equals("TransitionTable")) { + table = WhichTable.TRANS; + } else if (line.equals("TokenTypeTable")) { + table = WhichTable.TOKEN; + } else { + String[] lineTokens = line.split(" "); + if (table == WhichTable.CLASS) { + char c = lineTokens[0].charAt(0); + if (lineTokens[0].equals("\\space")) { + c = ' '; + } else if (lineTokens[0].equals("\\t")) { + c = '\t'; + } else if (lineTokens[0].equals("\\n")) { + c = '\n'; + } + CharCat cc = new CharCat(c, lineTokens[1]); + classifier.add(cc); + } else if (table == WhichTable.TRANS) { + Transition t = new Transition(lineTokens[0], lineTokens[1], lineTokens[2]); + transitions.add(t); + } else if (table == WhichTable.TOKEN) { + TokenType tt = new TokenType(lineTokens[0], lineTokens[1]); + tokens.add(tt); + } + } + } + } + } + } +} |
