netlist2.java

Download: project files

netlist2 tests some parsing operations for netlist files.

We could have used the Java StringTokenizer to obtain tokens separated by whitespace. However, here we illustrate the use of Regular Expressions for this purpose. See the use of the String split() method on line 42.

This is a Java version of the C++ console program.


01: // netlist2.java
02: 
03: import java.io.*;
04: 
05: public class netlist2 {
06: 
07:     static public void main(String args[])
08:     {
09:         String filename;
10:         FileInputStream instream;
11:         BufferedReader in;
12:         
13:         if (args.length < 1) {
14:             System.err.println("usage: java netlist2 source");
15:             System.exit(0);
16:         }
17:         try {
18:             filename = args[0];
19:             instream = new FileInputStream(filename);
20:             in = new BufferedReader(new InputStreamReader(instream));
21:             read(in);
22:             in.close();
23:         } catch (IOException e) {  
24:                  System.out.println( e);
25:         }
26:     }
27:     
28:     static void read(BufferedReader inr) {
29:         LineNumberReader in = new LineNumberReader(inr);
30:         boolean done = false;
31:         while (!done) {
32:             int n = in.getLineNumber();
33:             try {
34:                 String inp = in.readLine();
35:                 if (inp==null) break;
36:                 System.out.println("Line " + n + ":  " + inp);
37:                 String [] str = inp.split("%");
38:                 System.out.println(str.length);
39:                 for (String s: str) {
40:                         System.out.println(s.length() + ": " + s);
41:                 }
42:                 String [] tokens = str[0].split("\\s+");
43:                 for (int k=0; k<tokens.length; k++) {
44:                     System.out.printf("  token %d: %s\n",k,tokens[k]);
45:                     if (k==4) {
46:                         double v = parseValue(tokens[k]);
47:                         System.out.printf("%g\n",v);
48:                     }
49:                 }
50:             }
51:             catch (IOException e) {
52:                 System.out.println(e);
53:             }
54:             catch (NumberFormatException e) {
55:                 System.out.println(e);
56:             }
57:         }
58:     }
59:     static double parseValue(String str)
60:     {
61:         char [] chars = str.toCharArray();
62:         int n = chars.length;
63:         while (n>0) {
64:             if (!Character.isLetter(chars[n-1])) break;
65:             n--;
66:         }
67:         double v;
68:         if (n<chars.length) {
69:             System.out.printf("char %d is %c\n",n,chars[n]);
70:             v = Double.parseDouble(str.substring(0,n));
71:             String suffix = str.substring(n);
72:             System.out.printf("value %g suffix %s\n",v,suffix);
73:         }
74:         else {
75:             v = Double.parseDouble(str);
76:             System.out.printf("value %g (no suffix)\n",v);
77:         }
78:         return v;
79:     }               
80:         
81: }


Results

Input (test.net)

% This is a test of circuit file parser
R  R1  N_2 N_1 3k
R  R2  N_2 N_3 4MEG
V  V1  N_1 N_3  5
R  R3  N_3  0   2

java netlist2 test.net

Line 0:  % This is a test of circuit file parser
2
0: 
38:  This is a test of circuit file parser
  token 0: 
Line 1:  R  R1  N_2 N_1 3k
1
17: R  R1  N_2 N_1 3k
  token 0: R
  token 1: R1
  token 2: N_2
  token 3: N_1
  token 4: 3k
char 1 is k
value 3.00000 suffix k
3.00000
Line 2:  R  R2  N_2 N_3 4MEG
1
19: R  R2  N_2 N_3 4MEG
  token 0: R
  token 1: R2
  token 2: N_2
  token 3: N_3
  token 4: 4MEG
char 1 is M
value 4.00000 suffix MEG
4.00000
Line 3:  V  V1  N_1 N_3  5
1
17: V  V1  N_1 N_3  5
  token 0: V
  token 1: V1
  token 2: N_1
  token 3: N_3
  token 4: 5
value 5.00000 (no suffix)
5.00000
Line 4:  R  R3  N_3  0   2
1
17: R  R3  N_3  0   2
  token 0: R
  token 1: R3
  token 2: N_3
  token 3: 0
  token 4: 2
value 2.00000 (no suffix)
2.00000


Maintained by John Loomis, updated Sat Mar 22 10:37:24 2008