Source Code Cross Referenced for LR1State.java in  » Parser » SJPT » ro » infoiasi » donald » compiler » parser » 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 » SJPT » ro.infoiasi.donald.compiler.parser 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package ro.infoiasi.donald.compiler.parser;
002:
003:        import ro.infoiasi.donald.compiler.cfg.*;
004:        import java.util.*;
005:
006:        public class LR1State {
007:            private int index;
008:            private Set kernel = new LinkedHashSet();
009:            private Set closure = new LinkedHashSet();
010:
011:            private Map itemsByNext = new LinkedHashMap();
012:
013:            public boolean equals(Object o) {
014:                return (o instanceof  LR1State
015:                        && kernel.equals(((LR1State) o).kernel) && closure
016:                        .equals(((LR1State) o).closure));
017:            }
018:
019:            public int hashCode() {
020:                return 17 + kernel.hashCode() * 37 + closure.hashCode();
021:            }
022:
023:            public boolean isEmpty() {
024:                return kernel.isEmpty();
025:            }
026:
027:            private void addToItemsByNext(LR1Item item) {
028:                if (!item.isComplete()) {
029:                    Symbol sym = item.getNextSymbol();
030:                    LinkedList itemList;
031:                    if (itemsByNext.containsKey(sym)) {
032:                        itemList = (LinkedList) itemsByNext.get(sym);
033:                    } else {
034:                        itemList = new LinkedList();
035:                    }
036:                    itemList.add(item);
037:                    itemsByNext.put(sym, itemList);
038:                }
039:            }
040:
041:            public boolean addKernelItem(LR1Item item) {
042:                if (kernel.add(item)) {
043:                    addToItemsByNext(item);
044:                    return true;
045:                } else {
046:                    return false;
047:                }
048:            }
049:
050:            public Collection addKernelItemWithClosure(LR1Item item, CFG g) {
051:                if (addKernelItem(item)) {
052:                    Collection closureKernel = new LinkedList();
053:                    closureKernel.add(item);
054:                    return closure(closureKernel, g);
055:                } else {
056:                    return null;
057:                }
058:            }
059:
060:            public Iterator iterator() {
061:                return new Iterator() {
062:                    Iterator it = kernel.iterator();
063:                    boolean inKernel = true;
064:
065:                    public boolean hasNext() {
066:                        if (it.hasNext() || (inKernel && !closure.isEmpty())) {
067:                            return true;
068:                        } else {
069:                            return false;
070:                        }
071:                    }
072:
073:                    public Object next() {
074:                        if (inKernel && !it.hasNext()) {
075:                            it = closure.iterator();
076:                            inKernel = false;
077:                        }
078:                        return it.next();
079:                    }
080:
081:                    public void remove() {
082:                        throw new UnsupportedOperationException();
083:                    }
084:                };
085:            }
086:
087:            public LinkedList find(Symbol x) {
088:                return (LinkedList) itemsByNext.get(x);
089:            }
090:
091:            public Iterator iterator(Symbol x) {
092:                List itemList = find(x);
093:                if (itemList == null) {
094:                    // iterator over the empty list
095:                    return new Iterator() {
096:                        public boolean hasNext() {
097:                            return false;
098:                        }
099:
100:                        public Object next() {
101:                            throw new NoSuchElementException();
102:                        }
103:
104:                        public void remove() {
105:                            throw new UnsupportedOperationException();
106:                        }
107:                    };
108:                } else {
109:                    return Collections.unmodifiableList(itemList).iterator();
110:                }
111:            }
112:
113:            public boolean contains(LR1Item item) {
114:                return kernel.contains(item) || closure.contains(item);
115:            }
116:
117:            private Collection closure(Collection closureKernel, CFG g) {
118:                Collection newClosureItems = new LinkedList(closureKernel);
119:                LinkedList queue = new LinkedList(closureKernel);
120:
121:                while (!queue.isEmpty()) {
122:                    LR1Item item = (LR1Item) queue.removeFirst();
123:
124:                    if (!item.isComplete()) {
125:                        Symbol sym = (Symbol) item.getNextSymbol();
126:                        if (!sym.isTerminal()) {
127:                            Iterator firstIt = g.first(
128:                                    item.getSufixAndLookahead()).iterator();
129:                            while (firstIt.hasNext()) {
130:                                Terminal newLookahead = (Terminal) firstIt
131:                                        .next();
132:                                List prodList = g.getProductions().find(
133:                                        (NonTerminal) sym);
134:                                Iterator pIt = prodList.iterator();
135:                                while (pIt.hasNext()) {
136:                                    Production prod = (Production) pIt.next();
137:                                    LR1Item newItem = new LR1Item(new LR0Item(
138:                                            prod), newLookahead);
139:                                    if (closure.add(newItem)) {
140:                                        addToItemsByNext(newItem);
141:                                        queue.addLast(newItem);
142:                                        newClosureItems.add(newItem);
143:                                    }
144:                                }
145:                            }
146:                        }
147:                    }
148:                }
149:                return newClosureItems;
150:            }
151:
152:            public Collection closure(CFG g) {
153:                return closure(kernel, g);
154:            }
155:
156:            void setIndex(int index) {
157:                this .index = index;
158:            }
159:
160:            public int getIndex() {
161:                return index;
162:            }
163:
164:            public Collection getKernelItems() {
165:                return Collections.unmodifiableSet(kernel);
166:            }
167:
168:            public Collection getClosureItems() {
169:                return Collections.unmodifiableSet(closure);
170:            }
171:
172:            public String toString() {
173:                StringBuffer sb = new StringBuffer();
174:                sb.append("{");
175:                Iterator it = kernel.iterator();
176:                while (it.hasNext()) {
177:                    sb.append(it.next());
178:                    if (it.hasNext()) {
179:                        sb.append(", ");
180:                    }
181:                }
182:                sb.append("; ");
183:                it = closure.iterator();
184:                while (it.hasNext()) {
185:                    sb.append(it.next());
186:                    if (it.hasNext()) {
187:                        sb.append(", ");
188:                    }
189:                }
190:                sb.append("}");
191:                return new String(sb);
192:            }
193:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.