KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > test > ArithmeticParser


1 /*
2  * ArithmeticParser.java
3  *
4  * THIS FILE HAS BEEN GENERATED AUTOMATICALLY. DO NOT EDIT!
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1
9  * of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19  * MA 02111-1307, USA.
20  *
21  * Copyright (c) 2003 Per Cederberg. All rights reserved.
22  */

23
24 package net.percederberg.grammatica.test;
25
26 import java.io.Reader JavaDoc;
27
28 import net.percederberg.grammatica.parser.Analyzer;
29 import net.percederberg.grammatica.parser.ParserCreationException;
30 import net.percederberg.grammatica.parser.ProductionPattern;
31 import net.percederberg.grammatica.parser.ProductionPatternAlternative;
32 import net.percederberg.grammatica.parser.RecursiveDescentParser;
33
34 /**
35  * A token stream parser.
36  *
37  * @author Per Cederberg, <per at percederberg dot net>
38  * @version 1.0
39  */

40 class ArithmeticParser extends RecursiveDescentParser {
41
42     /**
43      * Creates a new parser.
44      *
45      * @param in the input stream to read from
46      *
47      * @throws ParserCreationException if the parser couldn't be
48      * initialized correctly
49      */

50     public ArithmeticParser(Reader JavaDoc in) throws ParserCreationException {
51         super(new ArithmeticTokenizer(in));
52         createPatterns();
53     }
54
55     /**
56      * Creates a new parser.
57      *
58      * @param in the input stream to read from
59      * @param analyzer the analyzer to use while parsing
60      *
61      * @throws ParserCreationException if the parser couldn't be
62      * initialized correctly
63      */

64     public ArithmeticParser(Reader JavaDoc in, Analyzer analyzer)
65         throws ParserCreationException {
66
67         super(new ArithmeticTokenizer(in), analyzer);
68         createPatterns();
69     }
70
71     /**
72      * Initializes the parser by creating all the production patterns.
73      *
74      * @throws ParserCreationException if the parser couldn't be
75      * initialized correctly
76      */

77     private void createPatterns() throws ParserCreationException {
78         ProductionPattern pattern;
79         ProductionPatternAlternative alt;
80
81         pattern = new ProductionPattern(ArithmeticConstants.EXPRESSION,
82                                         "Expression");
83         alt = new ProductionPatternAlternative();
84         alt.addProduction(ArithmeticConstants.TERM, 1, 1);
85         alt.addProduction(ArithmeticConstants.EXPRESSION_REST, 0, 1);
86         pattern.addAlternative(alt);
87         addPattern(pattern);
88
89         pattern = new ProductionPattern(ArithmeticConstants.EXPRESSION_REST,
90                                         "ExpressionRest");
91         alt = new ProductionPatternAlternative();
92         alt.addToken(ArithmeticConstants.ADD, 1, 1);
93         alt.addProduction(ArithmeticConstants.EXPRESSION, 1, 1);
94         pattern.addAlternative(alt);
95         alt = new ProductionPatternAlternative();
96         alt.addToken(ArithmeticConstants.SUB, 1, 1);
97         alt.addProduction(ArithmeticConstants.EXPRESSION, 1, 1);
98         pattern.addAlternative(alt);
99         addPattern(pattern);
100
101         pattern = new ProductionPattern(ArithmeticConstants.TERM,
102                                         "Term");
103         alt = new ProductionPatternAlternative();
104         alt.addProduction(ArithmeticConstants.FACTOR, 1, 1);
105         alt.addProduction(ArithmeticConstants.TERM_REST, 0, 1);
106         pattern.addAlternative(alt);
107         addPattern(pattern);
108
109         pattern = new ProductionPattern(ArithmeticConstants.TERM_REST,
110                                         "TermRest");
111         alt = new ProductionPatternAlternative();
112         alt.addToken(ArithmeticConstants.MUL, 1, 1);
113         alt.addProduction(ArithmeticConstants.TERM, 1, 1);
114         pattern.addAlternative(alt);
115         alt = new ProductionPatternAlternative();
116         alt.addToken(ArithmeticConstants.DIV, 1, 1);
117         alt.addProduction(ArithmeticConstants.TERM, 1, 1);
118         pattern.addAlternative(alt);
119         addPattern(pattern);
120
121         pattern = new ProductionPattern(ArithmeticConstants.FACTOR,
122                                         "Factor");
123         alt = new ProductionPatternAlternative();
124         alt.addProduction(ArithmeticConstants.ATOM, 1, 1);
125         pattern.addAlternative(alt);
126         alt = new ProductionPatternAlternative();
127         alt.addToken(ArithmeticConstants.LEFT_PAREN, 1, 1);
128         alt.addProduction(ArithmeticConstants.EXPRESSION, 1, 1);
129         alt.addToken(ArithmeticConstants.RIGHT_PAREN, 1, 1);
130         pattern.addAlternative(alt);
131         addPattern(pattern);
132
133         pattern = new ProductionPattern(ArithmeticConstants.ATOM,
134                                         "Atom");
135         alt = new ProductionPatternAlternative();
136         alt.addToken(ArithmeticConstants.NUMBER, 1, 1);
137         pattern.addAlternative(alt);
138         alt = new ProductionPatternAlternative();
139         alt.addToken(ArithmeticConstants.IDENTIFIER, 1, 1);
140         pattern.addAlternative(alt);
141         addPattern(pattern);
142     }
143 }
144
Popular Tags