Beispiel #1
0
static unsigned char *find_next_val(unsigned char *fpage, u_int16_t*value)
{
  unsigned char *cur_ptr;
  unsigned char token[MAX_TOKEN];
  cur_ptr = find_next_token(fpage, token);
  if(cur_ptr)
    *value = (u_int16_t)convert_str_to_number(token);

  return cur_ptr;
}
Beispiel #2
0
static unsigned char *find_next_address(unsigned char *fpage, unsigned short *address)
{
  unsigned char *cur_ptr;
  unsigned char token[MAX_TOKEN];
//  memset(token, NULL, sizeof(token));
  memset(token, 0, sizeof(token));
  cur_ptr = find_next_token(fpage, token);
  if(cur_ptr)
    *address = (unsigned short)convert_str_to_number(token);
  return cur_ptr;
}
Beispiel #3
0
static unsigned char *find_param_version(unsigned char *fpage, unsigned short *version)
{
  unsigned char *cur_ptr;
  unsigned char token[MAX_TOKEN];
  if((cur_ptr = (unsigned char *)strstr((char *)fpage,VERSTR)))
  {
    cur_ptr+= (sizeof(VERSTR)-1);
    cur_ptr = find_next_token(cur_ptr,token);
    if(cur_ptr)
      *version = (unsigned short)convert_str_to_number(token);
  }
  return cur_ptr;
}
Beispiel #4
0
unsigned char * camsensor_parser_find_data
(
    unsigned char *fpage, 
    u_int16_t *value
)
{
    unsigned char *cur_ptr;
    unsigned char token[MAX_TOKEN];
    cur_ptr = find_next_token(fpage, token);
    if (cur_ptr)
    {
        *value = (u_int16_t)convert_str_to_number(token);
    }
    return cur_ptr;
}
Beispiel #5
0
static void monitor_completions(const char *string_so_far, linenoiseCompletions *lc)
{
    int start_of_token, token_len;
    char *help_commands[] = {"help", "?"};
    const linenoiseCompletions help_lc = {
         sizeof(help_commands)/sizeof(*help_commands),
         help_commands
    };

    find_next_token(string_so_far, 0, &start_of_token, &token_len);
    if (!string_so_far[start_of_token + token_len]) {
         fill_completions(string_so_far, start_of_token, token_len, &command_lc, lc);
         return;
    }
    if (is_token_in(string_so_far + start_of_token, token_len, &help_lc)) {
        find_next_token(string_so_far, start_of_token + token_len, &start_of_token, &token_len);
        if (!string_so_far[start_of_token + token_len]){
             fill_completions(string_so_far, start_of_token, token_len, &command_lc, lc);
             return;
        }
    }
    if (is_token_in(string_so_far + start_of_token, token_len, &need_filename_lc)) {
        int start_of_path;
        DIR* dir;
        struct dirent *direntry;
        struct linenoiseCompletions files_lc = {0, NULL};
        int i;

        for (start_of_token += token_len; string_so_far[start_of_token] && isspace(string_so_far[start_of_token]); start_of_token++);
        if (string_so_far[start_of_token] != '"') {
            char *string_to_append = concat_strings(string_so_far, start_of_token, "\"");
            linenoiseAddCompletion(lc, string_to_append);
            free(string_to_append);
            return;
        }
        for (start_of_path = ++start_of_token, token_len = 0; string_so_far[start_of_token + token_len]; token_len++) {
            if(string_so_far[start_of_token + token_len] == '"'
            && string_so_far[start_of_token + token_len - 1] != '\\') {
                return;
            }
            if(string_so_far[start_of_token + token_len] == '/') {
                start_of_token += token_len + 1;
                token_len = -1;
            }
        }
        if (start_of_token == start_of_path) {
            dir = opendir(".");
        } else {
            char *path = concat_strings(string_so_far + start_of_path, start_of_token - start_of_path, "");
            dir = opendir(path);
            free(path);
        }
        if (dir) {
            for (direntry = readdir(dir); direntry; direntry = readdir(dir)) {
                if (strcmp(direntry->d_name, ".") && strcmp(direntry->d_name, "..")) {
                    char *entryname = lib_msprintf("%s%s", direntry->d_name, is_dir(direntry) ? "/" : "\"");
                    linenoiseAddCompletion(&files_lc, entryname);
                    lib_free(entryname);
                }
            }
            fill_completions(string_so_far, start_of_token, token_len, &files_lc, lc);
            for(i = 0; i < files_lc.len; i++) {
                free(files_lc.cvec[i]);
            }
            closedir(dir);
            return;
        }
    }
}
Beispiel #6
0
/* splits up a list of file names and feeds it to eval_include_dep_file,
   employing threads to try speed up the file reading. */
