#!/usr/bin/perl -w
use strict;
# $Id: maketokens.pl,v 1.6 2005/09/04 14:14:56 joshr Exp $

# this script outputs what's intended to be 'tokens.c'

die "$0: File does not exist: jqp.tab.h. Use Makefile.\n" unless -f "jqp.tab.h";

my @lines = `egrep '^#define' jqp.tab.h`;
chomp(@lines);
my %tokens;
for(@lines) {
	my (undef, $name, $token) = split(/\s+/, $_, 3); 
	$tokens{$token} = $name;
}

# this is totally ugly, but it's debugged and works. 

print '#include "tokens.h"' . "\n";		# prints to file
print "struct tokenname toks[] = {\n";		# prints to file
for (sort {$a <=> $b} keys(%tokens)) {
	print " {$_, \"$tokens{$_}\"},\n";		# prints to file
}
print " {-1, \"\"}\n"; 		# prints to file
print "};\n";		# prints to file
print "
struct tokenname *get_token_names()
{
	return toks;
}
";		# prints to file
