summaryrefslogtreecommitdiff
path: root/lib/Automake/Parser/parser.pl
blob: b2384fab9cf5aae08359e8727f563f4013d8ca79 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/perl

BEGIN
{
	# Adds the directory containing modules which are copied during
	# make install
	unshift (@INC, '/tmp/parser');
}

use strict;
use Lexer;
use Tree;
use ParserTable;
use File::Basename;

# Stores the relative path of the Makefile.am file with respect to 
# current working directory
my $basedir = File::Basename::dirname($ARGV[0]);

# To enable debug mode, use 1 . Prints the parser stack at each 
# iteration
my $debug = 0;

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

my @stack = (0);
print STDERR "Parser Output\n" if $debug;

my $multiline = 0;
my $curr_tokens;

while ( @stack )
{
	if($stack[-1] == $ParserTable::accept)
	{
		print STDERR "Complete\n";
		printgraph( $stack[-4] );
		recursesubdirs( $basedir, $stack[-4] );
		last;
	}
	while( !@tokens )
	{
		# Calls lexer to get next tokens.
		( $curr_tokens, $multiline ) = lex( $multiline );
		
		# Continue if their is no tokens
		next unless $curr_tokens;
		
		push @tokens, @$curr_tokens;
	}
	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[1]."\n";
	}
	print STDERR @stack, "\n" if $debug;
}