%{
/*=-=-=-=-=-=-=-=-=-=-  RCCL/RCI Notice of Copyright  -=-=-=-=-=-=-=-=-=-=-=*
 *									    *
 *	Copyright (C) 1987 by Mike Parker. Permission to use, distribute    *
 *	and copy is granted for non-commercial and research purposes only,  *
 *	providing the authors are acknowledged and this copyright notice is *
 *	displayed. The authors provide this software in hope that it will   *
 *	useful, but do not assume responsibility for its use.		    *
 *									    *
 *	This software was developed using the facilities of the McGill 	    *
 *	University Research Center for Intelligent Machines, Montreal,	    *
 *	Canada, for a project undertaken and supported by the RCA Advanced  *
 *	Technology Laboratories, Moorestown, New Jersey.		    *
 *									    *
 *=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
#include <ctype.h>

char lastseg[64] = "\t.text\n";
%}
W	[ \t]*
S	[_$a-zA-Z0-9]*
D	[0-9]*
%%
{W}".data".*\n			{ strcpy(lastseg,yytext);
				  ECHO;
				}
{W}".text".*\n			{ strcpy(lastseg,yytext);
				  ECHO;
				}
{W}".bss".*\n			{ strcpy(lastseg,yytext);
				  ECHO;
				}
{W}".comm"{W}{S}","{D}.*\n	{ char *cp;
				  char *sym;
				  char *val;
				  char *index();
				  cp = index(yytext,'.') + 5;
				  while (isspace(*cp))
				   { cp ++;
				   }
				  sym = cp;
				  cp = index(cp,',');
				  *cp++ = '\0';
				  val = cp;
				  cp = index(cp,'\n');
				  *cp++ = '\0';
				  fprintf(yyout,
"\t.globl %s\n\t.data\n%s: .space %s\n%s",
					sym,sym,val,lastseg);
				}
{W}".lcomm"{W}{S}","{D}.*\n	{ char *cp;
				  char *sym;
				  char *val;
				  char *index();
				  cp = index(yytext,'.') + 6;
				  while (isspace(*cp))
				   { cp ++;
				   }
				  sym = cp;
				  cp = index(cp,',');
				  *cp++ = '\0';
				  val = cp;
				  cp = index(cp,'\n');
				  *cp++ = '\0';
				  fprintf(yyout,"\t.data\n%s: .space %s\n%s",
					sym,val,lastseg);
				}
%%
#define streq(a,b) (strcmp(a,b) == 0)

char **argvec;

main(ac,av)
int ac;
char **av;
{
 argvec = av;

 if ( (ac == 1) ||
      ( (ac == 2) && streq(av[1],"-") ) )
  { yylex();
  }
 else if (ac == 2)
  { yyin = fopen(av[1],"r");
    if (yyin == NULL)
     { fprintf(stderr,"%s: %s: ",argvec[0],av[1]);
       perror(0);
       exit(1);
     }
    yylex();
  }
 else if (ac == 3)
  { if (! streq(av[1],"-"))
     { yyin = fopen(av[1],"r");
       if (yyin == NULL)
	{ fprintf(stderr,"%s: %s: ",argvec[0],av[1]);
	  perror(0);
	  exit(1);
	}
     }
    if (! streq(av[2],"-"))
     { yyout = fopen(av[2],"w");
       if (yyout == NULL)
	{ fprintf(stderr,"%s: %s: ",argvec[0],av[2]);
	  perror(0);
	  exit(1);
	}
     }
    yylex();
  }
 else
  { fprintf(stderr,"Usage: %s [ input-file [ output-file ] ]\n",argvec[0]);
    exit(1);
  }
 exit(0);
}

int yywrap()
{
 return(1);
}
