zoukankan      html  css  js  c++  java
  • 词法分析

    # 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>\n", tmp, k+1);
    return 0;
    }
    k++;
    }
    printf("<%s, 10>\n", tmp);

    return 0;
    }

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

    while(a[i] != '\0')
    {
    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] = '\0';
    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] = '\0';
    printf("<%s, 11>\n", tmp);
    j = 0;
    }

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

    i++;
    }
    }

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

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

    return 0;
    }

  • 相关阅读:
    【JZOJ6223】【20190617】互膜
    【JZOJ6225】【20190618】计数
    【JZOJ6226】【20190618】纳什均衡
    【JZOJ6210】【20190612】wsm
    【学习笔记】析合树
    【JZOJ6206】【20190610】二分图边染色
    【loj3123】【CTS2019】重复
    【loj3120】【CTS2019】珍珠
    【loj3119】【CTS2019】随机立方体
    CTS&&APIO2019爆零记
  • 原文地址:https://www.cnblogs.com/darkhate/p/5942794.html
Copyright © 2011-2022 走看看