Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
ANTLR Tool Source Code
ANTLR is a powerful parser generator for multiple programming languages
including Java.
ANTLR contains 2 major modules:
ANTLR Tool Source Code files are provided in the distribution packge (antlr4-4.10.1.zip). You can download them at ANTLR Website.
You can also browse the source code below:
✍: FYIcenter
⏎ org/antlr/v4/misc/Graph.java
/*
* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
package org.antlr.v4.misc;
import org.antlr.v4.runtime.misc.OrderedHashSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** A generic graph with edges; Each node as a single Object payload.
* This is only used to topologically sort a list of file dependencies
* at the moment.
*/
public class Graph<T> {
public static class Node<T> {
public T payload;
@SuppressWarnings("unchecked")
public List<Node<T>> edges = Collections.EMPTY_LIST; // points at which nodes?
public Node(T payload) { this.payload = payload; }
public void addEdge(Node<T> n) {
if ( edges==Collections.EMPTY_LIST ) edges = new ArrayList<Node<T>>();
if ( !edges.contains(n) ) edges.add(n);
}
@Override
public String toString() { return payload.toString(); }
}
/** Map from node payload to node containing it */
protected Map<T,Node<T>> nodes = new LinkedHashMap<T,Node<T>>();
public void addEdge(T a, T b) {
//System.out.println("add edge "+a+" to "+b);
Node<T> a_node = getNode(a);
Node<T> b_node = getNode(b);
a_node.addEdge(b_node);
}
public Node<T> getNode(T a) {
Node<T> existing = nodes.get(a);
if ( existing!=null ) return existing;
Node<T> n = new Node<T>(a);
nodes.put(a, n);
return n;
}
/** DFS-based topological sort. A valid sort is the reverse of
* the post-order DFA traversal. Amazingly simple but true.
* For sorting, I'm not following convention here since ANTLR
* needs the opposite. Here's what I assume for sorting:
*
* If there exists an edge u -> v then u depends on v and v
* must happen before u.
*
* So if this gives nonreversed postorder traversal, I get the order
* I want.
*/
public List<T> sort() {
Set<Node<T>> visited = new OrderedHashSet<Node<T>>();
ArrayList<T> sorted = new ArrayList<T>();
while ( visited.size() < nodes.size() ) {
// pick any unvisited node, n
Node<T> n = null;
for (Node<T> tNode : nodes.values()) {
n = tNode;
if ( !visited.contains(n) ) break;
}
if (n!=null) { // if at least one unvisited
DFS(n, visited, sorted);
}
}
return sorted;
}
public void DFS(Node<T> n, Set<Node<T>> visited, ArrayList<T> sorted) {
if ( visited.contains(n) ) return;
visited.add(n);
if ( n.edges!=null ) {
for (Node<T> target : n.edges) {
DFS(target, visited, sorted);
}
}
sorted.add(n.payload);
}
}
⏎ org/antlr/v4/misc/Graph.java
Or download all of them as a single archive file:
File name: antlr-tool-4.10.1-sources.jar File size: 347718 bytes Release date: 2022-04-15 Download
2022-04-24, ≈103🔥, 0💬
Popular Posts:
Java Architecture for XML Binding (JAXB) is a Java API that allows Java developers to map Java class...
Java Servlet 3.0 Specification API. JAR File Size and Download Location: File name: servlet-api.jar,...
JDK 11 jdk.security.auth.jmod is the JMOD file for JDK 11 Security Auth module. JDK 11 Security Auth...
JDK 8 tools.jar is the JAR file for JDK 8 tools. It contains Java classes to support different JDK t...
The Apache FontBox library is an open source Java tool to obtain low level information from font fil...