compiler/PPCLL1Visitor.st
changeset 452 9f4558b3be66
child 454 a9cd5ea7cc36
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/compiler/PPCLL1Visitor.st	Sun May 10 06:28:36 2015 +0100
@@ -0,0 +1,54 @@
+"{ Package: 'stx:goodies/petitparser/compiler' }"
+
+"{ NameSpace: Smalltalk }"
+
+PPCRewritingVisitor subclass:#PPCLL1Visitor
+	instanceVariableNames:''
+	classVariableNames:''
+	poolDictionaries:''
+	category:'PetitCompiler-Visitors'
+!
+
+!PPCLL1Visitor methodsFor:'as yet unclassified'!
+
+isDeterministicChoice: node
+    | firsts |
+    firsts := OrderedCollection new.
+    node children do: [ :child |
+        (self startsWithToken: child) ifFalse: [ ^ false ].
+        firsts addAll: child firstSetWithTokens.
+    ].
+    (firsts asIdentitySet size = firsts size) ifFalse: [ ^ false ].
+
+    firsts allPairsDo: [ :e1 :e2 | 
+        e1 == e2 ifFalse: [
+            (e1 overlapsWith: e2) ifTrue: [ ^ false ] ].
+    ].
+    ^ true
+!
+
+startsWithToken: node
+    | firstSet terminal total |
+    total := 0.
+    firstSet := node firstSetWithTokens.
+        
+    terminal := (firstSet detect: [ :e | e isTerminal ] ifNone: [ nil ]).
+    terminal isNil ifFalse: [ ^ false ].
+        
+    ^ true
+!
+
+visitChoiceNode: node
+    super visitChoiceNode: node.
+    (self isDeterministicChoice: node) ifTrue: [ 
+        self change.
+        ^ PPCDeterministicChoiceNode new
+            children: node children;
+            name: node name;
+            firstFollowCache: node firstFollowCache;
+            yourself
+    ].
+
+    ^ node
+! !
+