KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > ParserGrammar


1 package antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/ParserGrammar.java#6 $
8  */

9
10 import java.util.Hashtable JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 import antlr.collections.impl.BitSet;
15 import antlr.collections.impl.Vector;
16
17
18 /** Parser-specific grammar subclass */
19 class ParserGrammar extends Grammar {
20
21
22     ParserGrammar(String JavaDoc className_, Tool tool_, String JavaDoc superClass) {
23         super(className_, tool_, superClass);
24     }
25
26     /** Top-level call to generate the code for this grammar */
27     public void generate() throws IOException JavaDoc {
28         generator.gen(this);
29     }
30
31     // Get name of class from which generated parser/lexer inherits
32
protected String JavaDoc getSuperClass() {
33         // if debugging, choose the debugging version of the parser
34
if (debuggingOutput)
35             return "debug.LLkDebuggingParser";
36         return "LLkParser";
37     }
38
39     /**Process command line arguments.
40      * -trace have all rules call traceIn/traceOut
41      * -traceParser have parser rules call traceIn/traceOut
42      * -debug generate debugging output for parser debugger
43      */

44     public void processArguments(String JavaDoc[] args) {
45         for (int i = 0; i < args.length; i++) {
46             if (args[i].equals("-trace")) {
47                 traceRules = true;
48                 antlrTool.setArgOK(i);
49             }
50             else if (args[i].equals("-traceParser")) {
51                 traceRules = true;
52                 antlrTool.setArgOK(i);
53             }
54             else if (args[i].equals("-debug")) {
55                 debuggingOutput = true;
56                 antlrTool.setArgOK(i);
57             }
58         }
59     }
60
61     /** Set parser options -- performs action on the following options:
62      */

63     public boolean setOption(String JavaDoc key, Token value) {
64         String JavaDoc s = value.getText();
65         if (key.equals("buildAST")) {
66             if (s.equals("true")) {
67                 buildAST = true;
68             }
69             else if (s.equals("false")) {
70                 buildAST = false;
71             }
72             else {
73                 antlrTool.error("buildAST option must be true or false", getFilename(), value.getLine(), value.getColumn());
74             }
75             return true;
76         }
77         if (key.equals("interactive")) {
78             if (s.equals("true")) {
79                 interactive = true;
80             }
81             else if (s.equals("false")) {
82                 interactive = false;
83             }
84             else {
85                 antlrTool.error("interactive option must be true or false", getFilename(), value.getLine(), value.getColumn());
86             }
87             return true;
88         }
89         if (key.equals("ASTLabelType")) {
90             super.setOption(key, value);
91             return true;
92         }
93         if (super.setOption(key, value)) {
94             return true;
95         }
96         antlrTool.error("Invalid option: " + key, getFilename(), value.getLine(), value.getColumn());
97         return false;
98     }
99 }
100
Popular Tags