Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize memory usage #1106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 25 additions & 12 deletions jflex/src/main/java/jflex/generator/Emitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import jflex.base.Build;
import jflex.base.Pair;
Expand Down Expand Up @@ -175,8 +178,7 @@ private void emitLookBuffer() {
if (!hasGenLookAhead()) return;

println(" /** For the backwards DFA of general lookahead statements */");
println(
" private boolean [] zzFin = new boolean [Math.min(ZZ_BUFFERSIZE, zzMaxBufferLen())+1];");
println(" private BitSet zzFin = null;");
println();
}

Expand Down Expand Up @@ -428,11 +430,22 @@ private void emitHeader() {
private void emitUserCode() {
println(scanner.userCode());

List<String> additionalImports = new ArrayList<>();
if (scanner.cup2Compatible()) {
additionalImports.addAll(
Arrays.asList(
"/* CUP2 imports */",
"import edu.tum.cup2.scanner.*;",
"import edu.tum.cup2.grammar.*;"));
}

if (hasGenLookAhead()) {
additionalImports.add("import java.util.BitSet;");
}

if (!additionalImports.isEmpty()) {
println();
println("/* CUP2 imports */");
println("import edu.tum.cup2.scanner.*;");
println("import edu.tum.cup2.grammar.*;");
additionalImports.forEach(this::println);
println();
}
}
Expand Down Expand Up @@ -1116,26 +1129,26 @@ private void emitActions() {
println(" // general lookahead, find correct zzMarkedPos");
println(" { int zzFState = " + dfa.entryState(action.getEntryState()) + ";");
println(" int zzFPos = zzStartRead;");
println(" if (zzFin.length <= zzBufferL.length) {");
println(" zzFin = new boolean[zzBufferL.length+1];");
println(" if (zzFin == null || zzFin.size() <= zzBufferL.length) {");
println(" zzFin = new BitSet(zzBufferL.length+1);");
println(" }");
println(" boolean zzFinL[] = zzFin;");
println(" BitSet zzFinL = zzFin;");
println(" while (zzFState != -1 && zzFPos < zzMarkedPos) {");
println(" zzFinL[zzFPos] = ((zzAttrL[zzFState] & 1) == 1);");
println(" zzFinL.set(zzFPos, ((zzAttrL[zzFState] & 1) == 1));");
println(" zzInput = Character.codePointAt(zzBufferL, zzFPos, zzMarkedPos);");
println(" zzFPos += Character.charCount(zzInput);");
println(" zzFState = zzTransL[ zzRowMapL[zzFState] + zzCMap(zzInput) ];");
println(" }");
println(" if (zzFState != -1) {");
println(" zzFinL[zzFPos++] = ((zzAttrL[zzFState] & 1) == 1);");
println(" zzFinL.set(zzFPos++, ((zzAttrL[zzFState] & 1) == 1));");
println(" }");
println(" while (zzFPos <= zzMarkedPos) {");
println(" zzFinL[zzFPos++] = false;");
println(" zzFinL.clear(zzFPos++);");
println(" }");
println();
println(" zzFState = " + dfa.entryState(action.getEntryState() + 1) + ";");
println(" zzFPos = zzMarkedPos;");
println(" while (!zzFinL[zzFPos] || (zzAttrL[zzFState] & 1) != 1) {");
println(" while (!zzFinL.get(zzFPos) || (zzAttrL[zzFState] & 1) != 1) {");
println(
" zzInput = Character.codePointBefore(zzBufferL, zzFPos, zzStartRead);");
println(" zzFPos -= Character.charCount(zzInput);");
Expand Down