misc/lemon.c
changeset 710 cbcba3ea6a05
parent 708 d29c193900b0
child 712 40a41fb135f7
equal deleted inserted replaced
709:6fbd0e20b40f 710:cbcba3ea6a05
       
     1 /*
       
     2  * My changes over the original lemon.c from SQLite are encased in
       
     3  *  #if __MOJOSHADER__ blocks.  --ryan.
       
     4  */
       
     5 #define __MOJOSHADER__ 1
       
     6 
     1 /*
     7 /*
     2 ** This file contains all sources (including headers) to the LEMON
     8 ** This file contains all sources (including headers) to the LEMON
     3 ** LALR(1) parser generator.  The sources have been combined into a
     9 ** LALR(1) parser generator.  The sources have been combined into a
     4 ** single file to make it easy to include LEMON in the source tree
    10 ** single file to make it easy to include LEMON in the source tree
     5 ** and Makefile of another program.
    11 ** and Makefile of another program.
  1387   strcpy(*paz, z);
  1393   strcpy(*paz, z);
  1388   for(z=*paz; *z && *z!='='; z++){}
  1394   for(z=*paz; *z && *z!='='; z++){}
  1389   *z = 0;
  1395   *z = 0;
  1390 }
  1396 }
  1391 
  1397 
       
  1398 #if __MOJOSHADER__
       
  1399 static char *user_templatename = NULL;
       
  1400 static void handle_T_option(char *z){
       
  1401   user_templatename = malloc( lemonStrlen(z)+1 );
       
  1402   if( user_templatename==0 ){
       
  1403     fprintf(stderr,"out of memory\n");
       
  1404     exit(1);
       
  1405   }
       
  1406   strcpy(user_templatename, z);
       
  1407 }
       
  1408 #endif
  1392 
  1409 
  1393 /* The main program.  Parse the command line and do it... */
  1410 /* The main program.  Parse the command line and do it... */
  1394 int main(argc,argv)
  1411 int main(argc,argv)
  1395 int argc;
  1412 int argc;
  1396 char **argv;
  1413 char **argv;
  1405   static int nolinenosflag = 0;
  1422   static int nolinenosflag = 0;
  1406   static struct s_options options[] = {
  1423   static struct s_options options[] = {
  1407     {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
  1424     {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
  1408     {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
  1425     {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
  1409     {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
  1426     {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
       
  1427 #if __MOJOSHADER__
       
  1428     {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."},
       
  1429 #endif
  1410     {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
  1430     {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
  1411     {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."},
  1431     {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."},
  1412     {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."},
  1432     {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."},
  1413     {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
  1433     {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
  1414     {OPT_FLAG, "s", (char*)&statistics,
  1434     {OPT_FLAG, "s", (char*)&statistics,
  1420   struct lemon lem;
  1440   struct lemon lem;
  1421 
  1441 
  1422   OptInit(argv,options,stderr);
  1442   OptInit(argv,options,stderr);
  1423   if( version ){
  1443   if( version ){
  1424      printf("Lemon version 1.0\n");
  1444      printf("Lemon version 1.0\n");
  1425      exit(0); 
  1445 #if __MOJOSHADER__
       
  1446      printf(" (...with MojoShader hacks.)\n");
       
  1447 #endif
       
  1448      exit(0);
  1426   }
  1449   }
  1427   if( OptNArgs()!=1 ){
  1450   if( OptNArgs()!=1 ){
  1428     fprintf(stderr,"Exactly one filename argument is required.\n");
  1451     fprintf(stderr,"Exactly one filename argument is required.\n");
  1429     exit(1);
  1452     exit(1);
  1430   }
  1453   }
  3068 struct lemon *lemp;
  3091 struct lemon *lemp;
  3069 {
  3092 {
  3070   static char templatename[] = "lempar.c";
  3093   static char templatename[] = "lempar.c";
  3071   char buf[1000];
  3094   char buf[1000];
  3072   FILE *in;
  3095   FILE *in;
  3073   char *tpltname;
  3096   char *tpltname = 0;
  3074   char *cp;
  3097   char *cp;
       
  3098 
       
  3099   #if __MOJOSHADER__
       
  3100   if (user_templatename != 0) {
       
  3101     if( access(user_templatename,004)==-1 ){
       
  3102       fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
       
  3103         user_templatename);
       
  3104       lemp->errorcnt++;
       
  3105       return 0;
       
  3106     }
       
  3107     in = fopen(user_templatename,"rb");
       
  3108     if( in==0 ){
       
  3109       fprintf(stderr,"Can't open the template file \"%s\".\n",user_templatename);
       
  3110       lemp->errorcnt++;
       
  3111       return 0;
       
  3112     }
       
  3113     return in;
       
  3114   }
       
  3115   #endif
  3075 
  3116 
  3076   cp = strrchr(lemp->filename,'.');
  3117   cp = strrchr(lemp->filename,'.');
  3077   if( cp ){
  3118   if( cp ){
  3078     sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
  3119     sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
  3079   }else{
  3120   }else{
  3583     free(name);
  3624     free(name);
  3584   }
  3625   }
  3585   tplt_xfer(lemp->name,in,out,&lineno);
  3626   tplt_xfer(lemp->name,in,out,&lineno);
  3586 
  3627 
  3587   /* Generate #defines for all tokens */
  3628   /* Generate #defines for all tokens */
       
  3629 #if __MOJOSHADER__
       
  3630   if( 1 ){
       
  3631 #else
  3588   if( mhflag ){
  3632   if( mhflag ){
       
  3633 #endif
  3589     char *prefix;
  3634     char *prefix;
       
  3635 #if !__MOJOSHADER__
  3590     fprintf(out,"#if INTERFACE\n"); lineno++;
  3636     fprintf(out,"#if INTERFACE\n"); lineno++;
       
  3637 #endif
  3591     if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
  3638     if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
  3592     else                    prefix = "";
  3639     else                    prefix = "";
  3593     for(i=1; i<lemp->nterminal; i++){
  3640     for(i=1; i<lemp->nterminal; i++){
  3594       fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
  3641       fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
  3595       lineno++;
  3642       lineno++;
  3596     }
  3643     }
       
  3644 #if !__MOJOSHADER__
  3597     fprintf(out,"#endif\n"); lineno++;
  3645     fprintf(out,"#endif\n"); lineno++;
       
  3646 #endif
  3598   }
  3647   }
  3599   tplt_xfer(lemp->name,in,out,&lineno);
  3648   tplt_xfer(lemp->name,in,out,&lineno);
  3600 
  3649 
  3601   /* Generate the defines */
  3650   /* Generate the defines */
  3602   fprintf(out,"#define YYCODETYPE %s\n",
  3651   fprintf(out,"#define YYCODETYPE %s\n",