KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > model > grammar > Production


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.model.grammar;
10
11 import net.sourceforge.chaperon.model.Violations;
12 import net.sourceforge.chaperon.model.symbol.Nonterminal;
13 import net.sourceforge.chaperon.model.symbol.SymbolList;
14 import net.sourceforge.chaperon.model.symbol.SymbolSet;
15 import net.sourceforge.chaperon.model.symbol.Terminal;
16
17 import java.io.Serializable JavaDoc;
18
19 /**
20  * This class presents a production of a grammar
21  *
22  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
23  * @version CVS $Id: Production.java,v 1.6 2003/12/09 19:55:52 benedikta Exp $
24  */

25 public class Production implements Serializable JavaDoc, Cloneable JavaDoc
26 {
27   private Nonterminal ntsymbol = new Nonterminal("noname");
28   private SymbolList definition = new SymbolList();
29   private Terminal precedence = null;
30   private String JavaDoc location = null;
31
32   /**
33    * Create an empty production.
34    */

35   public Production() {}
36
37   /**
38    * Create a production.
39    *
40    * @param ntsymbol The symbol of this production.
41    */

42   public Production(Nonterminal ntsymbol)
43   {
44     setSymbol(ntsymbol);
45   }
46
47   /**
48    * Create a production.
49    *
50    * @param ntsymbol The symbol of this production.
51    * @param definition Definition of the production.
52    */

53   public Production(Nonterminal ntsymbol, SymbolList definition)
54   {
55     setSymbol(ntsymbol);
56     setDefinition(definition);
57   }
58
59   /**
60    * Set the symbol for this production
61    *
62    * @param ntsymbol Non terminal symbol
63    */

64   public void setSymbol(Nonterminal ntsymbol)
65   {
66     if (ntsymbol==null)
67       throw new NullPointerException JavaDoc();
68
69     this.ntsymbol = ntsymbol;
70   }
71
72   /**
73    * Return the symbol from this production
74    *
75    * @return Nonterminal symbol
76    */

77   public Nonterminal getSymbol()
78   {
79     return ntsymbol;
80   }
81
82   /**
83    * Set the definition for this production
84    *
85    * @param definition A list of symbols, which the definition of this production represented.
86    */

87   public void setDefinition(SymbolList definition)
88   {
89     if (definition==null)
90       throw new NullPointerException JavaDoc();
91
92     this.definition = definition;
93   }
94
95   /**
96    * Return the definition.
97    *
98    * @return A list of Symbols, which the definition of this production represented.
99    */

100   public SymbolList getDefinition()
101   {
102     return definition;
103   }
104
105   /**
106    * Return the the length of the Definition
107    *
108    * @return Lengt of the Definition
109    */

110   public int getLength()
111   {
112     return definition.getSymbolCount();
113   }
114
115   /**
116    * Set the precedence for this production by a terminal symbol
117    *
118    * @param tsymbol Terminal symbol
119    */

120   public void setPrecedence(Terminal tsymbol)
121   {
122     precedence = tsymbol;
123   }
124
125   /**
126    * Return the precedence symbol.
127    *
128    * @return Terminal symbol.
129    */

130   public Terminal getPrecedence()
131   {
132     return precedence;
133   }
134
135   /**
136    * If the production contains a precedence symbol.
137    *
138    * @return True, if a precedence symbol is defined.
139    */

140   public boolean hasPrecedence()
141   {
142     return precedence!=null;
143   }
144
145   /**
146    * Return all used symbols in this production
147    *
148    * @return Set of symbols.
149    */

150   public SymbolSet getSymbols()
151   {
152     SymbolSet set = new SymbolSet();
153
154     set.addSymbol(ntsymbol);
155     set.addSymbol(definition);
156     if (precedence!=null)
157       set.addSymbol(precedence);
158
159     return set;
160   }
161
162   /**
163    * Set the location from the input source.
164    *
165    * @param location Location in the input source.
166    */

167   public void setLocation(String JavaDoc location)
168   {
169     this.location = location;
170   }
171
172   /**
173    * Returns the location from the input source.
174    *
175    * @return Location in the input source.
176    */

177   public String JavaDoc getLocation()
178   {
179     return location;
180   }
181
182   /**
183    * Validates the production.
184    *
185    * @return Return a list of violations, if this object isn't valid.
186    */

187   public Violations validate()
188   {
189     Violations violations = new Violations();
190
191     if (ntsymbol==null)
192       violations.addViolation("No symbol is for the left side defined", location);
193
194     /*if ((definition==null) || (definition.getSymbolCount()<=0))
195       violations.addViolation("No symbols are for the right side defined",
196                               location);*/

197     return violations;
198   }
199
200   /**
201    * Compares the production with another production.
202    *
203    * @param o Other object.
204    *
205    * @return True, if the production are equal.
206    */

207   public boolean equals(Object JavaDoc o)
208   {
209     if (o==this)
210       return true;
211
212     if (o instanceof Production)
213     {
214       Production production = (Production)o;
215
216       if (precedence!=null)
217         return (ntsymbol.equals(production.ntsymbol)) &&
218                (definition.equals(production.definition)) &&
219                (precedence.equals(production.precedence));
220       else
221         return (ntsymbol.equals(production.ntsymbol)) &&
222                (definition.equals(production.definition)) && (production.precedence==null);
223     }
224
225     return false;
226   }
227
228   /**
229    * Return a string representation of the production.
230    *
231    * @return String representation of the production.
232    */

233   public String JavaDoc toString()
234   {
235     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
236
237     buffer.append(ntsymbol+" := ");
238     for (int j = 0; j<definition.getSymbolCount(); j++)
239       buffer.append(definition.getSymbol(j)+" ");
240
241     if (hasPrecedence())
242       buffer.append("[precedence="+getPrecedence()+"]");
243
244     return buffer.toString();
245   }
246
247   /**
248    * @return
249    *
250    * @throws CloneNotSupportedException
251    */

252   public Object JavaDoc clone()
253   {
254     Production clone = new Production();
255
256     clone.ntsymbol = ntsymbol;
257     clone.definition = (SymbolList)definition.clone();
258     clone.precedence = precedence;
259     clone.location = location;
260
261     return clone;
262   }
263 }
264
Popular Tags