Commit fe729fe8 by Dr. Michael Petter

patched forgetfull Chainremover

parent 711aefb5
package java_cup.runtime;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
public class SyntaxTreeTransform {
private static final XMLElement[] X = new XMLElement[0];
private static class ListFlattener extends SyntaxTreeDFS.AbstractVisitor{
private List<String> name;
public ListFlattener(String ... name) {
super();
this.name=Arrays.asList(name);
}
public XMLElement root(){
System.out.println(stack.size()+" - "+intstack.size());
return stack.pop();
}
private Stack<XMLElement> stack = new Stack<>();
private Stack<Integer> intstack = new Stack<>();
@Override
public void defaultPre(XMLElement element, List<XMLElement> children) {
intstack.push(children.size());
}
@Override
public void defaultPost(XMLElement element, List<XMLElement> children) {
int n = intstack.pop();
if (n>0){
XMLElement el=null;
outer: do {
if (name.contains(element.getTagname())) for (XMLElement e:children)
if (e.getTagname().equals(element.getTagname())){
el = e;
break outer;
}
LinkedList<XMLElement> elems = new LinkedList<>();
while (n-->0) elems.addFirst(stack.pop());
stack.push(new XMLElement.NonTerminal(element.getTagname(), 0, elems.toArray(X)));
System.out.println(" doch noch: ");
return;
}while (false);
LinkedList<XMLElement> elems = new LinkedList<>();
while (n-->0) {
elems.addFirst(stack.pop());
// XMLElement x = stack.pop();
// if (x!=el) elems.addFirst(x);
}
stack.addAll(elems);
intstack.push(intstack.pop()+n-1);
}
if (n==0) {
stack.push(element);
}
}
}
public static XMLElement flattenLists(XMLElement elem,String ...names) {
ListFlattener cr = new ListFlattener(names);
SyntaxTreeDFS.dfs(elem, cr);
return cr.root();
}
private static class ChainRemover extends SyntaxTreeDFS.AbstractVisitor{
public XMLElement root(){
return stack.pop();
......@@ -16,7 +71,8 @@ public class SyntaxTreeTransform {
if (n>1){
LinkedList<XMLElement> elems = new LinkedList<>();
while (n-->0) elems.addFirst(stack.pop());
XMLElement ne = new XMLElement.NonTerminal(arg0.getTagname(),0,elems.toArray(new XMLElement[0]));
XMLElement.NonTerminal non = (XMLElement.NonTerminal)arg0;
XMLElement ne = new XMLElement.NonTerminal(arg0.getTagname(),non.getVariant(),elems.toArray(X));
stack.push(ne);
return;
}
......
rm *.java
rm *.class
jflex c.jflex
java -jar ../../dist/java-cup-11b.jar -locations -interface -parser Parser -xmlactions c.cup
javac -cp ../../dist/java-cup-11b-runtime.jar:. *.java
java -cp ../../dist/java-cup-11b-runtime.jar:. Parser input.c simple.xml
java -cp ../../dist/java-cup-11b-runtime.jar:. Parser complicated.c complicated.xml
java -jar ../../target/dist/java-cup-11b.jar -locations -interface -parser Parser -xmlactions c.cup
javac -cp ../../target/dist/java-cup-11b-runtime.jar:. *.java
java -cp ../../target/dist/java-cup-11b-runtime.jar:. Parser input.c simple.xml
java -cp ../../target/dist/java-cup-11b-runtime.jar:. Parser complicated.c complicated.xml
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment