01: /*
02: [The "BSD licence"]
03: Copyright (c) 2005-2006 Terence Parr
04: All rights reserved.
05:
06: Redistribution and use in source and binary forms, with or without
07: modification, are permitted provided that the following conditions
08: are met:
09: 1. Redistributions of source code must retain the above copyright
10: notice, this list of conditions and the following disclaimer.
11: 2. Redistributions in binary form must reproduce the above copyright
12: notice, this list of conditions and the following disclaimer in the
13: documentation and/or other materials provided with the distribution.
14: 3. The name of the author may not be used to endorse or promote products
15: derived from this software without specific prior written permission.
16:
17: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27: */
28: package org.antlr.tool;
29:
30: import org.antlr.runtime.ANTLRFileStream;
31: import org.antlr.runtime.CharStream;
32: import org.antlr.runtime.CommonTokenStream;
33: import org.antlr.runtime.tree.ParseTree;
34:
35: import java.io.BufferedReader;
36: import java.io.FileReader;
37: import java.util.StringTokenizer;
38:
39: /** Interpret any ANTLR grammar:
40: *
41: * java Interp file.g tokens-to-ignore start-rule input-file
42: *
43: * java Interp C.g 'WS COMMENT' program t.c
44: *
45: * where the WS and COMMENT are the names of tokens you want to have
46: * the parser ignore.
47: */
48: public class Interp {
49: // pass me a java file to parse
50: public static void main(String[] args) throws Exception {
51: if (args.length != 4) {
52: System.err
53: .println("java Interp file.g tokens-to-ignore start-rule input-file");
54: return;
55: }
56: String grammarFileName = args[0];
57: String ignoreTokens = args[1];
58: String startRule = args[2];
59: String inputFileName = args[3];
60:
61: Grammar parser = new Grammar(null, grammarFileName,
62: new BufferedReader(new FileReader(grammarFileName)));
63:
64: String lexerGrammarText = parser.getLexerGrammar();
65: Grammar lexer = new Grammar();
66: lexer.importTokenVocabulary(parser);
67: lexer.setGrammarContent(lexerGrammarText);
68: CharStream input = new ANTLRFileStream(inputFileName);
69: Interpreter lexEngine = new Interpreter(lexer, input);
70: CommonTokenStream tokens = new CommonTokenStream(lexEngine);
71: StringTokenizer tk = new StringTokenizer(ignoreTokens, " ");
72: while (tk.hasMoreTokens()) {
73: String tokenName = tk.nextToken();
74: tokens.setTokenTypeChannel(lexer.getTokenType(tokenName),
75: 99);
76: }
77:
78: if (parser.getRule(startRule) == null) {
79: System.err.println("Rule " + startRule
80: + " does not exist in " + grammarFileName);
81: return;
82: }
83: Interpreter parseEngine = new Interpreter(parser, tokens);
84: ParseTree t = parseEngine.parse(startRule);
85: System.out.println(t.toStringTree());
86: }
87: }
|