Source Code Cross Referenced for Nullable.java in  » Parser » runcc » fri » patterns » interpreter » parsergenerator » parsertables » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Parser » runcc » fri.patterns.interpreter.parsergenerator.parsertables 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package fri.patterns.interpreter.parsergenerator.parsertables;
002:
003:        import java.util.*;
004:        import fri.patterns.interpreter.parsergenerator.Token;
005:        import fri.patterns.interpreter.parsergenerator.syntax.*;
006:
007:        /**
008:         Nullability of a nonterminal means that it can be "nothing", i.e.
009:         there is a rule that contains the nonterminal on the left side and
010:         no symbol on the right side (empty right side), expressing an optional
011:         rule.
012:         <p>
013:         <b>Algorithm:</b><br>
014:         Sort rules by size.
015:         Search every rule that contain the nonterminal on left side.
016:         Scan the left side of every rule until a terminal or a non-nullable
017:         nonterminal appears. If found, rule is not nullable, else it is nullable.
018:         If one of the rules that derive nonterminal is nullable, this nonterminal
019:         is nullable.
020:
021:         @author (c) 2000, Fritz Ritzberger
022:         */
023:
024:        class Nullable extends Hashtable {
025:            /** The special empty symbol. */
026:            public static final String NULL = "";
027:
028:            /**
029:            	Explores the nullability of all nonterminals in syntax. Provides nullability
030:            	as Boolean within this Map, key is nonterminal.
031:            	@param syntax the grammar to scan for nonterminals and their nullability.
032:            	@param nonterminals pre-built list of nonterminals.
033:             */
034:            public Nullable(Syntax syntax, List nonterminals)
035:                    throws ParserBuildException {
036:                // loop nonterminals
037:                Map done = new Hashtable(nonterminals.size()); // avoid endless loops
038:                for (int i = 0; i < nonterminals.size(); i++) {
039:                    String nt = (String) nonterminals.get(i);
040:                    checkNullability(syntax, nt, done);
041:                }
042:            }
043:
044:            /** Returns true if passed nonterminal is nullable, else false. */
045:            public boolean isNullable(String nonterminal) {
046:                Boolean nullable = (Boolean) get(nonterminal);
047:                return nullable.booleanValue();
048:            }
049:
050:            private boolean checkNullability(Syntax syntax, String nonterm,
051:                    Map done) throws ParserBuildException {
052:                Boolean n = (Boolean) get(nonterm);
053:                if (n != null)
054:                    return n.booleanValue();
055:
056:                if (done.get(nonterm) != null)
057:                    return false; // endless recursion
058:
059:                done.put(nonterm, nonterm); // avoid endless loops
060:
061:                // loop rules for an empty rule deriving this nonterminal
062:                for (int j = 0; j < syntax.size(); j++) {
063:                    Rule rule = syntax.getRule(j);
064:                    String nt = rule.getNonterminal(); // left side of derivation
065:
066:                    if (nt.equals(nonterm) && rule.rightSize() <= 0) // this rule derives the nonterminal and is empty
067:                        return putSymbol(nonterm, true);
068:                }
069:
070:                // loop rules for nullable nonterminal sequences
071:                for (int j = 0; j < syntax.size(); j++) {
072:                    Rule rule = syntax.getRule(j);
073:                    String nt = rule.getNonterminal(); // left side of derivation
074:
075:                    if (nt.equals(nonterm)) { // this rule derives the nonterminal
076:                        boolean nullable = true; // assume it is nullable
077:
078:                        // then all symbols on right side must be nullable
079:                        for (int i = 0; nullable && i < rule.rightSize(); i++) {
080:                            String symbol = rule.getRightSymbol(i);
081:
082:                            if (Token.isTerminal(symbol)) { // a terminal ends symbol-loop
083:                                nullable = false;
084:                            } else if (symbol.equals(nonterm) == false) { // do not search self
085:                                try {
086:                                    nullable = checkNullability(syntax, symbol,
087:                                            done); // is nullable when this symbol is nullable
088:                                } catch (Exception ex) {
089:                                    throw new ParserBuildException(
090:                                            "Nullable ERROR: "
091:                                                    + ex.getMessage() + " <- "
092:                                                    + nonterm);
093:                                }
094:                            }
095:                        }
096:
097:                        if (nullable) // one nullable rule is enough for nonterminal to be nullable
098:                            return putSymbol(nonterm, true);
099:                    }
100:                }
101:                return putSymbol(nonterm, false);
102:            }
103:
104:            private boolean putSymbol(String symbol, boolean value) {
105:                put(symbol, new Boolean(value));
106:                return value;
107:            }
108:
109:            /**
110:            	Detect an empty terminal in input grammar: length == 0,  '', "", ``.
111:            	@return true when symbol is empty.
112:             */
113:            public static boolean isNull(String symbol) {
114:                return symbol.length() <= 0
115:                        || symbol.equals("" + Token.STRING_QUOTE
116:                                + Token.STRING_QUOTE)
117:                        || symbol.equals("" + Token.COMMAND_QUOTE
118:                                + Token.COMMAND_QUOTE)
119:                        || symbol.equals("" + Token.CHAR_QUOTE
120:                                + Token.CHAR_QUOTE);
121:            }
122:
123:            // test main
124:
125:            public static void main(String[] args) {
126:                List nt = new ArrayList();
127:                nt.add("S");
128:                nt.add("T");
129:                nt.add("F");
130:                nt.add("L");
131:
132:                List sx = new ArrayList();
133:
134:                List r = new ArrayList();
135:                r.add("S");
136:                r.add("T");
137:                r.add("L");
138:                r.add("F");
139:                sx.add(r);
140:
141:                r = new ArrayList();
142:                r.add("S");
143:                r.add("T");
144:                r.add("F");
145:                sx.add(r);
146:
147:                r = new ArrayList();
148:                r.add("T");
149:                sx.add(r);
150:
151:                r = new ArrayList();
152:                r.add("F");
153:                //r.add("S");
154:                sx.add(r);
155:
156:                try {
157:                    Nullable n = new Nullable(new Syntax(sx), nt);
158:                    String s = "S";
159:                    System.err
160:                            .println("nullable " + s + ": " + n.isNullable(s));
161:                } catch (Exception e) {
162:                    e.printStackTrace();
163:                }
164:            }
165:
166:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.