AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
GraphParser.java
Go to the documentation of this file.
1package aud.example.graph;
2
3import java.io.File;
4import aud.graph.*;
5import aud.HashMap;
6import aud.util.Sys;
7import aud.util.DotViewer;
8
23public class GraphParser {
24
25 Tokenizer scanner_ = null;
26 boolean verbose_ = false;
28 HashMap<String,AbstractNode> nodeMap_ = null;
29
32 graph_=g;
33 }
34
39 public void parse(String input) {
40 scanner_ = new Tokenizer(input);
41 scanner_.next(); // use as "lookahead"
42 nodeMap_=new HashMap<String,AbstractNode>();
43 graph(0);
44 expect(Tokenizer.END_OF_INPUT,"<end of input>");
45 }
46
48 public void parse(File file) {
49 parse(Sys.readFile(file));
50 }
51
53 public void setVerbose(boolean b) {
54 verbose_=b;
55 }
56
58 protected String errorNear() {
59 return "near '"+scanner_.matchedText()+
60 "'\nbefore '"+scanner_.remainder()+"'";
61 }
62
64 protected int lookahead() {
65 return scanner_.matchedTokenId();
66 }
67
69 protected void nextToken() {
70 if (scanner_.next()==Tokenizer.NO_MATCH)
71 throw new RuntimeException("unknown token: lexcial analysis failed at '"+
72 scanner_.remainder()+"'");
73 }
74
78 protected void expect(int tokenid,String text) {
79 if (lookahead()!=tokenid)
80 throw new RuntimeException("expected '"+text+"' got '"+
81 scanner_.matchedText()+"'\nbefore '"+
82 scanner_.remainder());
83 }
84
86 protected void verbose(int level,String message) {
87 if (verbose_) {
88 System.err.println(Sys.indent(level)+message+
89 " ['"+scanner_.matchedText()+"','"+
90 scanner_.remainder()+"']");
91 }
92 }
93
94 //
95 // the parser
96 //
97
99 protected void graph(int level) {
100 verbose(level,"<graph>");
101
103 edge(level+1);
104 }
105
107 protected AbstractEdge edge(int level) {
108 verbose(level,"<edge>");
109 AbstractNode from=node(level+1);
111 return null;
112
113 boolean directed=(lookahead()==Tokenizer.DEDGE);
114
115 if (directed ^ graph_.isDirected())
116 throw new RuntimeException("specified directed edge "+
117 "for undirected graph or vice versa");
118
119 nextToken();
120
121 Double w=weight(level+1);
122
123 AbstractNode to=node(level+1);
124 AbstractEdge e=graph_.addEdge(from,to);
125
126 if (w!=null)
127 e.setWeight(w);
128
129 return e;
130 }
131
133 protected AbstractNode node(int level) {
134 verbose(level,"<node>");
137 expect(Tokenizer.IDENTIFIER,"node identifier");
138 String id=scanner_.matchedText();
139 nextToken();
140
141 AbstractNode n=nodeMap_.find(id);
142
143 if (n==null) {
144 n=graph_.addNode();
145 n.setLabel(id);
146 nodeMap_.insert(id,n);
147 }
148
149 if (lookahead()==Tokenizer.AT) {
150 double[] p=pos(level+1);
151 n.setPosition(p[0],p[1]);
152 }
153
154 return n;
155 }
156
158 protected double[] pos(int level) {
159 verbose(level,"<pos>");
160 expect(Tokenizer.AT,"@(x,y)");
161 nextToken();
162 expect(Tokenizer.LEFT_PAREN,"'(' position");
163 nextToken();
164
165 double x,y;
166 expect(Tokenizer.NUMBER,"position x-coordinate");
167 x=Double.parseDouble(scanner_.matchedText());
168 nextToken();
169
170 expect(Tokenizer.COMMA,"',' comma separating coordinates");
171 nextToken();
172
173 expect(Tokenizer.NUMBER,"position y-coordinate");
174 y=Double.parseDouble(scanner_.matchedText());
175 nextToken();
176
177 expect(Tokenizer.RIGHT_PAREN,"')' position");
178 nextToken();
179
180 return new double[] { x,y };
181 }
182
184 protected Double weight(int level) {
185 verbose(level,"<weight>");
186
188 return null;
189
190 nextToken();
191 expect(Tokenizer.NUMBER,"number (weight)");
192
193 double w=Double.parseDouble(scanner_.matchedText());
194 nextToken();
195
197 nextToken();
198
199 return w;
200 }
201
202
204 @SuppressWarnings("unchecked")
205 public static void main(String[] args) {
206
209 (new SimpleNode(),new SimpleEdge(),false);
210
211 String input=(args.length>0) ?
212 Sys.readFile(new File(args[0])) : "a@(1,1) -- [1] b b -- c c -- a";
213
214 System.out.println("input = '"+input+"'");
215
216 GraphParser p=
218 p.setVerbose(true);
219 p.parse(input);
220
221 System.out.println(g);
222 System.out.println(g.toDot());
223
225 }
226}
Implementation of an unordered map based on a hash table.
Definition: HashMap.java:31
Value insert(Key key, Value value)
insert key-value pair
Definition: HashMap.java:260
Value find(Key key)
find value for key @endiliteral
Definition: HashMap.java:281
Parse text to build graph.
static void main(String[] args)
test and example for usage
int lookahead()
helper: "lookahead" is the usual phrasing
void nextToken()
helper: consume current token and advance to next token
void expect(int tokenid, String text)
helper: check token (without calling nextToken!)
void parse(String input)
parse input
AbstractNode node(int level)
parse node
void graph(int level)
parse list of nodes/edges
double[] pos(int level)
parse position
void verbose(int level, String message)
helper: print out information
void parse(File file)
parse(String) contents of file
AbstractEdge edge(int level)
parse edge
String errorNear()
helper: generate a simple error message
void setVerbose(boolean b)
set verbose mode (report state to System.err)
GraphParser(AbstractGraph< AbstractNode, AbstractEdge > g)
create new parser for graph g
Double weight(int level)
parse weight
Breaks input string into pieces ("tokens").
Interface to edges of a graph.
void setWeight(double w)
set weight
Interface to a graph.
abstract Edge addEdge(Node source, Node destination)
Create and add new edge from source to destination.
abstract boolean isDirected()
Is graph directed?
abstract Node addNode()
create and add new node
String toDot()
Get dot representation.
Interface to nodes of a graph.
void setPosition(double x, double y)
helper for drawing the graph (if supported)
void setLabel(String label)
set label (if supported)
Graph implementation based on adjacency matrix.
Definition: GraphAM.java:15
plain simple edge
Definition: SimpleEdge.java:4
plain simple node
Definition: SimpleNode.java:4
Simple viewer for Graphvizable.
Definition: DotViewer.java:47
static DotViewer displayWindow(Graphvizable object, String caption)
create new DotViewer (toplevel window) and display object
Definition: DotViewer.java:139
void setExitOnClose()
exit application if viewer is closed
Definition: DotViewer.java:154
static final int END_OF_INPUT
no more input
int next(Rule[] rules)
match remainder to table of rules @endiliteral
int matchedTokenId()
get result of last call to next()
String matchedText()
get text of last match or call to next
String remainder()
get remaining text
static final int NO_MATCH
no match (usually implies a syntax error)
System related utilities.
Definition: Sys.java:58
static String indent(int level)
get indentation string filled with spaces
Definition: Sys.java:183
static String readFile(File file)
read entire file and return contents as String
Definition: Sys.java:216
Graph data structures and algorithms.
utilities (not related to AuD lecture)
Definition: Colormap.java:1
AuD lecture: Data structures, algorithms, examples.
Definition: A234Tree.java:1