Source Code Cross Referenced for LALRSyntaxNode.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.syntax.*;
005:
006:        /**
007:         LR bottom-up parser syntax node. This node type contains an
008:         algorithm to propagate lookahead-sets of items to dependent items
009:         after syntax nodes were constructed.
010:        
011:         @see fri.patterns.interpreter.parsergenerator.parsertables.LRSyntaxNode
012:         @author (c) 2000, Fritz Ritzberger
013:         */
014:
015:        class LALRSyntaxNode extends LRSyntaxNode {
016:            /** Construction of node with FIRST-sets and nullability of all nonterminals in syntax. */
017:            public LALRSyntaxNode(Nullable nullable, FirstSets firstSets) {
018:                super (nullable, firstSets);
019:            }
020:
021:            /** Factory-method that constructs a LALRSyntaxNode. */
022:            protected SLRSyntaxNode createSyntaxNode() {
023:                return new LALRSyntaxNode(nullable, firstSets);
024:            }
025:
026:            /**
027:            	Factory-method that constructs a LALRRuleStateItem.
028:            	A start-lookahead gets appended to the item when it is the start node.
029:             */
030:            protected RuleStateItem createRuleStateItem(int ruleIndex, Rule rule) {
031:                LALRRuleStateItem item = new LALRRuleStateItem(ruleIndex, rule);
032:                addStartLookahead(item, ruleIndex);
033:                return item;
034:            }
035:
036:            /** Calls super. After build lookaheads get propagated. */
037:            public List build(Syntax syntax, List syntaxNodes, Hashtable kernels) {
038:                syntaxNodes = super .build(syntax, syntaxNodes, kernels);
039:
040:                // propagate lookaheads
041:                for (int i = 0; i < syntaxNodes.size(); i++) {
042:                    LALRSyntaxNode node = (LALRSyntaxNode) syntaxNodes.get(i);
043:                    for (Enumeration e = node.entries.elements(); e
044:                            .hasMoreElements();) {
045:                        LALRRuleStateItem item = (LALRRuleStateItem) e
046:                                .nextElement();
047:                        item.propagateLookaheads(null);
048:                    }
049:                }
050:                return syntaxNodes;
051:            }
052:
053:            /**
054:            	Method called from closure, adopt all rules that derive the pending nonterminal.
055:            	Default lookaheads are calculated here. Items that need lookahead propagation
056:            	are located here.
057:             */
058:            protected void addRulesDerivingPendingNonTerminal(
059:                    RuleStateItem itm, String nonterm, Syntax syntax,
060:                    List newItems) {
061:                // make the closure for one item:
062:                // if pointer before a nonterminal, add all rules that derive it
063:
064:                LALRRuleStateItem item = (LALRRuleStateItem) itm;
065:                boolean needsPropagation = false;
066:                List lookahead = null;
067:
068:                for (int i = 0; i < syntax.size(); i++) {
069:                    Rule rule = syntax.getRule(i);
070:
071:                    if (rule.getNonterminal().equals(nonterm)) {
072:                        LALRRuleStateItem rsi = (LALRRuleStateItem) createRuleStateItem(
073:                                i, rule);
074:
075:                        // look if new item is already contained
076:                        LALRRuleStateItem existing = (LALRRuleStateItem) entries
077:                                .get(rsi);
078:                        if (existing != null) {
079:                            rsi = existing;
080:                        } else { // if not contained, add it
081:                            entries.put(rsi, rsi); // real list
082:                            newItems.add(rsi); // work list
083:                        }
084:
085:                        if (lookahead == null)
086:                            // calculate lookahead of originator and if it needs to propagate changes in
087:                            // its lookahead to the new items (has nullable nonterms until end)
088:                            needsPropagation = item.calculateLookahead(
089:                                    lookahead = new ArrayList(), nullable,
090:                                    firstSets);
091:
092:                        rsi.addLookahead(lookahead.iterator()); // merge lookaheads
093:
094:                        if (needsPropagation) // if lookahead is visible from this item,
095:                            item.addPropagate(rsi); // add to propagate list
096:                    }
097:                }
098:            }
099:
100:            /**
101:            	Called from closure, connect a rule state item to its follower.
102:            	Lookahead-Propagation gets prepared by linking parent to child.
103:             */
104:            protected void linkParentItemToChild(RuleStateItem parent,
105:                    int newIndex, List syntaxNodes, RuleStateItem child) {
106:                LALRRuleStateItem pnt = (LALRRuleStateItem) parent;
107:                pnt.followNodeIndex = newIndex;
108:
109:                LALRSyntaxNode node = (LALRSyntaxNode) syntaxNodes
110:                        .get(newIndex);
111:
112:                // find corresponding item
113:                LALRRuleStateItem rsi = (LALRRuleStateItem) node.entries
114:                        .get(child);
115:
116:                // probably will have to propagate lookaheads to shifted item
117:                pnt.addPropagate(rsi);
118:            }
119:
120:            /**
121:            	Rule state entry item class, contained within LALR syntax node.
122:            	Lookahead propagation is implemented here.
123:             */
124:            protected class LALRRuleStateItem extends LRRuleStateItem {
125:                boolean needsPropagation = false;
126:                Stack propagateItems = new Stack();
127:
128:                public LALRRuleStateItem(int ruleIndex, Rule rule) {
129:                    super (ruleIndex, rule);
130:                }
131:
132:                protected LALRRuleStateItem(RuleStateItem orig) {
133:                    super (orig);
134:                }
135:
136:                /** Factory-method creating LALRRuleStateItem. */
137:                protected RuleStateItem createRuleStateItem(RuleStateItem orig) {
138:                    return new LALRRuleStateItem(orig);
139:                }
140:
141:                /** Add an item that need lookahead propagation by this one. */
142:                void addPropagate(RuleStateItem item) {
143:                    propagateItems.push(item);
144:                    needsPropagation = true;
145:                }
146:
147:                /** Accept incoming lookahead and propagate the own lookahead. */
148:                void propagateLookaheads(Iterator originatorLookahead) {
149:                    boolean change = false;
150:
151:                    /// return when if nothing to propagate
152:                    if (needsPropagation == false
153:                            && (originatorLookahead == null || originatorLookahead
154:                                    .hasNext() == false))
155:                        return;
156:
157:                    if (originatorLookahead != null)
158:                        change = addLookahead(originatorLookahead); // accept lookahead
159:
160:                    // if changed or need it, propagate across all linked items
161:                    if (change || needsPropagation) {
162:                        needsPropagation = false;
163:
164:                        for (int i = 0; i < propagateItems.size(); i++) {
165:                            LALRRuleStateItem item = (LALRRuleStateItem) propagateItems
166:                                    .get(i);
167:                            item.propagateLookaheads(lookahead.keySet()
168:                                    .iterator());
169:                        }
170:                    }
171:                }
172:
173:                /** Rule index and position of dot must be equal. */
174:                public boolean equals(Object o) {
175:                    RuleStateItem item = (RuleStateItem) o;
176:                    return ruleIndex == item.ruleIndex
177:                            && pointerPosition == item.pointerPosition;
178:                }
179:
180:                /** The ruleIndex * 13 + dot poisition. */
181:                public int hashCode() {
182:                    if (hashCache == null)
183:                        hashCache = new Integer(ruleIndex * 13
184:                                + pointerPosition);
185:                    return hashCache.intValue();
186:                }
187:
188:            } // end class LALRRuleStateItem
189:
190:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.