• 0916 编程实验一 词法分析程序


    # include<stdio.h> # include<stdlib.h> # include<string.h>

    int panduan(char tmp[]) {  char b[6][10] = {"begin", "if", "then", "while", "do", "end", };  int k = 0;

     while(k < 6)     // 临时变量tmp 与 字符串b[k] 进行比较  {   if(strcmp(tmp, b[k]) == 0)   {    printf("<%s, %d> ", tmp, k+1);    return 0;   }   k++;  }  printf("<%s, 10> ", tmp);

     return 0; }

    void cifafenxi(char a[]) {  int i = 0, j = 0;  char tmp[10];  int flag = 0;

     while(a[i] != '')  {   if(a[i] >= 'a' && a[i] <= 'z')  // 判断标识符   {    tmp[j] = a[i];    j++;    while((a[i+1] >= 'a' && a[i+1] <= 'z') || (a[i+1] >= '0' && a[i+1] <= '9'))    {     i++;     tmp[j] = a[i];     j++;    }    tmp[j] = '';    j = panduan(tmp);   }

      if(a[i] >= '0' && a[i] <= '9')  // 判断数字   {    tmp[j] = a[i];        j++;    while(a[i+1] >= '0' && a[i+1] <= '9')    {     i++;     tmp[j] = a[i];     j++;    }    tmp[j] = '';    printf("<%s, 11> ", tmp);    j = 0;   }

      switch(a[i])  // 判断符号   {   case '+':printf("<+, 13> ");break;   case '-':printf("<-, 14> ");break;   case '*':printf("<*, 15> ");break;   case '/':printf("</, 16> ");break;   case ':':    {     if(a[i+1] == '=')      printf("<:=, 18> ");     else      printf("<:, 17> ");    }break;   case '<':    {     if(a[i+1] == '=')      printf("<<=, 21> ");     else if(a[i+1] == '>')      printf("<<>, 22> ");     else      printf("<<, 20> ");    }break;   case '>':    {     if(a[i+1] == '=')      printf("<>=, 24> ");     else      printf("<>, 23> ");    }break;   case '=':printf("<=, 25> ");break;   case ';':printf("<;, 26> ");break;   case '(':printf("<(, 27> ");break;   case ')':printf("<), 28> ");break;   case '#':printf("<#, 0> ");break;   }     i++;  } }

    int main(void) {  char a[300];

     printf("input : ");  gets(a);  cifafenxi(a);

     return 0; }

  • 相关阅读:
    TP5报如下的错误 Indirect modification of overloaded element of thinkpaginatorCollection has no effect【转】
    PHPnumber_format()函数的使用
    蓝桥杯-PREV28-地宫取宝
    HihoCode-1053-居民迁移
    HihoCode-1323-回文字符串
    蓝桥杯-PREV3-带分数
    HDU-6312-Game
    HDU-6438-Buy and Resell
    CF-1117C-Magic Ship
    HDU-2802-F(N)
  • 原文地址:https://www.cnblogs.com/qiuhuimin/p/4897545.html
Copyright © 2020-2023  润新知