deprecated/buildtools/buildsystemtools/lib/Parse/Yapp/Lalr.pm
author kelvzhu
Wed, 27 Oct 2010 16:03:51 +0800
changeset 662 60be34e1b006
parent 655 3f65fd25dfd4
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     1
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     2
# Module Parse::Yapp::Lalr
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     3
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     4
# (c) Copyright 1998-2001 Francois Desarmenien, all rights reserved.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     5
# (see the pod text in Parse::Yapp module for use and distribution rights)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     6
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     7
package Parse::Yapp::Lalr;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     8
@ISA=qw( Parse::Yapp::Grammar );
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     9
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    10
require 5.004;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    11
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    12
use Parse::Yapp::Grammar;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    13
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    14
=for nobody
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    15
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    16
Parse::Yapp::Compile Object Structure:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    17
--------------------------------------
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    18
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    19
   GRAMMAR =>    Parse::Yapp::Grammar,
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    20
   STATES  =>    [ { CORE    => [ items... ],
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    21
                     ACTIONS  => { term => action }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    22
                     GOTOS   => { nterm => stateno }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    23
                   }... ]
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    24
   CONFLICTS=>{ SOLVED => { stateno  => [ ruleno, token, solved ] }, 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    25
                FORCED => { TOTAL => [ nbsr, nbrr ],
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    26
                            DETAIL => { stateno => { TOTAL => [ nbsr, nbrr ] }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    27
                                                     LIST => [ ruleno, token ]
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    28
                                                   }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    29
                                      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    30
                          } 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    31
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    32
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    33
'items' are of form: [ ruleno, dotpos ]
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    34
'term' in ACTIONS is '' means default action
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    35
'action' may be:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    36
    undef:  explicit error (nonassociativity)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    37
    0    :  accept
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    38
    >0   :  shift and go to state 'action'
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    39
    <0   :  reduce using rule -'action'
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    40
'solved' may have values of:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    41
         'shift'  if solved as Shift
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    42
         'reduce' if solved as Reduce
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    43
         'error'  if solved by discarding both Shift and Reduce (nonassoc)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    44
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    45
SOLVED is a set of states containing Solved conflicts
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    46
FORCED are forced conflict resolutions
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    47
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    48
nbsr and nbrr are number of shift/reduce and reduce/reduce conflicts
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    49
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    50
TOTAL is the total number of SR/RR conflicts for the parser
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    51
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    52
DETAIL is the detail of conflicts for each state
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    53
TOTAL is the total number of SR/RR conflicts for a state
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    54
LIST is the list of discarded reductions (for display purpose only)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    55
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    56
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    57
=cut
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    58
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    59
use strict;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    60
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    61
use Carp;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    62
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    63
###############
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    64
# Constructor #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    65
###############
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    66
sub new {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    67
    my($class)=shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    68
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    69
		ref($class)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    70
	and	$class=ref($class);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    71
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    72
	my($self)=$class->SUPER::new(@_);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    73
    $self->_Compile();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    74
    bless($self,$class);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    75
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    76
###########
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    77
# Methods #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    78
###########
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    79
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    80
###########################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    81
# Method To View Warnings #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    82
###########################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    83
sub Warnings {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    84
    my($self)=shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    85
    my($text);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    86
    my($nbsr,$nbrr)=@{$$self{CONFLICTS}{FORCED}{TOTAL}};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    87
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    88
	$text=$self->SUPER::Warnings();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    89
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    90
        $nbsr != $$self{GRAMMAR}{EXPECT}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    91
    and $text.="$nbsr shift/reduce conflict".($nbsr > 1 ? "s" : "");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    92
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    93
        $nbrr
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    94
    and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    95
            $nbsr
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    96
        and $text.=" and ";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    97
        $text.="$nbrr reduce/reduce conflict".($nbrr > 1 ? "s" : "");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    98
    };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    99
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   100
       (    $nbsr != $$self{GRAMMAR}{EXPECT}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   101
        or  $nbrr)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   102
    and $text.="\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   103
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   104
    $text;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   105
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   106
#############################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   107
# Method To View DFA States #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   108
#############################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   109
sub ShowDfa {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   110
    my($self)=shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   111
    my($text);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   112
    my($grammar,$states)=($$self{GRAMMAR}, $$self{STATES});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   113
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   114
    for my $stateno (0..$#$states) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   115
        my(@shifts,@reduces,@errors,$default);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   116
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   117
        $text.="State $stateno:\n\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   118
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   119
        #Dump Kernel Items
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   120
        for (sort {     $$a[0] <=> $$b[0]
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   121
                    or  $$a[1] <=> $$b[1] } @{$$states[$stateno]{'CORE'}}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   122
            my($ruleno,$pos)=@$_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   123
            my($lhs,$rhs)=@{$$grammar{RULES}[$ruleno]}[0,1];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   124
            my(@rhscopy)=@$rhs;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   125
        
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   126
                $ruleno
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   127
            or  $rhscopy[-1] = '$end';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   128
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   129
            splice(@rhscopy,$pos,0,'.');
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   130
            $text.= "\t$lhs -> ".join(' ',@rhscopy)."\t(Rule $ruleno)\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   131
        }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   132
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   133
        #Prepare Actions
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   134
        for (keys(%{$$states[$stateno]{ACTIONS}})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   135
            my($term,$action)=($_,$$states[$stateno]{ACTIONS}{$_});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   136
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   137
                $term eq chr(0)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   138
            and $term = '$end';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   139
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   140
                not defined($action)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   141
            and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   142
                push(@errors,$term);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   143
                next;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   144
            };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   145
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   146
                $action > 0
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   147
            and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   148
                push(@shifts,[ $term, $action ]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   149
                next;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   150
            };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   151
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   152
            $action = -$action;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   153
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   154
                $term
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   155
            or  do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   156
                $default= [ '$default', $action ];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   157
                next;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   158
            };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   159
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   160
            push(@reduces,[ $term, $action ]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   161
        }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   162
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   163
            #Dump shifts
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   164
            @shifts
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   165
        and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   166
            $text.="\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   167
            for (sort { $$a[0] cmp $$b[0] } @shifts) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   168
                my($term,$shift)=@$_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   169
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   170
                $text.="\t$term\tshift, and go to state $shift\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   171
            }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   172
        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   173
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   174
            #Dump errors
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   175
            @errors
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   176
        and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   177
            $text.="\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   178
            for my $term (sort { $a cmp $b } @errors) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   179
                $text.="\t$term\terror (nonassociative)\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   180
            }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   181
        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   182
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   183
        #Prepare reduces
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   184
            exists($$self{CONFLICTS}{FORCED}{DETAIL}{$stateno})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   185
        and push(@reduces,@{$$self{CONFLICTS}{FORCED}{DETAIL}{$stateno}{LIST}});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   186
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   187
        @reduces=sort { $$a[0] cmp $$b[0] or $$a[1] <=> $$b[1] } @reduces;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   188
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   189
            defined($default)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   190
        and push(@reduces,$default);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   191
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   192
        #Dump reduces
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   193
            @reduces
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   194
        and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   195
            $text.="\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   196
            for (@reduces) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   197
                my($term,$ruleno)=@$_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   198
                my($discard);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   199
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   200
                    $ruleno < 0
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   201
                and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   202
                    ++$discard;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   203
                    $ruleno = -$ruleno;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   204
                };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   205
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   206
                $text.= "\t$term\t".($discard  ? "[" : "");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   207
                if($ruleno) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   208
                    $text.= "reduce using rule $ruleno ".
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   209
                            "($$grammar{RULES}[$ruleno][0])";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   210
                }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   211
                else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   212
                    $text.='accept';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   213
                }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   214
                $text.=($discard  ? "]" : "")."\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   215
            }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   216
        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   217
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   218
            #Dump gotos
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   219
            exists($$states[$stateno]{GOTOS})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   220
        and    do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   221
                $text.= "\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   222
                for (keys(%{$$states[$stateno]{GOTOS}})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   223
                    $text.= "\t$_\tgo to state $$states[$stateno]{GOTOS}{$_}\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   224
                }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   225
            };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   226
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   227
        $text.="\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   228
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   229
    $text;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   230
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   231
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   232
######################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   233
# Method to get summary about parser #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   234
######################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   235
sub Summary {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   236
    my($self)=shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   237
    my($text);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   238
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   239
	$text=$self->SUPER::Summary();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   240
    $text.="Number of states        : ".
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   241
            scalar(@{$$self{STATES}})."\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   242
    $text;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   243
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   244
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   245
#######################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   246
# Method To Get Infos about conflicts #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   247
#######################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   248
sub Conflicts {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   249
    my($self)=shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   250
    my($states)=$$self{STATES};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   251
    my($conflicts)=$$self{CONFLICTS};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   252
    my($text);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   253
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   254
    for my $stateno ( sort { $a <=> $b } keys(%{$$conflicts{SOLVED}})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   255
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   256
        for (@{$$conflicts{SOLVED}{$stateno}}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   257
            my($ruleno,$token,$how)=@$_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   258
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   259
                $token eq chr(0)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   260
            and $token = '$end';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   261
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   262
            $text.="Conflict in state $stateno between rule ".
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   263
                   "$ruleno and token $token resolved as $how.\n"; 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   264
        }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   265
    };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   266
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   267
    for my $stateno ( sort { $a <=> $b } keys(%{$$conflicts{FORCED}{DETAIL}})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   268
        my($nbsr,$nbrr)=@{$$conflicts{FORCED}{DETAIL}{$stateno}{TOTAL}};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   269
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   270
        $text.="State $stateno contains ";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   271
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   272
            $nbsr
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   273
        and $text.="$nbsr shift/reduce conflict".
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   274
                   ($nbsr > 1 ? "s" : "");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   275
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   276
            $nbrr
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   277
        and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   278
                $nbsr
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   279
            and $text.=" and ";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   280
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   281
            $text.="$nbrr reduce/reduce conflict".
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   282
                   ($nbrr > 1 ? "s" : "");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   283
        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   284
        $text.="\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   285
    };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   286
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   287
    $text;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   288
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   289
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   290
#################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   291
# Method to dump parsing tables #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   292
#################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   293
sub DfaTable {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   294
    my($self)=shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   295
    my($states)=$$self{STATES};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   296
    my($stateno);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   297
    my($text);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   298
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   299
    $text="[\n\t{";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   300
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   301
    $text.=join("\n\t},\n\t{",
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   302
                map {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   303
                    my($state)=$_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   304
                    my($text);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   305
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   306
                    $text="#State ".$stateno++."\n\t\t";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   307
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   308
                       (    not exists($$state{ACTIONS}{''})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   309
                        or  keys(%{$$state{ACTIONS}}) > 1)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   310
                    and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   311
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   312
                        $text.="ACTIONS => {\n\t\t\t";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   313
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   314
                        $text.=join(",\n\t\t\t",
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   315
                                map {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   316
                                    my($term,$action)=($_,$$state{ACTIONS}{$_});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   317
                                    my($text);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   318
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   319
                                    if(substr($term,0,1) eq "'") {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   320
									    $term=~s/([\@\$\"])/\\$1/g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   321
                                        $term=~s/^'|'$/"/g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   322
                                    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   323
                                    else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   324
                                        $term=      $term eq chr(0)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   325
                                                ?   "''" 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   326
                                                :   "'$term'";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   327
                                    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   328
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   329
                                    if(defined($action)) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   330
                                        $action=int($action);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   331
                                    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   332
                                    else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   333
                                        $action='undef';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   334
                                    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   335
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   336
                                    "$term => $action";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   337
                                
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   338
                                } grep { $_ } keys(%{$$state{ACTIONS}}));
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   339
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   340
                        $text.="\n\t\t}";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   341
                    };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   342
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   343
                        exists($$state{ACTIONS}{''})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   344
                    and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   345
                            keys(%{$$state{ACTIONS}}) > 1
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   346
                        and $text.=",\n\t\t";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   347
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   348
                        $text.="DEFAULT => $$state{ACTIONS}{''}";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   349
                    };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   350
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   351
                        exists($$state{GOTOS})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   352
                    and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   353
                        $text.=",\n\t\tGOTOS => {\n\t\t\t";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   354
                        $text.=join(",\n\t\t\t",
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   355
                                map {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   356
                                    my($nterm,$stateno)=($_,$$state{GOTOS}{$_});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   357
                                    my($text);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   358
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   359
                                    "'$nterm' => $stateno";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   360
                                
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   361
                                } keys(%{$$state{GOTOS}}));
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   362
                        $text.="\n\t\t}";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   363
                    };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   364
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   365
                    $text;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   366
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   367
                }@$states);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   368
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   369
    $text.="\n\t}\n]";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   370
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   371
    $text;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   372
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   373
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   374
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   375
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   376
####################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   377
# Method to build Dfa from Grammar #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   378
####################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   379
sub _Compile {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   380
	my($self)=shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   381
	my($grammar,$states);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   382
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   383
	$grammar=$self->{GRAMMAR};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   384
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   385
    $states = _LR0($grammar);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   386
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   387
    $self->{CONFLICTS} = _LALR($grammar,$states);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   388
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   389
    $self->{STATES}=$states;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   390
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   391
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   392
#########################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   393
# LR0 States Generation #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   394
#########################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   395
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   396
###########################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   397
# General digraph routine #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   398
###########################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   399
sub _Digraph {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   400
    my($rel,$F)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   401
    my(%N,@S);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   402
    my($infinity)=(~(1<<31));
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   403
    my($Traverse);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   404
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   405
    $Traverse = sub {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   406
        my($x,$d)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   407
        my($y);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   408
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   409
        push(@S,$x);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   410
        $N{$x}=$d;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   411
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   412
            exists($$rel{$x})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   413
        and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   414
            for $y (keys(%{$$rel{$x}})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   415
                    exists($N{$y})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   416
                or  &$Traverse($y,$d+1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   417
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   418
                    $N{$y} < $N{$x}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   419
                and $N{$x} = $N{$y};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   420
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   421
                $$F{$x}|=$$F{$y};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   422
            }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   423
        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   424
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   425
            $N{$x} == $d
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   426
        and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   427
            for(;;) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   428
                $y=pop(@S);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   429
                $N{$y}=$infinity;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   430
                    $y eq $x
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   431
                and last;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   432
                $$F{$y}=$$F{$x};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   433
            }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   434
        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   435
    };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   436
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   437
    for (keys(%$rel)) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   438
            exists($N{$_})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   439
        or  &$Traverse($_,1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   440
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   441
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   442
#######################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   443
# Generate LR0 states # 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   444
#######################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   445
=for nobody
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   446
Formula used for closures:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   447
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   448
    CLOSE(A) = DCLOSE(A) u U (CLOSE(B) | A close B)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   449
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   450
where:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   451
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   452
    DCLOSE(A) = { [ A -> alpha ] in P }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   453
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   454
    A close B iff [ A -> B gamma ] in P
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   455
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   456
=cut
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   457
sub _SetClosures {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   458
	my($grammar)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   459
    my($rel,$closures);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   460
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   461
    for my $symbol (keys(%{$$grammar{NTERM}})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   462
        $closures->{$symbol}=pack('b'.@{$$grammar{RULES}});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   463
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   464
        for my $ruleno (@{$$grammar{NTERM}{$symbol}}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   465
            my($rhs)=$$grammar{RULES}[$ruleno][1];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   466
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   467
            vec($closures->{$symbol},$ruleno,1)=1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   468
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   469
                @$rhs > 0
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   470
            and exists($$grammar{NTERM}{$$rhs[0]})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   471
            and ++$rel->{$symbol}{$$rhs[0]};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   472
        }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   473
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   474
    _Digraph($rel,$closures);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   475
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   476
	$closures
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   477
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   478
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   479
sub _Closures {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   480
    my($grammar,$core,$closures)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   481
    my($ruleset)=pack('b'.@{$$grammar{RULES}});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   482
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   483
    for (@$core) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   484
        my($ruleno,$pos)=@$_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   485
        my($rhs)=$$grammar{RULES}[$ruleno][1];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   486
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   487
            $pos < @$rhs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   488
        and exists($closures->{$$rhs[$pos]})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   489
        and $ruleset|=$closures->{$$rhs[$pos]};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   490
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   491
    [ @$core, map  { [ $_, 0 ] }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   492
              grep { vec($ruleset,$_,1) }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   493
              0..$#{$$grammar{RULES}} ];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   494
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   495
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   496
sub _Transitions {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   497
    my($grammar,$cores,$closures,$states,$stateno)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   498
    my($core)=$$states[$stateno]{'CORE'};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   499
    my(%transitions);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   500
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   501
    for (@{_Closures($grammar,$core,$closures)}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   502
        my($ruleno,$pos)=@$_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   503
        my($rhs)=$$grammar{RULES}[$ruleno][1];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   504
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   505
            $pos == @$rhs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   506
        and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   507
            push(@{$$states[$stateno]{ACTIONS}{''}},$ruleno);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   508
            next;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   509
        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   510
        push(@{$transitions{$$rhs[$pos]}},[ $ruleno, $pos+1 ]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   511
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   512
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   513
    for (keys(%transitions)) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   514
        my($symbol,$core)=($_,$transitions{$_});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   515
        my($corekey)=join(',',map  { join('.',@$_) }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   516
                              sort {    $$a[0] <=> $$b[0]
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   517
                                    or  $$a[1] <=> $$b[1] }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   518
                              @$core);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   519
        my($tostateno);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   520
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   521
            exists($cores->{$corekey})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   522
        or  do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   523
            push(@$states,{ 'CORE' => $core });
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   524
            $cores->{$corekey}=$#$states;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   525
        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   526
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   527
        $tostateno=$cores->{$corekey};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   528
        push(@{$$states[$tostateno]{FROM}},$stateno);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   529
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   530
			exists($$grammar{TERM}{$_})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   531
		and	do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   532
            $$states[$stateno]{ACTIONS}{$_} = [ $tostateno ];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   533
			next;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   534
		};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   535
        $$states[$stateno]{GOTOS}{$_} = $tostateno;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   536
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   537
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   538
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   539
sub _LR0 {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   540
	my($grammar)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   541
	my($states) = [];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   542
    my($stateno);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   543
    my($closures);  #$closures={ nterm => ruleset,... }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   544
	my($cores)={};  # { "itemlist" => stateno, ... }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   545
					# where "itemlist" has the form:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   546
					# "ruleno.pos,ruleno.pos" ordered by ruleno,pos
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   547
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   548
    $closures = _SetClosures($grammar);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   549
    push(@$states,{ 'CORE' => [ [ 0, 0 ] ] });
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   550
    for($stateno=0;$stateno<@$states;++$stateno) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   551
        _Transitions($grammar,$cores,$closures,$states,$stateno);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   552
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   553
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   554
	$states
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   555
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   556
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   557
#########################################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   558
# Add Lookahead tokens where needed to make LALR states #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   559
#########################################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   560
=for nobody
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   561
    Compute First sets for non-terminal using the following formula:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   562
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   563
    FIRST(A) =      { a in T u { epsilon } | A l a }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   564
                u
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   565
                U   { FIRST(B) | B in V and A l B }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   566
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   567
    where:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   568
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   569
    A l x iff [ A -> X1 X2 .. Xn x alpha ] in P and Xi =>* epsilon, 1 <= i <= n
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   570
=cut
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   571
sub _SetFirst {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   572
	my($grammar,$termlst,$terminx)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   573
    my($rel,$first)=( {}, {} );
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   574
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   575
    for my $symbol (keys(%{$$grammar{NTERM}})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   576
        $first->{$symbol}=pack('b'.@$termlst);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   577
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   578
        RULE:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   579
        for my $ruleno (@{$$grammar{NTERM}{$symbol}}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   580
            my($rhs)=$$grammar{RULES}[$ruleno][1];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   581
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   582
            for (@$rhs) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   583
                    exists($terminx->{$_})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   584
                and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   585
                    vec($first->{$symbol},$terminx->{$_},1)=1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   586
                    next RULE;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   587
                };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   588
                ++$rel->{$symbol}{$_};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   589
                    exists($$grammar{NULLABLE}{$_})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   590
                or  next RULE;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   591
            }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   592
            vec($first->{$symbol},0,1)=1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   593
        }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   594
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   595
    _Digraph($rel,$first);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   596
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   597
	$first
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   598
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   599
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   600
sub _Preds {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   601
    my($states,$stateno,$len)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   602
    my($queue, $preds);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   603
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   604
        $len
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   605
    or  return [ $stateno ];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   606
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   607
    $queue=[ [ $stateno, $len ] ];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   608
    while(@$queue) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   609
        my($pred) = shift(@$queue);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   610
        my($stateno, $len) = @$pred;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   611
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   612
            $len == 1
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   613
        and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   614
			push(@$preds,@{$states->[$stateno]{FROM}});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   615
            next;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   616
        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   617
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   618
        push(@$queue, map { [ $_, $len - 1 ] }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   619
					  @{$states->[$stateno]{FROM}});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   620
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   621
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   622
    # Pass @$preds through a hash to ensure unicity
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   623
    [ keys( %{ +{ map { ($_,1) } @$preds } } ) ];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   624
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   625
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   626
sub _FirstSfx {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   627
    my($grammar,$firstset,$termlst,$terminx,$ruleno,$pos,$key)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   628
    my($first)=pack('b'.@$termlst);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   629
    my($rhs)=$$grammar{RULES}[$ruleno][1];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   630
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   631
    for (;$pos < @$rhs;++$pos) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   632
            exists($terminx->{$$rhs[$pos]})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   633
        and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   634
            vec($first,$terminx->{$$rhs[$pos]},1)=1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   635
            return($first);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   636
        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   637
        $first|=$firstset->{$$rhs[$pos]};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   638
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   639
            vec($first,0,1)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   640
        and vec($first,0,1)=0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   641
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   642
            exists($$grammar{NULLABLE}{$$rhs[$pos]})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   643
        or  return($first);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   644
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   645
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   646
    vec($first,0,1)=1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   647
    $first;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   648
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   649
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   650
=for noboby
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   651
    Compute Follow sets using following formula:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   652
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   653
    FOLLOW(p,A) =       READ(p,A)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   654
                    u
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   655
                    U   { FOLLOW(q,B) | (p,A) include (q,B)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   656
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   657
    where:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   658
 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   659
    READ(p,A) = U { FIRST(beta) | [ A -> alpha A . beta ] in KERNEL(GOTO(p,A))
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   660
                  } - { epsilon }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   661
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   662
    (p,a) include (q,B) iff [ B -> alpha A . beta ] in KERNEL(GOTO(p,A),
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   663
                            epsilon in FIRST(beta) and
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   664
                            q in PRED(p,alpha)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   665
=cut
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   666
sub _ComputeFollows {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   667
	my($grammar,$states,$termlst)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   668
	my($firstset,$terminx);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   669
	my($inconsistent, $rel, $follows, $sfx)= ( {}, {}, {}, {} );
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   670
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   671
    %$terminx= map { ($termlst->[$_],$_) } 0..$#$termlst;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   672
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   673
    $firstset=_SetFirst($grammar,$termlst,$terminx);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   674
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   675
    for my $stateno (0..$#$states) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   676
		my($state)=$$states[$stateno];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   677
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   678
           	exists($$state{ACTIONS}{''})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   679
        and (   @{$$state{ACTIONS}{''}} > 1
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   680
             or keys(%{$$state{ACTIONS}}) > 1 )
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   681
		and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   682
			++$inconsistent->{$stateno};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   683
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   684
			for my $ruleno (@{$$state{ACTIONS}{''}}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   685
				my($lhs,$rhs)=@{$$grammar{RULES}[$ruleno]}[0,1];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   686
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   687
                for my $predno (@{_Preds($states,$stateno,scalar(@$rhs))}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   688
                    ++$rel->{"$stateno.$ruleno"}{"$predno.$lhs"};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   689
                }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   690
			}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   691
		};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   692
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   693
    		exists($$state{GOTOS})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   694
		or	next;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   695
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   696
        for my $symbol (keys(%{$$state{GOTOS}})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   697
            my($tostate)=$$states[$$state{GOTOS}{$symbol}];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   698
            my($goto)="$stateno.$symbol";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   699
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   700
            $follows->{$goto}=pack('b'.@$termlst);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   701
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   702
            for my $item (@{$$tostate{'CORE'}}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   703
                my($ruleno,$pos)=@$item;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   704
				my($key)="$ruleno.$pos";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   705
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   706
					exists($sfx->{$key})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   707
				or	$sfx->{$key} = _FirstSfx($grammar,$firstset,
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   708
											 $termlst,$terminx,
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   709
											 $ruleno,$pos,$key);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   710
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   711
                $follows->{$goto}|=$sfx->{$key};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   712
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   713
                    vec($follows->{$goto},0,1)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   714
                and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   715
                    my($lhs)=$$grammar{RULES}[$ruleno][0];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   716
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   717
                    vec($follows->{$goto},0,1)=0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   718
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   719
                    for my $predno (@{_Preds($states,$stateno,$pos-1)}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   720
                        ++$rel->{$goto}{"$predno.$lhs"};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   721
                    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   722
                };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   723
            }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   724
        }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   725
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   726
    _Digraph($rel,$follows);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   727
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   728
	($follows,$inconsistent)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   729
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   730
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   731
sub _ComputeLA {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   732
	my($grammar,$states)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   733
	my($termlst)= [ '',keys(%{$$grammar{TERM}}) ];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   734
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   735
    my($follows,$inconsistent) = _ComputeFollows($grammar,$states,$termlst);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   736
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   737
    for my $stateno ( keys(%$inconsistent ) ) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   738
        my($state)=$$states[$stateno];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   739
        my($conflict);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   740
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   741
        #NB the sort is VERY important for conflicts resolution order
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   742
        for my $ruleno (sort { $a <=> $b }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   743
                        @{$$state{ACTIONS}{''}}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   744
            for my $term ( map { $termlst->[$_] } grep {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   745
                           vec($follows->{"$stateno.$ruleno"},$_,1) }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   746
                           0..$#$termlst) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   747
                    exists($$state{ACTIONS}{$term})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   748
                and ++$conflict;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   749
                push(@{$$state{ACTIONS}{$term}},-$ruleno);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   750
            }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   751
        }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   752
        delete($$state{ACTIONS}{''});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   753
            $conflict
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   754
        or  delete($inconsistent->{$stateno});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   755
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   756
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   757
	$inconsistent
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   758
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   759
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   760
#############################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   761
# Solve remaining conflicts #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   762
#############################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   763
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   764
sub _SolveConflicts {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   765
	my($grammar,$states,$inconsistent)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   766
    my(%rulesprec,$RulePrec);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   767
    my($conflicts)={    SOLVED  =>  {},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   768
                    	FORCED  =>  {   TOTAL   =>  [ 0, 0 ],
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   769
                                    	DETAIL  =>  {}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   770
                                 	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   771
                };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   772
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   773
    $RulePrec = sub {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   774
        my($ruleno)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   775
        my($rhs,$rprec)=@{$$grammar{RULES}[$ruleno]}[1,2];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   776
        my($lastterm);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   777
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   778
            defined($rprec)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   779
        and return($rprec);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   780
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   781
            exists($rulesprec{$ruleno})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   782
        and return($rulesprec{$ruleno});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   783
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   784
        $lastterm=(grep { exists($$grammar{TERM}{$_}) } @$rhs)[-1];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   785
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   786
            defined($lastterm)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   787
        and ref($$grammar{TERM}{$lastterm})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   788
        and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   789
            $rulesprec{$ruleno}=$$grammar{TERM}{$lastterm}[1];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   790
            return($rulesprec{$ruleno});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   791
        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   792
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   793
        undef;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   794
    };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   795
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   796
    for my $stateno (keys(%$inconsistent)) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   797
        my($state)=$$states[$stateno];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   798
        my($actions)=$$state{ACTIONS};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   799
        my($nbsr,$nbrr);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   800
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   801
        for my $term ( keys(%$actions) ) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   802
            my($act)=$$actions{$term};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   803
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   804
                @$act > 1
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   805
            or  next;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   806
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   807
                $$act[0] > 0
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   808
            and ref($$grammar{TERM}{$term})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   809
            and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   810
                my($assoc,$tprec)=@{$$grammar{TERM}{$term}};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   811
                my($k,$error);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   812
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   813
                for ($k=1;$k<@$act;++$k) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   814
                    my($ruleno)=-$$act[$k];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   815
                    my($rprec)=&$RulePrec($ruleno);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   816
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   817
                        defined($rprec)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   818
                    or  next;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   819
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   820
                        (     $tprec > $rprec
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   821
                         or ( $tprec == $rprec and $assoc eq 'RIGHT'))
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   822
                    and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   823
                        push(@{$$conflicts{SOLVED}{$stateno}},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   824
                             [ $ruleno, $term, 'shift' ]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   825
                        splice(@$act,$k--,1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   826
                        next;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   827
                    };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   828
                        (   $tprec < $rprec
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   829
                         or $assoc eq 'LEFT')
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   830
                    and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   831
                        push(@{$$conflicts{SOLVED}{$stateno}},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   832
                             [ $ruleno, $term, 'reduce' ]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   833
                            $$act[0] > 0
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   834
                        and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   835
                            splice(@$act,0,1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   836
                            --$k;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   837
                        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   838
                        next;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   839
                    };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   840
                    push(@{$$conflicts{SOLVED}{$stateno}},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   841
                         [ $ruleno, $term, 'error' ]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   842
                    splice(@$act,$k--,1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   843
                        $$act[0] > 0
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   844
                    and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   845
                        splice(@$act,0,1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   846
                        ++$error;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   847
                        --$k;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   848
                    };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   849
                }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   850
                    $error
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   851
                and unshift(@$act,undef);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   852
            };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   853
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   854
                @$act > 1
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   855
            and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   856
                $nbrr += @$act - 2;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   857
                ($$act[0] > 0 ? $nbsr : $nbrr) += 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   858
                push(@{$$conflicts{FORCED}{DETAIL}{$stateno}{LIST}},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   859
                    map { [ $term, $_ ] } splice(@$act,1));
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   860
            };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   861
        }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   862
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   863
            $nbsr
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   864
        and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   865
            $$conflicts{FORCED}{TOTAL}[0]+=$nbsr;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   866
            $$conflicts{FORCED}{DETAIL}{$stateno}{TOTAL}[0]+=$nbsr;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   867
        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   868
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   869
            $nbrr
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   870
        and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   871
            $$conflicts{FORCED}{TOTAL}[1]+=$nbrr;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   872
            $$conflicts{FORCED}{DETAIL}{$stateno}{TOTAL}[1]+=$nbrr;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   873
        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   874
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   875
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   876
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   877
	$conflicts
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   878
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   879
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   880
###############################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   881
# Make default reduce actions #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   882
###############################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   883
sub _SetDefaults {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   884
	my($states)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   885
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   886
    for my $state (@$states) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   887
        my($actions)=$$state{ACTIONS};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   888
        my(%reduces,$default,$nodefault);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   889
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   890
            exists($$actions{''})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   891
        and do {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   892
            $$actions{''}[0] = -$$actions{''}[0];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   893
			++$nodefault;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   894
        };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   895
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   896
		#shift error token => no default
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   897
            exists($$actions{error})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   898
        and $$actions{error}[0] > 0
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   899
        and ++$nodefault;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   900
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   901
        for my $term (keys(%$actions)) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   902
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   903
			$$actions{$term}=$$actions{$term}[0];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   904
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   905
                (   not defined($$actions{$term})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   906
                 or $$actions{$term} > 0
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   907
                 or $nodefault)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   908
            and next;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   909
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   910
            push(@{$reduces{$$actions{$term}}},$term);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   911
        }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   912
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   913
			keys(%reduces) > 0
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   914
		or	next;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   915
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   916
        $default=( map { $$_[0] }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   917
                   sort { $$b[1] <=> $$a[1] or $$b[0] <=> $$a[0] }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   918
                   map { [ $_, scalar(@{$reduces{$_}}) ] }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   919
                   keys(%reduces))[0];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   920
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   921
        delete(@$actions{ @{$reduces{$default}} });
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   922
        $$state{ACTIONS}{''}=$default;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   923
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   924
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   925
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   926
sub _LALR {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   927
	my($grammar,$states) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   928
	my($conflicts,$inconsistent);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   929
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   930
    $inconsistent = _ComputeLA($grammar,$states);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   931
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   932
    $conflicts = _SolveConflicts($grammar,$states,$inconsistent);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   933
    _SetDefaults($states);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   934
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   935
	$conflicts
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   936
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   937
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   938
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   939
1;