Skip to content

Commit

Permalink
add simplifier & cmd has lr gen
Browse files Browse the repository at this point in the history
  • Loading branch information
mesut146 committed Sep 27, 2021
1 parent d442c8b commit 563d64f
Show file tree
Hide file tree
Showing 31 changed files with 439 additions and 208 deletions.
3 changes: 2 additions & 1 deletion examples/calc.g
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ token{
NUM: [0-9]+;
}

%start = E;
//precedence goes from lower to higher
E: E ("+" | "-") E
| E ("*" | "/") E
| E "^" E
| "(" E ")"
| NUM;
| NUM;
54 changes: 46 additions & 8 deletions src/main/java/mesut/parserx/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import mesut.parserx.dfa.Validator;
import mesut.parserx.gen.LexerGenerator;
import mesut.parserx.gen.ll.RecDescent;
import mesut.parserx.gen.lr.AstBuilderGen;
import mesut.parserx.gen.lr.CodeGen;
import mesut.parserx.gen.transform.Factor;
import mesut.parserx.gen.transform.LeftRecursive;
import mesut.parserx.nodes.Node;
Expand All @@ -24,7 +26,7 @@
public class Main {

static List<String> cmds = Arrays.asList("-left", "-factor", "-epsilon",
"-optimize", "-dfa", "-nfa", "-nfa2dfa", "-regex", "-lr0", "-desc", "-lexer");
"-optimize", "-dfa", "-nfa", "-nfa2dfa", "-regex", "-lr0", "-desc", "-lexer", "-lalr");

