// Use derivatives to match regular expressions public static boolean match(Node regex, String string) { // Two visitors Derivative d = new Derivative(); Nullable nullable = new Nullable(); // For debugging, create the printer here Printer printer = new Printer(); int index = 1; // Just compute the derivative with respect to the first character, then the second, then the // third and so on. for (char c : string.toCharArray()) { d.c = c; // Set the first character // For debugging purposes, // Print out the new regex System.out.println("Derivative: " + index); System.out.println("Regex: " + regex.accept(printer)); regex = regex.accept(d); // regex should match what it used to match, sans first character c index++; } System.out.println("Final Regex: " + regex.accept(printer)); // If the final language contains the empty string, then the original string was in the original // language. // Does the regex match the empty string? return regex.accept(nullable); }
public static void main(String[] args) { try { Node root = new MiniJavaParser(System.in).Goal(); HashMap<String, String> s = (HashMap<String, String>) root.accept(new GJNoArguDepthFirst()); // Your assignment part is invoked here. root.accept(new GJDepthFirst<Object, Object>(), s); System.out.println("Program type checked successfully"); } catch (ParseException e) { System.out.println(e.toString()); } }
@Override public void accept(NodeVisitor visitor) { visitor.begin(this); for (Node node : nodes.toArray(new Node[nodes.size()])) { node.accept(visitor); } visitor.end(this); }
/** * Call record every time you compute an analysis result for a node. Intended to be used as you * return: return record(node, answer); */ public BASet<IdOrOp> recur(Node n) { BASet<IdOrOp> res = n.accept(this); if (res.comparator() != NodeComparator.idOrOpComparer) { throw new Error("Wrong comparator on " + res); } fv.put(n, res); return res; }
private Criteria joinChildrenNodesInContainer(LogicalNode node, CriteriaJoin cj) { CriteriaContainer parent = new SimpleCriteriaContainer(cj); for (Node child : node) { parent.add(child.accept(this)); } return parent; }
@Override public void accept(BottomUpMemoVisitor visitor) { if (visitor.alreadyVisited(this)) { visitor.visitAgain(this); } else { arg.accept(visitor); visitor.visit(this); } }
/** * Aceasta este functia Main in care se vor apela toate metodele ce rezolva cerinta temei. * * @param args Reprezinta siruld e caractere ce va reprezenta fisierul din care se vor citi * intructiunile * @throws IOException */ public static void main(String[] args) throws IOException { Arbore arb = new Arbore(); arb.constructie_arbore(args[0]); Node nod = new Node("r", 0, 0, null); Node nod2, nod3 = new Node("r", 0, 0, null), nod4 = new Node("r", 0, 0, null); Stack<Node> stack = new Stack<Node>(); while (!arb.getStiva_rezultat().isEmpty()) stack.push(arb.getStiva_rezultat().pop()); while (!stack.isEmpty()) { nod2 = stack.pop(); nod.list.add(nod2); nod3.list.add(nod2); nod4.list.add(nod2); } nod.display(nod, args[0]); // ex2 boolean b = false; try { FileWriter fstream1 = new FileWriter(args[0] + "_sv"); BufferedWriter out1 = new BufferedWriter(fstream1); Visitor sv = new SemanticVisitor(); Visitor uv = new UnVizitator(sv); nod3.accept(uv); b = ((SemanticVisitor) sv).afis(out1); out1.close(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } // ex3 if (b == true) { try { FileWriter fstream = new FileWriter(args[0] + "_rv"); BufferedWriter out = new BufferedWriter(fstream); Visitor rv = new ResultVisitor(); Visitor uv2 = new UnVizitator(rv); nod4.accept(uv2); ((ResultVisitor) rv).afis(out); out.close(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } } }
@Override public Node accept( final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterLiteralNode(this)) { final List<Expression> oldValue = Arrays.asList(value); final List<Expression> newValue = Node.accept(visitor, oldValue); return visitor.leaveLiteralNode(oldValue != newValue ? setValue(lc, newValue) : this); } return this; }
public static void main(String[] args) { try { Node root = new MiniJavaParser(System.in).Goal(); // System.out.println("Program parsed successfully"); root.accept(new TypeChecker()); // Your assignment part is invoked here. System.out.print("Program type checked successfully"); } catch (ParseException e) { // System.out.println(e.toString()); } }
public static void main(String[] args) { try { Node root = new MiniRAParser(System.in).Goal(); // System.out.println("Program parsed successfully"); Object a = root.accept(new GJDepthFirst(), null); // root.accept(new GJNoArgu(a)); } catch (ParseException e) { System.out.println(e.toString()); } }
/** f0 -> "JUMP" f1 -> Label() */ public PGValue visit(JumpStmt n, Node argu) { String l_name = (n.f1.accept(this, argu)).GetLabel(); label_name = l_name; start = false; stop = false; // System.out.println("JUMP " + l_name); argu.accept(this, argu); stop = true; start = true; return null; }
private void traverseLevelOrder(Node<K, V> node, Visitor<K, V> visitor) { Queue<Node<K, V>> queue = new Queue<Node<K, V>>(); queue.enqueue(node); while (!queue.isEmpty()) { Node<K, V> aNode = queue.dequeue(); aNode.accept(visitor); if (aNode.left != null) queue.enqueue(aNode.left); if (aNode.right != null) queue.enqueue(aNode.right); } }
@Override public void accept(Visitor visitor) { // Visit this node final boolean recurse = visitor.visit(this); // Recurse if (recurse) { for (Node r : children) { r.accept(visitor); } } }
/** f0 -> "CJUMP" f1 -> Exp() f2 -> Label() */ public PGValue visit(CJumpStmt n, Node argu) { PGValue p = n.f1.accept(this, argu); int cond = 0; if (p == null) return null; else cond = p.GetVal(); if (cond == 1) return null; String l_name = (n.f2.accept(this, argu)).GetLabel(); label_name = l_name; start = false; stop = false; // System.out.println("CJUMP " + l_name); argu.accept(this, argu); stop = true; start = true; return null; }
public static void main(String[] args) throws FileNotFoundException, ParseException { // try { // use the following line only in IDE Node root = new MiniRAParser( new FileInputStream("/home/chinmay/dev/compilers-lab/temp/MoreThan4.minira")) .Goal(); // Final submission file should take input from command line // Node root = new MiniRAParser(System.in).Goal(); // System.out.println("Program parsed successfully"); // root.accept(new OrigGJ()); GJNoArguDepthFirst gjv = new GJNoArguDepthFirst(); root.accept(gjv); // } //catch (Throwable e) { // System.out.println(e.toString()); // } }
/** Appends additional sqlWhere in relationship if any to the end of the join. */ private void appendAdditionalJoinCondition( SqlWriter writer, DatabaseType databaseType, final String sourceVarName, final String targetVarName) { if (relationship.getSqlFilter() == null || relationship.getSqlFilter().length() == 0) { return; } Node node = NodeFactory.parseExpression(relationship.getSqlFilter()); NodeVisitor visit = new AbstractNodeVisitor() { @Override public boolean visitIdentifier(Identifier identifier) { String path = identifier.getName(); int dotPos = path.indexOf('.'); if (dotPos < 0) { throw new EJBQLException( "Invalid sqlWhere in relationship: " + relationship + " - " + relationship.getSqlFilter()); } String entityName = path.substring(0, dotPos); if (entityName.equals(relationship.getSourceEntity().getSystemName()) || entityName.equals(relationship.getSourceEntity().getTableName())) { identifier.setName(sourceVarName + path.substring(dotPos)); } else if (entityName.equals(relationship.getTargetEntity().getSystemName()) || entityName.equals(relationship.getTargetEntity().getTableName())) { identifier.setName(targetVarName + path.substring(dotPos)); } return true; } }; node.accept(visit); writer.write(" AND "); node.toString(writer, databaseType); }
@SuppressWarnings("unchecked") public T compute(Node<?, ?> language) { assert language != null; getWorkList().clear(); begin(); T accumulator; // Visit a grammar if (language.tag == Node.Tag.ID) { getWorkList().todo((Node<String, Void>) language); accumulator = bottom(); for (Node<String, Void> identifier : getWorkList()) { accumulator = reduce(accumulator, g.acceptRule(this, identifier)); if (done(accumulator)) { return end(accumulator); } } } // Visit a regex else { accumulator = Node.accept(this, language); } return end(accumulator); }
@Override public void accept(BottomUpVisitor visitor) { arg.accept(visitor); visitor.visit(this); }
void visitChildren(@NotNull SuperNode node) { for (Node child : node.getChildren()) { child.accept(this); } }
/** f0 -> "CALL" f1 -> Exp() f2 -> "(" f3 -> ( Exp() )* f4 -> ")" */ public PGValue visit(Call n, Node argu) { // NOTE: bug fix by [email protected] 10/30/07, [email protected] 11/14/07 // // details: local_arg_size was previously initialized to 20. // This resulted in TEMP values being overwritten by the caller. // For recursive calls, the overwritten values were bugs if any // thing stored in an overwritten temp was used after the // recursive call. For this reason, we now make a copy of the // entire temp space before each call. n.f0.accept(this, argu); PGValue function_addr = n.f1.accept(this, argu); if (!(function_addr instanceof Label_pg)) { System.err.println("error in CALL"); System.exit(0); return null; // Will never actually be reached. } String m_name = function_addr.GetLabel(); n.f2.accept(this, argu); // Caller saves all local variables. PGValue[] saved_locals = new PGValue[TPV.length]; System.arraycopy(TPV, 0, saved_locals, 0, TPV.length); // Copy call args into first N temps. ParamList_pg pl = (ParamList_pg) n.f3.accept(this, argu); // NOTE: we save and return the TEMPs for the caller, so we are // free to update the TPV array at this point. [email protected] fixed a // bug where this statement caused the callee to have TPV // side-effects for the caller. // NOTE: that if the method had no parameters, pl will be null. // This bug was discovered by Prof. Todd Millstein [email protected] // check to see if the callee has any arguments and if so, pass // them into the TPV temp array using the caller's parameters if (pl != null) { int num_params = pl.GetVal(); PGValue[] param_list = pl.GetList(); for (int i = 0; i < num_params; i++) { TPV[i] = param_list[i]; } } n.f4.accept(this, argu); met_name = m_name; boolean old_start = start; boolean old_stop = stop; start = true; stop = false; // System.out.println("No NO NO 222" + met_name); PGValue _ret = pg_root.accept(this, pg_root); // System.out.println("No NO NO"); start = old_start; stop = old_stop; // Restore saved local variables. TPV = saved_locals; return _ret; }
private List<Name> forEach(Iterable<? extends Node> list, List<Name> names) { for (Node node : list) { node.accept(this, names); } return names; }
public static void main(String[] args) { /*try { //ArrayList<Node> Nodes = new ArrayList<Node>(); AST ass = new AST(); Sequence seq = ass.new Sequence(); FileReader inFile = new FileReader("Test.scn"); StreamTokenizer st = new StreamTokenizer(inFile); st.ordinaryChar('.'); st.ordinaryChar('/'); st.eolIsSignificant(true); String ID; int x; int y; int w; int h; String fileName; int token = st.nextToken(); while(token != StreamTokenizer.TT_EOF) { char ch; String s; switch(token) { case StreamTokenizer.TT_WORD: s = st.sval; if(s.equals("Move")) { st.nextToken(); st.nextToken(); ID = st.sval; st.nextToken(); st.nextToken(); x = (int)st.nval; st.nextToken(); st.nextToken(); y = (int)st.nval; st.nextToken(); seq.elements.add(new Move(new Id(ID), new Number(x), new Number(y))); System.out.println("Move " + ID + " " + x + ", " + y); } /*else if(s.equals("Object") || s.equals("Camera") || s.equals("Sprite")) { System.out.print("<Type> " + s + " "); }*/ /*else if(s.equals("Object")) { st.nextToken(); ID = st.sval; st.nextToken(); st.nextToken(); x = (int)st.nval; st.nextToken(); st.nextToken(); y = (int)st.nval; st.nextToken(); st.nextToken(); w = (int)st.nval; st.nextToken(); st.nextToken(); h = (int)st.nval; st.nextToken(); st.nextToken(); fileName = st.sval; st.nextToken(); seq.elements.add(new Obj(new Id(ID), new Variables(new Number(x), new Number(y), new Number(w), new Number(h), new Id(fileName)))); System.out.println("Object " + ID + " " + x + ", " + y + ", " + w + ", " + h + ", " + fileName); } else if(s.equals("Camera")) { st.nextToken(); ID = st.sval; st.nextToken(); st.nextToken(); x = (int)st.nval; st.nextToken(); st.nextToken(); y = (int)st.nval; st.nextToken(); seq.elements.add(new Camera(new Id(ID), new Variables(new Number(x), new Number(y)))); System.out.println("Camera " + ID + " " + x + ", " + y); } else if(s.equals("Sprite")) { st.nextToken(); ID = st.sval; st.nextToken(); st.nextToken(); x = (int)st.nval; st.nextToken(); st.nextToken(); y = (int)st.nval; st.nextToken(); st.nextToken(); w = (int)st.nval; st.nextToken(); st.nextToken(); h = (int)st.nval; st.nextToken(); st.nextToken(); fileName = st.sval; st.nextToken(); seq.elements.add(new Sprite(new Id(ID), new Variables(new Number(x), new Number(y), new Number(w), new Number(h), new Id(fileName)))); System.out.println("Sprite " + ID + " " + x + ", " + y + ", " + w + ", " + h + ", " + fileName); } else { System.out.print("<ID> " + s + " "); } break; case StreamTokenizer.TT_NUMBER: int n = (int)st.nval; System.out.print("<Number> " + n); seq.elements.add(new Number(n)); break; case '(': ch = (char)st.ttype; System.out.print("<Variables>" + ch); break; case ')': ch = (char)st.ttype; System.out.print(ch); break; case ',': ch = (char)st.ttype; System.out.print(ch + " "); break; case '"': s = st.sval; System.out.print("<ID> " + "\"" + s + "\""); break; case StreamTokenizer.TT_EOL: System.out.println(); case '\0': break; default: s = st.sval; System.out.println("ERROR: Unrecognized Token: " + s); break; } token = st.nextToken(); } inFile.close(); System.out.println(); } catch(IOException e) { System.out.println("Error: " + e); }*/ Node trial = Tokenizer(); trial.accept(new AST.CompilerVisitor()); Node Object = sprite(id("TEST"), variables(number(1), number(4), number(1), number(4), id("file.png"))); StatementInterpreter runner = new StatementInterpreter(); Object.accept(runner); }
private void traverseInOrder(Node<K, V> node, Visitor<K, V> visitor) { if (node == null) return; traverseInOrder(node.left, visitor); node.accept(visitor); traverseInOrder(node.right, visitor); }
@Override public Node accept(Visitor v) throws Exception { left = left.accept(v); right = right.accept(v); return v.Visit(this); }
@Override public void accept(TopDownVisitor visitor) { visitor.visit(this); arg.accept(visitor); }