KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > beaver > spec > Grammar


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * This file is part of Beaver Parser Generator. *
3  * Copyright (C) 2003,2004 Alexander Demenchuk <alder@softanvil.com>. *
4  * All rights reserved. *
5  * See the file "LICENSE" for the terms and conditions for copying, *
6  * distribution and modification of Beaver. *
7  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

8
9 package beaver.spec;
10
11 import beaver.comp.util.BitSet;
12
13 /**
14  * Parsed specification
15  */

16 public class Grammar
17 {
18     static public class Exception extends java.lang.Exception JavaDoc
19     {
20         public Exception(String JavaDoc msg)
21         {
22             super(msg);
23         }
24     }
25
26     static public final String JavaDoc EBNF_LIST_TYPE = "java.util.ArrayList";
27     static public final String JavaDoc EBNF_LIST_TYPE_NAME = EBNF_LIST_TYPE.substring(EBNF_LIST_TYPE.lastIndexOf('.') + 1);
28     
29     /** Content of the "prolog" section to be copied verbatim at the beginning of the generated parser source file. */
30     public String JavaDoc prolog;
31
32     /** Java package of the parser */
33     public String JavaDoc package_name;
34     
35     /** List of Java types and/or packages that wil be declared in the "import" section of a generated source file. */
36     public String JavaDoc[] imports;
37     
38     /** New parser class name */
39     public String JavaDoc class_name;
40
41     /** Declarations edded to the parser class through %embed */
42     public String JavaDoc class_code;
43
44     /** Part of parser instance initialization code added though %init */
45     public String JavaDoc init_code;
46
47     /** Terminal symbols used in the grammar. */
48     public Terminal[] terminals;
49
50     /** NonTerminal symbols used in the grammar. */
51     public NonTerminal[] nonterminals;
52     
53     /** The list of production rules */
54     public Production[] rules;
55     
56     /** grammar's goal */
57     public NonTerminal goal_symbol;
58
59     /** "$" terminal symbol */
60     public Terminal eof;
61
62     /** Cached "error" nonterminal */
63     public NonTerminal error;
64
65     Grammar()
66     {
67         eof = new Terminal("EOF");
68         error = new NonTerminal("error");
69     }
70
71     public void markNullableProductions()
72     {
73         boolean changed;
74         do
75         {
76             changed = false;
77
78             for (int i = 0; i < nonterminals.length; i++)
79             {
80                 if (nonterminals[i].checkNullability())
81                 {
82                     changed = true;
83                 }
84             }
85         }
86         while (changed);
87     }
88     
89     /**
90      * Find first sets of every nonterminal.
91      * <p/>
92      * The first set is the set of all terminal symbols which can begin a string generated
93      * by that nonterminal.
94      */

95     public void buildFirstSets()
96     {
97         for (int i = 0; i < nonterminals.length; i++)
98         {
99             nonterminals[i].first_set = new BitSet(terminals.length);
100         }
101         for (int i = 0; i < rules.length; i++)
102         {
103             rules[i].startFirstSet();
104         }
105         boolean modified;
106         do
107         {
108             modified = false;
109
110             for (int i = 0; i < rules.length; i++)
111             {
112                 if (rules[i].extendFirstSet())
113                 {
114                     modified = true;
115                 }
116             }
117         }
118         while (modified);
119     }
120 }
121
Popular Tags