使用符号表模拟栈

符号表其实就是一个字典,使用两个数组实现

typedef int Item;

typedef int Key;

static Item *g_cache;//键的索引数组,存在重复键,但是值是一个

static Item *g_Item;//是键-值数组

static int M;//数组的大小

int indexX;//数组的索引

//存入数组

void key(Item aItem,Key aKey)

{

   g_Item[aItem] = aKey;

}


void STinit(int n)

{

   g_cache =malloc(sizeof(&g_cache)*n);

   if (!g_cache)

    {

        printf("create failed!!");

       return;

    }

   g_Item =malloc(sizeof(&g_Item)*n);

   if (!g_Item)

    {

        printf("create failed!!!!!");

       return;

    }

   M = n;

   indexX =0;

   memset(g_Item,0,sizeof(&g_Item)*n);

   memset(g_cache,0,sizeof(&g_cache)*n);

}


int STcount()

{

   returnindexX;

}


void STinsert(Item aItem)

{

   if (indexX >=M)

    {

      g_cache =realloc(g_cache,M/2);

      g_Item =realloc(g_Item,M/2);

       M =M +M/2;

    }

   g_cache[indexX++] = aItem;

}


Item STmove(Key aKey)

{

   int tempItem =0;


   Item* tempCache =malloc(sizeof(&tempCache)*M);

   if (!tempCache)

    {

        printf("create failed!!");

        printf("search result failed!!");

       return tempItem;

    }

   memset(tempCache,0,sizeof(&tempCache)*M);

   int i =0;

   int n =STcount();

   int tempIndex =0;

    

   for (i = --n;i >=0;--i)

    {

        tempCache[tempIndex++] =g_cache[i];

       if (g_Item[g_cache[i]] == aKey)

        {

            tempItem =1;

           break;

        }

    }

    

    

   for (int j = --tempIndex;j >=0;--j,++i)

    {

       if (i <0)

        {

            i =0;

        }

       g_cache[i] = tempCache[j];

    }

    

   if (!tempItem)

    {

        printf("search result failed!!");

    }

   free(tempCache);

    tempCache =NULL;

   return tempItem;

}


Item STsearch(Key aKey)

{

   returnSTmove(aKey);

}


int STdeleteRepeat(Item aItem)

{

   int ret = 0;

   Item* tempCache = malloc(sizeof(&tempCache)*M);

   if (!tempCache)

    {

        printf("create failed!!");

        printf("search result failed!!");

       return ret;

    }

   memset(tempCache, 0, sizeof(&tempCache)*M);

    

   int n = STcount();

   int tempIndex = 0;

   for (int i = --n;i >=0;--i)

    {

        

       if (g_cache[i] == aItem)

        {

            ret +=1;

        }

       else

        {

            tempCache[tempIndex++] =g_cache[i];

        }

    }

   memset(g_cache,0,sizeof(&g_cache)*n);

   for (int i = --tempIndex,j =0;i >= 0;--i,++j)

    {

       g_cache[j] = tempCache[i];

    }

   free(tempCache);

    tempCache =NULL;

    indexX -=ret;

   return ret;

}


void STdelete(Item aItem)

{

    if(STdeleteRepeat(aItem))

    {

        printf("delete successfull:%d\n",aItem);

    }

   else

    {

        printf("delete failed:%d\n",aItem);

    }

}


Item STselect(int aSelect)

{

    

   if (aSelect <0||aSelect >=M)

    {

       return0;

    }

   if (g_Item[g_cache[aSelect]])

    {

        printf("select successfull");

    }

   else

    {

        printf("select failed");

    }

   returng_cache[aSelect];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要编写一个语法分析程序,你需要先确定语法规则并将其转化为文法表示形式。然后,你需要编写代码来执行该文法,并在执行过程中创建语法树。 下面是一个简单的例子,可以帮助你更好地理解如何编写语法分析程序。 假设你想编写一个程序来分析数学表达式,例如: ``` 3 + 4 * 2 - 1 ``` 你的语法规则可能如下所示: ``` <expr> ::= <term> | <expr> + <term> | <expr> - <term> <term> ::= <factor> | <term> * <factor> | <term> / <factor> <factor> ::= <number> | ( <expr> ) <number> ::= <digit> | <digit> <number> <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ``` 这些规则描述了一个数学表达式可以包含哪些元素以及它们可以如何组合在一起。 接下来,你需要将这些规则转换为代码。这涉及到将规则分解为更小的部分,并使用递归函数来执行这些部分。 例如,对于上面的表达式,你可以编写一个函数来处理<expr>规则,如下所示: ```c expr() { term(); while (token == '+' || token == '-') { token = getNextToken(); term(); } } ``` 这个函数首先调用term()函数来处理表达式中的第一个项。然后,它检查下一个符号是否是加号或减号,并在下一个项呈现之前重复此过程。 你需要编写类似的函数来处理<term>和<factor>规则。 在执行这些函数时,你需要跟踪分析过程中的符号。每当你读入一个符号时,你将其压入符号,每当你使用一个符号时,你将其从符号中弹出。 例如,对于上面的表达式,当你读入第一个数字3时,你将其压入符号中。当你读入加号时,你将其压入符号中。当你读入下一个数字4时,你将其压入符号中。当你读入乘号时,你将其压入符号中。当你读入下一个数字2时,你将其压入符号中。 当你执行<term>规则时,你将从符号中弹出数字2和乘号,并将结果压入符号中。当你执行<expr>规则时,你将从符号中弹出数字4和加号,并从符号中弹出数字2和乘号,并将结果压入符号中。最后,当你执行<expr>规则时,你将从符号中弹出数字1和减号,并从符号中弹出结果7。 这些步骤会创建一个语法树,该树描述了输入表达式的结构。你可以使用这颗语法树来模拟分析过程,并输出每一步符号的变化情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值