📄 symboltab.cpp
字号:
// Copyright (c) 1996 Federal Highway Administration
//
// This software has been developed for the Federal Highway Administration
// by Viggen Corporation under contract with Oak Ridge National Lab.
//
// Permission to use, copy, and distribute this software for any purpose
// without fee is hereby granted, provided that the above copyright notice
// appears in all copies and that both the copyright and this permission notice
// appear in the supporting documentation.
//
// Permission to modify this software is granted provided that the above
// copyright and this permission notice appears in the modified software.
//
// This software is provided "as is" with no warranty expressed or implied.
//
// For additional information, please go to the Web site www.ntcip.org.
//
/*******************************************************************************
*
* Copyright (c) 1997 Viggen Corporation
*
* Permission to use, copy, and distribute this software for any purpose
* without fee is hereby granted, provided that this copyright notice
* appears in all copies and this permission notice appears in the
* supporting documentation.
*
* Permission to modify this software is granted provided that the above
* copyright and this permission notice appears in the modified software.
*
******************************************************************************/
/*********************************************
*
* symboltab.cpp
*
* 9/29/97 Glenn Pruitt
*********************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lexical.h"
#include "symboltab.h"
//Function definitions
int add_identifier_to_st(struct SYTAB **the_table, char *id_name,
int id_type, int length)
{
struct SYTAB *ptr;
struct SYTAB *curr;
struct SYTAB *last;
//Allocate and fill structure
ptr = (struct SYTAB *)safe_alloc(1,sizeof(struct SYTAB));
ptr->next = NULL;
strcpy(ptr->identifier,id_name);
ptr->type = id_type;
ptr->invar = false;
if(id_type == SY_STRING_TYPE)
{
ptr->labelval = length;
ptr->stringval = (char *)safe_alloc(length,sizeof(char));
}
if(*the_table == NULL)
{
//add to head set index to zero
ptr->index = 0;
*the_table = ptr;
return(ptr->index);
}
else
{
//append to tail
curr = *the_table;
last = NULL;
while(curr)
{
last = curr;
curr = curr->next;
}
last->next = ptr;
ptr->index = last->index + 1;
return(ptr->index);
}
}
void delete_symbol_table(struct SYTAB **the_table)
{
struct SYTAB *tmp;
while(*the_table)
{
tmp = *the_table;
*the_table = (*the_table)->next;
if(tmp->type == SY_STRING_TYPE && tmp->stringval)
safe_free(tmp->stringval);
safe_free(tmp);
}
return;
}
void set_value_in_st(struct SYTAB *the_table, int token_index, long ival,
float fval, char *sval, int lval)
{
struct SYTAB *curr;
curr = the_table;
while(curr && curr->index < token_index)
curr = curr->next;
if(curr)
{
//found correct entry
if(curr->type == SY_LABEL_TYPE)
curr->labelval = lval;
if(curr->type == SY_INT_TYPE)
curr->intval = ival;
if(curr->type == SY_FLOAT_TYPE)
curr->floatval = fval;
if(curr->type == SY_STRING_TYPE)
{
safe_free(curr->stringval);
if(sval != NULL)
{
curr->stringval = (char *)safe_alloc(strlen(sval)+1,sizeof(char));
strcpy(curr->stringval,sval);
curr->labelval = strlen(sval) +1;
}
else
{
curr->stringval = (char *)safe_alloc(1,sizeof(char));
strcpy(curr->stringval,"");
curr->labelval = 1;
}
}
}
else
error_message("set:symbol table value does not exist\n");
}
int get_symbol_type(struct SYTAB *the_table, int token_index)
{
struct SYTAB *curr;
curr = the_table;
while(curr && curr->index < token_index)
curr = curr->next;
if(curr)
{
return(curr->type);
}
else
{
error_message("get type:symbol table value does not exist\n");
return(0);
}
}
long get_symbol_int_value(struct SYTAB *the_table, int token_index)
{
struct SYTAB *curr;
curr = the_table;
while(curr && curr->index < token_index)
curr = curr->next;
if(curr)
{
return(curr->intval);
}
else
{
error_message("get int:symbol table value does not exist\n");
return(-9999);
}
}
float get_symbol_float_value(struct SYTAB *the_table, int token_index)
{
struct SYTAB *curr;
curr = the_table;
while(curr && curr->index < token_index)
curr = curr->next;
if(curr)
{
return(curr->floatval);
}
else
{
error_message("get float:symbol table value does not exist\n");
return(-9999);
}
}
char *get_symbol_string_value(struct SYTAB *the_table, int token_index)
{
struct SYTAB *curr;
curr = the_table;
while(curr && curr->index < token_index)
curr = curr->next;
if(curr)
{
return(curr->stringval);
}
else
{
error_message("get string:symbol table value does not exist\n");
return("");
}
}
int get_symbol_string_size(struct SYTAB *the_table, int token_index)
{
struct SYTAB *curr;
curr = the_table;
while(curr && curr->index < token_index)
curr = curr->next;
if(curr)
{
return(curr->labelval);
}
else
{
error_message("get size:symbol table value does not exist\n");
return(0);
}
}
int get_symbol_offset_value(struct SYTAB *the_table, int token_index)
{
struct SYTAB *curr;
curr = the_table;
while(curr && curr->index < token_index)
curr = curr->next;
if(curr)
{
return(curr->labelval);
}
else
{
error_message("get off:symbol table value does not exist\n");
return(0);
}
}
int symbol_table_lookup(struct SYTAB *the_table, char *identifier)
{
struct SYTAB *curr;
curr = the_table;
while(curr && (strcmp(curr->identifier,identifier) != 0))
curr= curr->next;
if(curr)
{
return(curr->index);
}
else
{
info_message("lookup:symbol table value does not exist\n");
return(-1);
}
}
char *get_identifier(struct SYTAB *the_table, int index, char *identifier)
{
struct SYTAB *curr;
curr = the_table;
while(curr && curr->index < index)
curr = curr->next;
if(curr)
{
strcpy(identifier,curr->identifier);
return(identifier);
}
else
{
error_message("get id:symbol table value does not exist\n");
return("");
}
}
int count_symbols(struct SYTAB *the_table)
{
struct SYTAB *curr;
int i;
curr = the_table;
i = 0;
while(curr)
{
i++;
curr = curr->next;
}
return(i);
}
void realloc_string_size(struct SYTAB *the_table, int index, int size)
{
struct SYTAB *curr;
curr = the_table;
while(curr && curr->index < index)
curr = curr->next;
if(curr)
{
safe_free(curr->stringval);
curr->stringval = (char *)safe_alloc(size,sizeof(char));
curr->labelval = size;
}
else
{
error_message("get id:symbol table value does not exist\n");
return;
}
}
void set_invar(struct SYTAB *the_table, int index)
{
struct SYTAB *curr;
curr = the_table;
while(curr && curr->index < index)
curr = curr->next;
if(curr)
{
curr->invar = true;
}
else
{
error_message("get id:symbol table value does not exist\n");
return;
}
}
int get_invar(struct SYTAB *the_table, int index)
{
struct SYTAB *curr;
curr = the_table;
while(curr && curr->index < index)
curr = curr->next;
if(curr)
{
if(curr->invar == true)
return(1);
else
return(0);
}
else
{
error_message("get id:symbol table value does not exist\n");
return(-1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -