summaryrefslogtreecommitdiff
path: root/lib/Automake/Parser/parser.pl
blob: 0163224dee1ca6dfc17917a5823d5943c084bfc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/perl
use strict;
use Lexer;
use Tree;
use ParserTable;

my $debug = 0;

open ( data, "<input.txt" );

#Stores the list of tokens generated by lexer.
my @tokens; 

while ( <data> )
{
	push @tokens, lex($_);
}
if( $debug )
{
	print "Lexer Output\n";
	foreach my $token ( @tokens )
	{
		print join(" ", @{$token}), "\n";
	}
}

push @tokens, ["end"];
my @stack = (0);
print "Parser Output\n" if $debug;

while ( @stack )
{
	if($stack[-1] == $ParserTable::accept)
	{
		print "Complete\n";
		printgraph( $stack[-4] );
		last;
	}
	my @curr_token = @{ $tokens[0] };	
	if(my $val = $ParserTable::table[ $stack[-1] ]{ $curr_token[0] })
	{
		push @stack, \@curr_token, $val;
		shift @tokens;
	}
	elsif(my $val = $ParserTable::table[ $stack[-1] ]{ reduce })
	{
		my @val1 = @$val;
		my @param;
		for(my $i = 1; $i <= 2 * $val1[0]; $i++)
		{
			if($i%2 == 0)
			{
				$val = pop @stack;
				push @param,$val;
			}
			else
			{
				pop @stack;
			}
		}
		@param = reverse @param;
		push @stack, $val1[1]->( @param );
		push @stack, $ParserTable::table[ $stack[-2] ]{ $stack[-1]->{ name }};
	}
	else
	{
		die "Unexpected Token ". @curr_token."\n";
	}
	print @stack, "\n" if $debug;
}