AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
aud.example.graph.GraphParser Class Reference

Parse text to build graph. More...

+ Collaboration diagram for aud.example.graph.GraphParser:

Public Member Functions

 GraphParser (AbstractGraph< AbstractNode, AbstractEdge > g)
 create new parser for graph g
More...
 
void parse (String input)
 parse input More...
 
void parse (File file)
 parse(String) contents of file More...
 
void setVerbose (boolean b)
 set verbose mode (report state to System.err) More...
 

Static Public Member Functions

static void main (String[] args)
 test and example for usage More...
 

Protected Member Functions

String errorNear ()
 helper: generate a simple error message More...
 
int lookahead ()
 helper: "lookahead" is the usual phrasing More...
 
void nextToken ()
 helper: consume current token and advance to next token More...
 
void expect (int tokenid, String text)
 helper: check token (without calling nextToken!) More...
 
void verbose (int level, String message)
 helper: print out information More...
 
void graph (int level)
 parse list of nodes/edges More...
 
AbstractEdge edge (int level)
 parse edge More...
 
AbstractNode node (int level)
 parse node More...
 
double[] pos (int level)
 parse position More...
 
Double weight (int level)
 parse weight More...
 

Detailed Description

Parse text to build graph.


 graph  ::= edges
 edges  ::= edge | edges edge
 edge   ::= ( node | node ( '--' | '->' ) weight node )
 node   ::= IDENTIFIER | IDENTIFIER pos
 pos    ::= '@' '(' NUMBER ',' NUMBER ')'
 weight ::= NIL | '(' NUMBER ')'
See also
Tokenizer
aud.graph.AbstractGraph

Definition at line 23 of file GraphParser.java.

Constructor & Destructor Documentation

◆ GraphParser()

create new parser for graph g

Definition at line 31 of file GraphParser.java.

31 {
32 graph_=g;
33 }

Member Function Documentation

◆ edge()

AbstractEdge aud.example.graph.GraphParser.edge ( int  level)
protected

parse edge

Definition at line 107 of file GraphParser.java.

107 {
108 verbose(level,"<edge>");
109 AbstractNode from=node(level+1);
110 if (lookahead()!=Tokenizer.EDGE && lookahead()!=Tokenizer.DEDGE)
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 }
int lookahead()
helper: "lookahead" is the usual phrasing
void nextToken()
helper: consume current token and advance to next token
AbstractNode node(int level)
parse node
void verbose(int level, String message)
helper: print out information
Double weight(int level)
parse weight
Interface to edges of a graph.
void setWeight(double w)
set weight
abstract Edge addEdge(Node source, Node destination)
Create and add new edge from source to destination.
abstract boolean isDirected()
Is graph directed?
Interface to nodes of a graph.

References aud.example.graph.Tokenizer.DEDGE, aud.example.graph.Tokenizer.EDGE, aud.example.graph.GraphParser.lookahead(), aud.example.graph.GraphParser.node(), and aud.example.graph.GraphParser.verbose().

Referenced by aud.example.graph.GraphParser.graph().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ errorNear()

String aud.example.graph.GraphParser.errorNear ( )
protected

helper: generate a simple error message

Definition at line 58 of file GraphParser.java.

58 {
59 return "near '"+scanner_.matchedText()+
60 "'\nbefore '"+scanner_.remainder()+"'";
61 }
String matchedText()
get text of last match or call to next
String remainder()
get remaining text

◆ expect()

void aud.example.graph.GraphParser.expect ( int  tokenid,
String  text 
)
protected

helper: check token (without calling nextToken!)

Exceptions
RuntimeExceptionif token does not match (syntax error)

Definition at line 78 of file GraphParser.java.

78 {
79 if (lookahead()!=tokenid)
80 throw new RuntimeException("expected '"+text+"' got '"+
81 scanner_.matchedText()+"'\nbefore '"+
82 scanner_.remainder());
83 }

References aud.example.graph.GraphParser.lookahead().

Referenced by aud.example.graph.GraphParser.node(), aud.example.graph.GraphParser.pos(), and aud.example.graph.GraphParser.weight().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ graph()

void aud.example.graph.GraphParser.graph ( int  level)
protected

parse list of nodes/edges

Definition at line 99 of file GraphParser.java.

99 {
100 verbose(level,"<graph>");
101
102 while (lookahead()!=Tokenizer.END_OF_INPUT)
103 edge(level+1);
104 }
AbstractEdge edge(int level)
parse edge

References aud.example.graph.GraphParser.edge(), aud.util.LexicalScanner.END_OF_INPUT, aud.example.graph.GraphParser.lookahead(), and aud.example.graph.GraphParser.verbose().

+ Here is the call graph for this function:

◆ lookahead()

int aud.example.graph.GraphParser.lookahead ( )
protected

helper: "lookahead" is the usual phrasing

Definition at line 64 of file GraphParser.java.

64 {
65 return scanner_.matchedTokenId();
66 }
int matchedTokenId()
get result of last call to next()

Referenced by aud.example.graph.GraphParser.edge(), aud.example.graph.GraphParser.expect(), aud.example.graph.GraphParser.graph(), aud.example.graph.GraphParser.node(), and aud.example.graph.GraphParser.weight().

+ Here is the caller graph for this function:

◆ main()

static void aud.example.graph.GraphParser.main ( String[]  args)
static

test and example for usage

Definition at line 205 of file GraphParser.java.