void
eval_include_dep (const char *names, struct floc *f, enum incdep_op op)
{
  struct incdep *head = 0;
  struct incdep *tail = 0;
  struct incdep *cur;
  const char *names_iterator = names;
  const char *name;
  unsigned int name_len;

  /* loop through NAMES, creating a todo list out of them. */

  while ((name = find_next_token (&names_iterator, &name_len)) != 0)
    {
       cur = xmalloc (sizeof (*cur) + name_len); /* not incdep_xmalloc here */
       cur->file_base = cur->file_end = NULL;
       memcpy (cur->name, name, name_len);
       cur->name[name_len] = '\0';
       cur->worker_tid = -1;
#ifdef PARSE_IN_WORKER
       cur->err_line_no = 0;
       cur->err_msg = NULL;
       cur->recorded_variables_in_set_head = NULL;
       cur->recorded_variables_in_set_tail = NULL;
       cur->recorded_variable_defs_head = NULL;
       cur->recorded_variable_defs_tail = NULL;
       cur->recorded_file_head = NULL;
       cur->recorded_file_tail = NULL;
#endif

       cur->next = NULL;
       if (tail)
         tail->next = cur;
       else
         head = cur;
       tail = cur;
    }

#ifdef ELECTRIC_HEAP
  if (1)
#else
  if (op == incdep_read_it)
#endif
    {
      /* work our way thru the files directly */

      cur = head;
      while (cur)
        {
          struct incdep *next = cur->next;
          incdep_read_file (cur, f);
          eval_include_dep_file (cur, f);
          incdep_freeit (cur);
          cur = next;
        }
    }
  else
    {
      /* initialize the worker threads and related stuff the first time around. */

      if (!incdep_initialized)
        incdep_init (f);

      /* queue the files and notify the worker threads. */

      incdep_lock ();

      if (incdep_tail_todo)
        incdep_tail_todo->next = head;
      else
        incdep_head_todo = head;
      incdep_tail_todo = tail;

      incdep_signal_todo ();
      incdep_unlock ();

      /* flush the todo queue if we're requested to do so. */

      if (op == incdep_flush)
        incdep_flush_it (f);
    }
}
Beispiel #7
0
static const char *
library_search (const char *lib, FILE_TIMESTAMP *mtime_ptr)
{
  static char *dirs[] =
    {
#ifndef _AMIGA
      "/lib",
      "/usr/lib",
#endif
#if defined(WINDOWS32) && !defined(LIBDIR)
/*
 * This is completely up to the user at product install time. Just define
 * a placeholder.
 */
#define LIBDIR "."
#endif
      LIBDIR,			/* Defined by configuration.  */
      0
    };

  const char *file = 0;
  char *libpatterns;
  FILE_TIMESTAMP mtime;

  /* Loop variables for the libpatterns value.  */
  char *p;
  const char *p2;
  unsigned int len;
  unsigned int liblen;

  /* Information about the earliest (in the vpath sequence) match.  */
  unsigned int best_vpath = 0, best_path = 0;

  char **dp;

  libpatterns = xstrdup (variable_expand ("$(.LIBPATTERNS)"));

  /* Skip the '-l'.  */
  lib += 2;
  liblen = strlen (lib);

  /* Loop through all the patterns in .LIBPATTERNS, and search on each one.
     To implement the linker-compatible behavior we have to search through
     all entries in .LIBPATTERNS and choose the "earliest" one.  */
  p2 = libpatterns;
  while ((p = find_next_token (&p2, &len)) != 0)
    {
      static char *buf = NULL;
      static unsigned int buflen = 0;
      static int libdir_maxlen = -1;
      static unsigned int std_dirs = 0;
      char *libbuf = variable_expand ("");

      /* Expand the pattern using LIB as a replacement.  */
      {
	char c = p[len];
	char *p3, *p4;

	p[len] = '\0';
	p3 = find_percent (p);
	if (!p3)
	  {
	    /* Give a warning if there is no pattern.  */
	    error (NILF, _(".LIBPATTERNS element '%s' is not a pattern"), p);
            p[len] = c;
	    continue;
	  }
	p4 = variable_buffer_output (libbuf, p, p3-p);
	p4 = variable_buffer_output (p4, lib, liblen);
	p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
	p[len] = c;
      }

      /* Look first for 'libNAME.a' in the current directory.  */
      mtime = name_mtime (libbuf);
      if (mtime != NONEXISTENT_MTIME)
	{
	  if (mtime_ptr != 0)
	    *mtime_ptr = mtime;
	  file = strcache_add (libbuf);
          /* This by definition will have the best index, so stop now.  */
          break;
	}

      /* Now try VPATH search on that.  */

      {
        unsigned int vpath_index, path_index;
        const char* f = vpath_search (libbuf, mtime_ptr ? &mtime : NULL,
                                      &vpath_index, &path_index);
        if (f)
          {
            /* If we have a better match, record it.  */
            if (file == 0 ||
                vpath_index < best_vpath ||
                (vpath_index == best_vpath && path_index < best_path))
              {
                file = f;
                best_vpath = vpath_index;
                best_path = path_index;

                if (mtime_ptr != 0)
                  *mtime_ptr = mtime;
              }
          }
      }

      /* Now try the standard set of directories.  */

      if (!buflen)
        {
          for (dp = dirs; *dp != 0; ++dp)
            {
              int l = strlen (*dp);
              if (l > libdir_maxlen)
                libdir_maxlen = l;
              std_dirs++;
            }
          buflen = strlen (libbuf);
          buf = xmalloc(libdir_maxlen + buflen + 2);
        }
      else if (buflen < strlen (libbuf))
        {
          buflen = strlen (libbuf);
          buf = xrealloc (buf, libdir_maxlen + buflen + 2);
        }

      {
        /* Use the last std_dirs index for standard directories. This
           was it will always be greater than the VPATH index.  */
        unsigned int vpath_index = ~((unsigned int)0) - std_dirs;

        for (dp = dirs; *dp != 0; ++dp)
	  {
            sprintf (buf, "%s/%s", *dp, libbuf);
            mtime = name_mtime (buf);
            if (mtime != NONEXISTENT_MTIME)
	      {
                if (file == 0 || vpath_index < best_vpath)
                  {
                    file = strcache_add (buf);
                    best_vpath = vpath_index;

                    if (mtime_ptr != 0)
                      *mtime_ptr = mtime;
                  }
              }

            vpath_index++;
          }
      }

    }

  free (libpatterns);
  return file;
}
Beispiel #8
0
void next_token ()
{
   token = find_next_token();
}
Beispiel #9
0
Tokens find_next_token ()
{
   char c, cs, fin;

   if (tounget)
   {
      printf("ungeted\n");
      tounget = 0;
      return unget;
   }

   next_char();
   c = lire_car_min ();

   if (isdigit (c))
   {
      valeur = atoii(c);
#if DEBUG
      printf("valeur: %d -> %d\n",valeur, atoii(c));
#endif
      c = lire_car_min ();
      while (isdigit (c))
      {
#if DEBUG
         printf("valeur: %d\n",valeur);
#endif
         valeur = valeur*10 + atoii (c);
         c = lire_car_min ();
      }

      /* Remise dans le flux du dernier caractère non numérique */
      insere_car (&fichier_pascal, c);
#if DEBUG
      printf("valeur: %d\n",valeur);
#endif
      return NUM_TOKEN;
   }

   switch (c)
   {
      case CAR_UNDER:
         return identifiant (c);
         /*return ID_TOKEN;*/
         break;

      case CAR_PLUS:
         return PLUS_TOKEN;
         break;

      case CAR_MOINS:
         return MOINS_TOKEN;
         break;

      case CAR_MUL:
         return MUL_TOKEN;
         break;

      case CAR_DIV:
         return DIV_TOKEN;
         break;

      case CAR_EGAL:
         return EGAL_TOKEN;
         break;

      case CAR_INF:
         c = lire_car_min ();
         switch (c)
         {
            case CAR_EGAL:
               return INF_EGAL_TOKEN;
               break;
            case CAR_SUP:
               return DIFF_TOKEN;
               break;
            default:
               insere_car (&fichier_pascal, c);
               return INF_TOKEN;
         }
         break;

      case CAR_SUP:
         c = lire_car_min ();
         switch (c)
         {
            case CAR_EGAL:
               return SUP_EGAL_TOKEN;
               break;
            default:
               insere_car (&fichier_pascal, c);
               return SUP_TOKEN;
         }
         break;

         /* Suppression des commentaires {...} */
      case CAR_ACC_OUV:
         while (lire_car_min () != CAR_ACC_FER);
         return find_next_token();
         break;

      case CAR_PAR_OUV:
         c = lire_car_min ();
         /* Suppression des commentaires (*...*) */
         if (c == CAR_MUL)
         {
            fin = 0;
            while (!fin)
            {
               c = lire_car_min ();
               if (c == CAR_MUL)
               {
                  cs = lire_car_min ();
                  if (cs == CAR_PAR_FER)
                     fin = 1;
               }
            }
            return find_next_token();
         }
         insere_car (&fichier_pascal, c);
         return PAR_OUV_TOKEN;
         break;

      case CAR_PAR_FER:
         return PAR_FER_TOKEN;
         break;

      case CAR_CRO_OUV:
         return CRO_OUV_TOKEN;
         break;

      case CAR_CRO_FER:
         return CRO_FER_TOKEN;
         break;

      case CAR_2PT:
         c = lire_car_min ();
         switch (c)
         {
            case CAR_EGAL:
               return AFFEC_TOKEN;
               break;
            default:
               insere_car (&fichier_pascal, c);
               return DEUX_PT_TOKEN;
         }
         break;

      case CAR_PT_VIRG:
         return PT_VIRG_TOKEN;
         break;

      case CAR_VIRG:
         return VIRG_TOKEN;
         break;

      case CAR_POINT:
         c = lire_car_min ();
         switch (c)
         {
            case CAR_POINT:
               return PT_PT_TOKEN;
               break;
            default:
               insere_car (&fichier_pascal, c);
               return POINT_TOKEN;
         }
         break;

      case CAR_QUOTE:
         return QUOTE_TOKEN;
         break;

      default:
         return reco_identifiant(c);
         break;
   }
   return TOKEN_INCONNU;
}
Beispiel #10
0
void main(int argc, char *argv[]){

			FILE *fp;
			FILE *fp2;
			buffer B;
			FILE *filename,*filename2;
			ast a;
			FPTR fptr;
			SPTR sptr;
		//	printf("Hello inside main\n");
			sptr=symbol_table_create(20,20);
			fptr=global(sptr);
	//		printf("DOne\n");
				char testcopy[]="testcopy.txt";
				parse_Tree PT;
				int T[52][49];
				
				filename=fopen(argv[2],"w");
			//	filename2=fopen(argv[3],"w");
				fp2=fopen("testcopy.txt","r");
				fp=fopen(argv[1],"r");			
			int num;
		printf("Enter the desired Option !!\n\n");
		do{
		
		printf("  1 : Select Option 1 for Lexical Analysis \n  2 : Select Option 2 for Parse Tree \n  3 : Select Option 3 for Abstract Syntax Tree \n  4 : Select Option 4 for Symbol Table \n  5 : Select Option 5 for Semantic Analysis \n  6 : Select Option 6 for Code Generation\n");
		scanf("%d",&num);
		switch(num){
			case 1:
		
				
				//line_number=1;
				//int k=BUFFERSIZE;
				buf=0;
				ahead=0;
				token_info TI;
				printf("***************************LEXICAL ANALYSIS***************************\n\n");
				printf("   LEXEME_NAME    |     TOKEN_NAME  |     LINE_NUMBER |\n\n");
				fprintf(filename,"***************************LEXICAL ANALYSIS***************************\n\n");
				fprintf(filename,"   LEXEME_NAME    |     TOKEN_NAME  |     LINE_NUMBER |\n\n");
				TI=find_next_token(fp,B);
				while(strcmp(TI.lexeme_name,"$")!=0){
		
				printf("| %15s | %15s |    %d\n",TI.lexeme_name,TI.token_name,TI.line);
				fprintf(filename,"| %15s | %15s |    %d\n",TI.lexeme_name,TI.token_name,TI.line);
				TI=find_next_token(fp,B);
				}
				TI=find_next_token(fp,B);
				printf("| %15s | %15s |    %d\n",TI.lexeme_name,TI.token_name,TI.line);
				fprintf(filename,"| %15s | %15s |    %d\n",TI.lexeme_name,TI.token_name,TI.line);
				printf("\n\n\n");
				
				
	
			break;
	
	
			case 2:
						
				create_parsing_table(T);
				PT=parse_input(argv[1],T);
			//	printf("end of parsing\n");
				PT->temp=PT->head;
			//	printf("end of parsing2222\n");
				
				printf("*************************************Printing the Parse Tree*************************************\n\n\n");
	            printf("|           LEXEME_NAME|         TOKEN_NAME   |         PARENT_NODE |       IS_LEAF_NODE |      LINE NUMBER  |\n\n\n");
	           fprintf(filename,"**********************************Printing the Parse Tree**********************************\n\n\n");
	           fprintf(filename,"|           LEXEME_NAME |         TOKEN_NAME |         PARENT_NODE |       IS_LEAF_NODE |      LINE NUMBER |\n\n\n");
				parse_tree_print(PT,filename);
			return;
			 case 3: 
			 
			    create_parsing_table(T);
				PT=parse_input(argv[1],T);
			//	printf("end of parsing\n");
				PT->temp=PT->head;
				create_ast(PT,a);
				printf("*************************************Printing the AST Tree *************************************\n\n\n");
	printf(" |LEXEME_NAME         |TOKEN_NAME          |PARENT_NODE         |IS_LEAF_NODE        |LINE NUMBER\n\n\n");
	fprintf(filename,"*************************************Printing the AST Tree*************************************\n\n\n");
	fprintf(filename," |LEXEME_NAME         |TOKEN_NAME          |PARENT_NODE         |IS_LEAF_NODE        |LINE NUMBER\n\n\n");
				
				ast_print(PT,filename);
				return;
			case 4:
			
			//printf("Hello inside case 4\n");
				create_parsing_table(T);
				PT=parse_input(argv[1],T);
			//	printf("end of parsing\n");
				PT->temp=PT->head;
				create_ast(PT,a);
				//ast_print(PT,filename2);
				PT->temp=PT->head;
				
				add_functions(sptr,fptr,PT);
				
				printf("\n\n***********************************************Printing the Symbol Table*************************************************\n\n\n");
	printf("     LEXEME_NAME     |         TOKEN_NAME   |        SCOPE    |     DATA_TYPE   |     SIZE   |    OFFSET  |   LINE NUMBER\n\n\n");
	fprintf(filename,"*************************************Printing the Symbol Table***************************************\n\n\n");
	fprintf(filename,"     LEXEME_NAME     |         TOKEN_NAME  |           SCOPE    |     DATA_TYPE   |     SIZE   |    OFFSET  |   LINE NUMBER\n\n\n");
				symbol_table_print(sptr,filename);
				
				printf("\n\n");
				fprintf(filename,"\n\n");
				return;
			
			case 5: 
			//printf("Hello inside case 4\n");
				create_parsing_table(T);
				PT=parse_input(argv[1],T);
			//	printf("end of parsing\n");
				PT->temp=PT->head;
				create_ast(PT,a);
				//ast_print(PT,filename2);
				PT->temp=PT->head;
				
				add_functions(sptr,fptr,PT);
				PT->temp=PT->head;
				check_semantic(PT,fptr,sptr);
				return;
				
			case 6 :
			
				create_parsing_table(T);
				PT=parse_input(argv[1],T);
			//	printf("end of parsing\n");
				PT->temp=PT->head;
				create_ast(PT,a);
				//ast_print(PT,filename2);
				PT->temp=PT->head;
				
				add_functions(sptr,fptr,PT);
				PT->temp=PT->head;
				assembly_code(PT,sptr,filename);
				return;
			
			
			
			
			
			default:
				printf("\nWrong choice!!!Enter Between 1 to 4");
		}
		
	} while(1);
	
	fclose(fp);
		
	fclose(filename);
//	fclose(filename2);
	fclose(fp2);
	
	
	return 0;
}