add resouces file
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
## 构建方式
|
||||
|
||||
1. ant
|
||||
2. java -jar out/CJCompiler.jar
|
||||
3. java lexer
|
||||
```
|
||||
ant run
|
||||
```
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
<target name="build">
|
||||
<mkdir dir="out/classes"/>
|
||||
<javac srcdir="src" destdir="out/classes" encoding="utf-8" classpathref="classpath" includeantruntime="false" fork="true">
|
||||
<javac srcdir="src" destdir="out/classes" encoding="utf-8" classpathref="classpath" includeantruntime="false" fork="true" sourcepath="src" debug="true">
|
||||
<compilerarg value="-J-Duser.language=en"/>
|
||||
</javac>
|
||||
</target>
|
||||
@@ -23,6 +23,17 @@
|
||||
<attribute name="Main-Class" value="com.compiler.Main"/>
|
||||
</manifest>
|
||||
<zipgroupfileset dir="lib" includes="*.jar"/>
|
||||
<fileset dir="res"/>
|
||||
</jar>
|
||||
</target>
|
||||
|
||||
<target name="run" depends="jar">
|
||||
<exec executable="java" >
|
||||
<arg value="-jar"/>
|
||||
<arg value="out/CJCompiler.jar"/>
|
||||
</exec>
|
||||
<exec executable="java">
|
||||
<arg value="lexer"/>
|
||||
</exec>
|
||||
</target>
|
||||
</project>
|
||||
@@ -2,22 +2,22 @@ package com.compiler.lexer;
|
||||
|
||||
import com.compiler.utils.CharSet;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.io.InputStream;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.compiler.utils.FileReader.getLines;
|
||||
|
||||
// 检测文法
|
||||
public class LexerTester {
|
||||
public LexerTester() {
|
||||
try {
|
||||
List<String> lines = Files.readAllLines(Paths.get("res/lexer.c"), StandardCharsets.US_ASCII);
|
||||
generateTokenList(lines);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
InputStream lexerStream = ClassLoader.getSystemResourceAsStream("lexer.c");
|
||||
List<String> lines = getLines(lexerStream);
|
||||
generateTokenList(lines);
|
||||
}
|
||||
|
||||
private void generateTokenList(List<String> lines) {
|
||||
|
||||
@@ -5,8 +5,18 @@ import com.compiler.utils.Pair;
|
||||
import com.compiler.utils.Show;
|
||||
import com.compiler.utils.StringSet;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
|
||||
import static com.compiler.utils.FileReader.getLines;
|
||||
|
||||
public class ParserTest extends LLParser {
|
||||
|
||||
public ParserTest() {
|
||||
@@ -81,9 +91,9 @@ public class ParserTest extends LLParser {
|
||||
}
|
||||
|
||||
private void initRules() {
|
||||
List<String> lines = FileReader.getLines("res/SimpleGrammar.txt");
|
||||
List<String> lines = getLines(ClassLoader.getSystemResourceAsStream("SimpleGrammar.txt"));
|
||||
rules = ParserGenerator.Read(lines);
|
||||
// Show.printRules(rules);
|
||||
// Show.printRules(rules);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package com.compiler.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class FileReader {
|
||||
public static List<String> getLines(String file){
|
||||
public static List<String> getLines(String file) {
|
||||
try {
|
||||
return Files.readAllLines(Paths.get(file), StandardCharsets.US_ASCII);
|
||||
|
||||
@@ -17,4 +19,30 @@ public class FileReader {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static List<String> getLines(File file) {
|
||||
List<String> result = new ArrayList<String>();
|
||||
try {
|
||||
Scanner scanner = new Scanner(file);
|
||||
while (scanner.hasNextLine()) {
|
||||
result.add(scanner.nextLine());
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static List<String> getLines(InputStream in) {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
||||
List<String> result = new ArrayList<>();
|
||||
try {
|
||||
while (reader.ready()) {
|
||||
result.add(reader.readLine());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user