static String usageStr = "usage:\n" +
"java -jar <jarfile> <command>\n" +
Expand All @@ -37,8 +39,10 @@ public class Main {
"-nfa [-dot] nfa from grammar\n" +
"-nfa2dfa [-optimize] nfa to dfa\n" +
"-regex nfa to regex\n" +
"-lexer [-out <path>] [-package <pkg>] [-lexerClass <cls>] [-lexerFunc <func>] [-tokenClass <cls>] generate just lexer\n" +
"-desc [-out <path>] [-package <pkg>] [-parserClass <cls>] [-astClass <cls>] [..lexer options] generate LL(1) recursive descent parser\n" +
"-lexer [-out <path>] [-package <pkg>] [-lexerClass <cls>] [-lexerFunc <func>] [-tokenClass <cls>] generates just lexer\n" +
"-desc [-out <path>] [-package <pkg>] [-parserClass <cls>] [-astClass <cls>] [..lexer options] generates LL(1) recursive descent parser\n" +
"-lalr [-out <path>] [-package <pkg>] [-parserClass <cls>] [-astClass <cls>] [..lexer options] generates lalr parser" +
"-lalr [-out <path>] [-package <pkg>] [-parserClass <cls>] [-astClass <cls>] [..lexer options] generates lr(1) parser" +
"\ninput is given by -in <path> or as last argument";

static void usage() {
Expand Down Expand Up @@ -72,7 +76,6 @@ else if (s.equals("-output") || s.equals("-out")) {
}
else if (s.equals("-dot")) {
hasDot = true;
i++;
}
else if (s.equals("-package") || s.equals("-pkg")) {
pkg = args[i + 1];
Expand Down Expand Up @@ -125,7 +128,6 @@ else if (i == args.length - 1) {
output = new File(input.getParent(), Utils.newName(input.getName(), "-out.g"));
}
Utils.write(tree.toString(), output);
logwrite(output);
}
else if (cmd.contains("-factor")) {
Tree tree = Tree.makeTree(input);
Expand All @@ -135,7 +137,6 @@ else if (cmd.contains("-factor")) {
output = new File(input.getParent(), Utils.newName(input.getName(), "-out.g"));
}
Utils.write(tree.toString(), output);
logwrite(output);
}
else if (cmd.contains("-nfa")) {
Tree tree = Tree.makeTree(input);
Expand Down Expand Up @@ -186,7 +187,6 @@ else if (cmd.contains("-regex")) {
}
else {
Utils.write(node.toString(), output);
logwrite(output);
}
}
else if (cmd.contains("-nfa2dfa")) {
Expand Down Expand Up @@ -266,6 +266,43 @@ else if (cmd.contains("-desc")) {
RecDescent gen = new RecDescent(tree);
gen.gen();
}
else if (cmd.contains("-lalr")) {
Tree tree = Tree.makeTree(input);
if (output == null) {
tree.options.outDir = input.getParent();
}
else {
tree.options.outDir = output.getAbsolutePath();
}
if (pkg != null) {
tree.options.packageName = pkg;
}
if (lexerClass != null) {
tree.options.lexerClass = lexerClass;
}
if (lexerFunc != null) {
tree.options.lexerFunction = lexerFunc;
}
if (tokenClass != null) {
tree.options.tokenClass = tokenClass;
}
if (parserClass != null) {
tree.options.parserClass = parserClass;
}
if (astClass != null) {
tree.options.astClass = astClass;
}
CodeGen gen = new CodeGen(tree, "lalr");
gen.gen();
AstBuilderGen builderGen = new AstBuilderGen(tree);
builderGen.gen();
if (hasDot) {
File dotFile = new File(tree.options.outDir, Utils.newName(input.getName(), "-dfa.dot"));
gen.gen.writeDot(new PrintWriter(dotFile));
File table = new File(tree.options.outDir, Utils.newName(input.getName(), "-table.dot"));
gen.gen.writeTableDot(new PrintWriter(table));
}
}
else {
throw new Exception("unknown commands: " + cmd);
}
Expand All @@ -274,8 +311,9 @@ else if (cmd.contains("-desc")) {
}
}

static void logwrite(File file) {
private static void logwrite(File file) {
System.out.println("writing " + file);
}


}
9 changes: 6 additions & 3 deletions src/main/java/mesut/parserx/gen/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ public static void first(Node node, Tree tree, boolean rec, Set<Name> set) {
}
else if (set.add(name)) {
if (rec && name.isRule()) {
RuleDecl decl = tree.getRule(name);
if (decl == null) {
List<RuleDecl> list = tree.getRules(name);
if (list.isEmpty()) {
throw new RuntimeException("rule not found: " + name);
}
first(decl.rhs, tree, rec, set);
for (RuleDecl decl : list) {
first(decl.rhs, tree, rec, set);
}

}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/mesut/parserx/gen/LexerGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public void generate() throws IOException {

File file = new File(options.outDir, options.lexerClass + ".java");
Utils.write(template.toString(), file);
System.out.println("lexer file generated to " + file);

writeTokenClass();
}
Expand Down Expand Up @@ -213,6 +212,5 @@ void writeTokenClass() throws IOException {
template.set("token_class", options.tokenClass);

Utils.write(template.toString(), out);
System.out.println("token class generated to " + out);
}
}
1 change: 0 additions & 1 deletion src/main/java/mesut/parserx/gen/ll/AstGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public void genAst() throws IOException {

File file = new File(options.outDir, options.astClass + ".java");
Utils.write(astWriter.get(), file);
System.out.println("writing " + file);
varCount.clear();
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/mesut/parserx/gen/ll/RecDescent.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public void gen() throws IOException {
File file = new File(options.outDir, options.parserClass + ".java");

Utils.write(code.get(), file);
System.out.println("parser file generated to " + file);
genTokenType();
}

Expand Down Expand Up @@ -137,7 +136,6 @@ void genTokenType() throws IOException {
c.append("}");
File file = new File(options.outDir, tokens + ".java");
Utils.write(c.get(), file);
System.out.println("write " + file);
}

String peekExpr() {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/mesut/parserx/gen/lr/AstBuilderGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public void gen() throws IOException {
writer.append("res.%s = make%s(node.children.get(0));", name.astInfo.varName, name.name);
}
}

writer.append("}");
}

Expand All @@ -87,6 +86,5 @@ public void gen() throws IOException {

File file = new File(options.outDir, "AstBuilder.java");
Utils.write(writer.get(), file);
System.out.println("writing " + file);
}
}
30 changes: 23 additions & 7 deletions src/main/java/mesut/parserx/gen/lr/CodeGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import mesut.parserx.gen.Template;
import mesut.parserx.nodes.Name;
import mesut.parserx.nodes.RuleDecl;
import mesut.parserx.nodes.Tree;
import mesut.parserx.utils.UnicodeUtils;
import mesut.parserx.utils.Utils;

Expand All @@ -17,17 +18,33 @@
//table driven parser gen
public class CodeGen {
public Options options;
LrDFA<?> dfa;
LrDFAGen<?> gen;
public LrDFA<?> dfa;
public LrDFAGen<?> gen;
List<RuleDecl> all;
LexerGenerator lexerGenerator;
boolean islr0;
String type;
IdMap idMap;

public CodeGen(LrDFAGen<?> gen, boolean islr0) {
public CodeGen(Tree tree, String type) {
if (type.equals("lalr") || type.equals("lr1")) {
Lr1Generator generator = new Lr1Generator(tree);
generator.merge = type.equals("lalr");
generator.generate();
this.gen = generator;
}
else {
Lr0Generator generator = new Lr0Generator(tree);
generator.generate();
this.gen = generator;
}
this.options = tree.options;
this.type = type;
}

public CodeGen(LrDFAGen<?> gen, String type) {
this.gen = gen;
this.options = gen.tree.options;
this.islr0 = islr0;
this.type = type;
}

public void gen() throws IOException {
Expand Down Expand Up @@ -82,7 +99,7 @@ void writeTable(Template template) {
//write accept
sb.append(pack(gen.acc));
LrItemSet acc = dfa.getSet(gen.acc);
if (islr0) {
if (type.equals("lr0")) {
//all tokens acc
sb.append(pack(dfa.tokens.size()));
for (Name tok : dfa.tokens) {
Expand Down Expand Up @@ -130,7 +147,6 @@ void writeTable(Template template) {
}

sb.append("\"");

template.set("table_packed", sb.toString());
}

Expand Down
Loading

0 comments on commit 563d64f

Please sign in to comment.