205 {
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
224 DotViewer.displayWindow(g,"graph").setExitOnClose();
225 }
GraphParser(AbstractGraph< AbstractNode, AbstractEdge > g)
create new parser for graph g
Interface to a graph.
String toDot()
Get dot representation.
Graph implementation based on adjacency matrix.
Definition: GraphAM.java:15
plain simple edge
Definition: SimpleEdge.java:4
plain simple node
Definition: SimpleNode.java:4

References aud.util.DotViewer.displayWindow(), aud.example.graph.GraphParser.parse(), aud.util.Sys.readFile(), aud.util.DotViewer.setExitOnClose(), aud.example.graph.GraphParser.setVerbose(), and aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.toDot().

+ Here is the call graph for this function:

◆ nextToken()

void aud.example.graph.GraphParser.nextToken ( )
protected

helper: consume current token and advance to next token

Definition at line 69 of file GraphParser.java.

69 {
70 if (scanner_.next()==Tokenizer.NO_MATCH)
71 throw new RuntimeException("unknown token: lexcial analysis failed at '"+
72 scanner_.remainder()+"'");
73 }
int next(Rule[] rules)
match remainder to table of rules @endiliteral

Referenced by aud.example.graph.GraphParser.pos(), and aud.example.graph.GraphParser.weight().

+ Here is the caller graph for this function:

◆ node()

AbstractNode aud.example.graph.GraphParser.node ( int  level)
protected

parse node

Definition at line 133 of file GraphParser.java.

133 {
134 verbose(level,"<node>");
135 if (lookahead()!=Tokenizer.IDENTIFIER &&
136 lookahead()!=Tokenizer.NUMBER)
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 }
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
void expect(int tokenid, String text)
helper: check token (without calling nextToken!)
double[] pos(int level)
parse position
abstract Node addNode()
create and add new node
void setPosition(double x, double y)
helper for drawing the graph (if supported)
void setLabel(String label)
set label (if supported)

References aud.example.graph.GraphParser.expect(), aud.example.graph.Tokenizer.IDENTIFIER, aud.example.graph.GraphParser.lookahead(), aud.example.graph.Tokenizer.NUMBER, and aud.example.graph.GraphParser.verbose().

Referenced by aud.example.graph.GraphParser.edge().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse() [1/2]

void aud.example.graph.GraphParser.parse ( File  file)

parse(String) contents of file

Definition at line 48 of file GraphParser.java.

48 {
49 parse(Sys.readFile(file));
50 }
void parse(String input)
parse input

References aud.example.graph.GraphParser.parse(), and aud.util.Sys.readFile().

+ Here is the call graph for this function:

◆ parse() [2/2]

void aud.example.graph.GraphParser.parse ( String  input)

parse input

Parameters
inputtext representation of parser
Exceptions
RuntimeExceptionon syntax error or invalid operation

Definition at line 39 of file GraphParser.java.

39 {
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 }
void graph(int level)
parse list of nodes/edges

Referenced by aud.example.graph.GraphParser.main(), aud.example.graph.RetroMaze.main(), aud.example.graph.TraversalExample.main(), aud.example.graph.MyGraph.MyGraph(), and aud.example.graph.GraphParser.parse().

+ Here is the caller graph for this function:

◆ pos()

double[] aud.example.graph.GraphParser.pos ( int  level)
protected

parse position

Definition at line 158 of file GraphParser.java.

158 {
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 }

References aud.example.graph.Tokenizer.AT, aud.example.graph.GraphParser.expect(), aud.example.graph.Tokenizer.LEFT_PAREN, aud.example.graph.GraphParser.nextToken(), aud.example.graph.Tokenizer.NUMBER, and aud.example.graph.GraphParser.verbose().

+ Here is the call graph for this function:

◆ setVerbose()

void aud.example.graph.GraphParser.setVerbose ( boolean  b)

set verbose mode (report state to System.err)

Definition at line 53 of file GraphParser.java.

53 {
54 verbose_=b;
55 }

Referenced by aud.example.graph.GraphParser.main().

+ Here is the caller graph for this function:

◆ verbose()

void aud.example.graph.GraphParser.verbose ( int  level,
String  message 
)
protected

helper: print out information

Definition at line 86 of file GraphParser.java.

86 {
87 if (verbose_) {
88 System.err.println(Sys.indent(level)+message+
89 " ['"+scanner_.matchedText()+"','"+
90 scanner_.remainder()+"']");
91 }
92 }

Referenced by aud.example.graph.GraphParser.edge(), aud.example.graph.GraphParser.graph(), aud.example.graph.GraphParser.node(), aud.example.graph.GraphParser.pos(), and aud.example.graph.GraphParser.weight().

+ Here is the caller graph for this function:

◆ weight()

Double aud.example.graph.GraphParser.weight ( int  level)
protected

parse weight

Definition at line 184 of file GraphParser.java.

184 {
185 verbose(level,"<weight>");
186
187 if (lookahead()!=Tokenizer.LEFT_BRACE)
188 return null;
189
190 nextToken();
191 expect(Tokenizer.NUMBER,"number (weight)");
192
193 double w=Double.parseDouble(scanner_.matchedText());
194 nextToken();
195
196 expect(Tokenizer.RIGHT_BRACE,"')'");
197 nextToken();
198
199 return w;
200 }

References aud.example.graph.GraphParser.expect(), aud.example.graph.Tokenizer.LEFT_BRACE, aud.example.graph.GraphParser.lookahead(), aud.example.graph.GraphParser.nextToken(), aud.example.graph.Tokenizer.NUMBER, and aud.example.graph.GraphParser.verbose().

+ Here is the call graph for this function:

The documentation for this class was generated from the following file: