v2.0

#include <stdio.h>
#include <string.h>
#include <ctype.h>
typedef struct node
{
  char list[81];
  int tempi;
}sentance;//分离出一个个关键字所用临时结构
void printch(char stack[][10],int top);
int main()
{
  sentance one,two,three;
  char whole[800];//存放用户输入数据
  char word=NULL;
  char stack[800][10];//存放关键字以及变量的栈
  int top=-1;
  int i=1;
  one.tempi=two.tempi=three.tempi=0;
  printf("Please input the code and end with the '#':");
  while(word!='#')
  {
    i=1;
    gets(whole);
    word=whole[0];//将字符串第一个字符赋给word
    while(word!='#')
      {
        while(ispunct(word)&&(word!='#'))
        {
          two.list[two.tempi]=word;
          two.tempi++;
          word=whole[i];//i是以1开始,所以whole[i]是下一个字符
          i++;
          if(two.list[two.tempi-1]!=':'&&two.list[two.tempi-1]!='<'&&two.list[two.tempi-1]!='>')//若果不是这三个符号,则直接入栈
          {
            two.list[two.tempi]='';
            top++;
            strcpy(stack[top],two.list);
            two.tempi=0;
          }
          else if(word=='='&&two.list[two.tempi-1]==':'||word=='='&&two.list[two.tempi-1]=='<'||word=='='&&two.list[two.tempi-      1]=='>'||word=='>'&&two.list[two.tempi-1]=='<')//比较是否为双标点情况
          {
            two.list[two.tempi]=word;
            two.list[two.tempi+1]='';
            top++;
            strcpy(stack[top],two.list);
            two.tempi=0;
            word=whole[i];//将下一个字符赋给word
            i++;//i指向下一个字符
          }
          else//当下一个字符没有组成双标点,则入栈
          {
            two.list[two.tempi]='';
            top++;
            strcpy(stack[top],two.list);
            two.tempi=0;
          }
        }
        while(isalpha(word)&&(word!='#'))
        {
          one.list[one.tempi]=word;
          one.tempi++;
          word=whole[i];
          i++;
          if(!(isalpha(word)))
          {
            one.list[one.tempi]='';
            top++;
            strcpy(stack[top],one.list);
            one.tempi=0;
          }
        }
        while(isdigit(word))
        {
          three.list[three.tempi]=word;
          three.tempi++;
          word=whole[i];
          i++;
          if(!(isdigit(word)))
          {
            three.list[three.tempi]='';
            top++;
            strcpy(stack[top],three.list);
            three.tempi=0;
          }
        }
        if(word==' ')//忽略空格
        {
          word=whole[i];
          i++;
        }
        if(word=='')//遇到结束符跳出循环
          break;
      }
  }
  if(top!=-1)//如果栈里无元素,则函数结束
    printch(stack,top);
  return 0;
}
void printch(char stack[][10],int top)
{
  char clist[][6]={"begin","if","then","while","do","end","","","","","","","+","-","*","/",":",":=","","<","<=","<>",">",">=","=",";","(",")","#"};
  int i,k;
  for(k=0;k<=top;k++)
  {
    if(strlen(stack[k])!=0)
    {
      if(stack[k][0]>='0'&&stack[k][0]<='9')//数字的输出
      {
        printf("<11,"%s"> ",stack[k]);
        continue;//如果是数字则continue循环
      }
      for(i=0;i<=28;i++)
      {
        if(strcmp(clist[i],stack[k])==0)
        {
          printf("<%d,"%s"> ",i+1,stack[k]);//关键字的输出
          break;
        }
        if(i==28)//找不到相同的字符串,则作为一个变量输出
        {
          printf("<10,"%s"> ",stack[k]);
        }
      }
    }
  }
}

原文地址:https://www.cnblogs.com/peivxuan/p/4835372.html