compiler/PPCLL1Visitor.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 23 Nov 2015 11:14:30 +0100
changeset 551 00ebb1b85f53
parent 454 a9cd5ea7cc36
permissions -rw-r--r--
Fixed CI scripts on Windows For an unknown reason, unzip on Windows reports status code 50 (presumably "the disk is (or was) full during extraction.") even if there's plenty of space. To workaround this, simply ignore status code 50 on Windows. Sigh.

"{ 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 do:[:e1 |
        firsts do:[:e2 |     
            e1 == e2 ifFalse: [
                (e1 overlapsWith: e2) ifTrue: [ ^ false ] ].
        ].
    ].
    ^ true

    "Modified: / 10-05-2015 / 07:27:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

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
! !