KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fri > patterns > interpreter > parsergenerator > lexer > examples > CStyleCommentStrip


1 package fri.patterns.interpreter.parsergenerator.lexer.examples;
2
3 import java.io.*;
4 import fri.patterns.interpreter.parsergenerator.Token;
5 import fri.patterns.interpreter.parsergenerator.Lexer;
6 import fri.patterns.interpreter.parsergenerator.syntax.*;
7 import fri.patterns.interpreter.parsergenerator.lexer.*;
8 import fri.patterns.interpreter.parsergenerator.syntax.builder.SyntaxSeparation;
9
10 /**
11     Sample for a Lexer built from blocks of StandardLexerRules.
12     Removes comments from Java/C source.
13
14     @author (c) 2002, Fritz Ritzberger
15 */

16
17 public class CStyleCommentStrip
18 {
19     private static Lexer lexer;
20     
21     static {
22         try {
23             String JavaDoc [][] rules = {
24                 { Token.TOKEN, "others" }, // define what we want to receive
25
{ Token.TOKEN, "`stringdef`" }, // need this rule, as string definitions could contain comments
26
{ Token.IGNORED, "`cstylecomment`" },
27                 { "others", "others", "other" },
28                 { "others", "other" },
29                 { "other", "`char`", Token.BUTNOT, "`cstylecomment`", Token.BUTNOT, "`stringdef`" },
30             };
31     
32             Syntax syntax = new Syntax(rules); // LexerBuilder makes unique
33
SyntaxSeparation separation = new SyntaxSeparation(syntax);
34             LexerBuilder builder = new LexerBuilder(separation.getLexerSyntax(), separation.getIgnoredSymbols());
35             lexer = builder.getLexer();
36             //lexer.setDebug(true); // dumps scanner consumer lists
37
lexer.setTerminals(separation.getTokenSymbols());
38         }
39         catch (Exception JavaDoc e) {
40             e.printStackTrace();
41         }
42     }
43     
44     
45     /**
46         Stripping C-style comments from an input reader and writing to output writer.
47         Both in and out get closed when finished.
48     */

49     public CStyleCommentStrip(Reader in, Writer out)
50         throws LexerException, IOException
51     {
52         try {
53             lexer.setInput(in);
54
55             Token t;
56             do {
57                 t = lexer.getNextToken(null);
58                 
59                 if (t.symbol == null)
60                     lexer.dump(System.err);
61                 else
62                 if (t.text != null)
63                     out.write(t.text.toString());
64             }
65             while (t.symbol != null && Token.isEpsilon(t) == false);
66         }
67         finally {
68             try { in.close(); } catch (Exception JavaDoc e) { e.printStackTrace(); }
69             try { out.close(); } catch (Exception JavaDoc e) { e.printStackTrace(); }
70         }
71     }
72
73
74
75     /** Example implementation: Stripping C-style comments from Java files, passed as arguments. */
76     public static void main(String JavaDoc [] args) {
77         if (args.length <= 0) {
78             System.err.println("SYNTAX: java "+CStyleCommentStrip.class.getName()+" file.java [file.java ...]");
79             System.err.println(" Strips // C-style /* comments */ from C/Java sources.");
80         }
81         else {
82             try {
83                 for (int i = 0; i < args.length; i++) {
84                     PrintWriter out = new PrintWriter(System.out);
85                     Reader in = new BufferedReader(new FileReader(args[i]));
86                     new CStyleCommentStrip(in, out);
87                 }
88             }
89             catch (Exception JavaDoc e) {
90                 e.printStackTrace();
91             }
92         }
93     }
94
95 }
96
Popular Tags