01: package fri.patterns.interpreter.parsergenerator.lexer.examples;
02:
03: import java.io.*;
04: import fri.patterns.interpreter.parsergenerator.Token;
05: import fri.patterns.interpreter.parsergenerator.Lexer;
06: import fri.patterns.interpreter.parsergenerator.syntax.*;
07: import fri.patterns.interpreter.parsergenerator.lexer.*;
08: import fri.patterns.interpreter.parsergenerator.syntax.builder.SyntaxSeparation;
09:
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: private static Lexer lexer;
19:
20: static {
21: try {
22: String[][] rules = {
23: { Token.TOKEN, "others" }, // define what we want to receive
24: { Token.TOKEN, "`stringdef`" }, // need this rule, as string definitions could contain comments
25: { Token.IGNORED, "`cstylecomment`" },
26: { "others", "others", "other" },
27: { "others", "other" },
28: { "other", "`char`", Token.BUTNOT,
29: "`cstylecomment`", Token.BUTNOT,
30: "`stringdef`" }, };
31:
32: Syntax syntax = new Syntax(rules); // LexerBuilder makes unique
33: SyntaxSeparation separation = new SyntaxSeparation(syntax);
34: LexerBuilder builder = new LexerBuilder(separation
35: .getLexerSyntax(), separation.getIgnoredSymbols());
36: lexer = builder.getLexer();
37: //lexer.setDebug(true); // dumps scanner consumer lists
38: lexer.setTerminals(separation.getTokenSymbols());
39: } catch (Exception e) {
40: e.printStackTrace();
41: }
42: }
43:
44: /**
45: Stripping C-style comments from an input reader and writing to output writer.
46: Both in and out get closed when finished.
47: */
48: public CStyleCommentStrip(Reader in, Writer out)
49: throws LexerException, IOException {
50: try {
51: lexer.setInput(in);
52:
53: Token t;
54: do {
55: t = lexer.getNextToken(null);
56:
57: if (t.symbol == null)
58: lexer.dump(System.err);
59: else if (t.text != null)
60: out.write(t.text.toString());
61: } while (t.symbol != null && Token.isEpsilon(t) == false);
62: } finally {
63: try {
64: in.close();
65: } catch (Exception e) {
66: e.printStackTrace();
67: }
68: try {
69: out.close();
70: } catch (Exception e) {
71: e.printStackTrace();
72: }
73: }
74: }
75:
76: /** Example implementation: Stripping C-style comments from Java files, passed as arguments. */
77: public static void main(String[] args) {
78: if (args.length <= 0) {
79: System.err.println("SYNTAX: java "
80: + CStyleCommentStrip.class.getName()
81: + " file.java [file.java ...]");
82: System.err
83: .println(" Strips // C-style /* comments */ from C/Java sources.");
84: } else {
85: try {
86: for (int i = 0; i < args.length; i++) {
87: PrintWriter out = new PrintWriter(System.out);
88: Reader in = new BufferedReader(new FileReader(
89: args[i]));
90: new CStyleCommentStrip(in, out);
91: }
92: } catch (Exception e) {
93: e.printStackTrace();
94: }
95: }
96: }
97:
98: }
|