词法分析(自己的想法)

#include<stdio.h>
#include<string.h>
#define N 30
char str[N]={NULL},st[N]={NULL};
int t=0,t1=0;
struct node//定义一个队列
{
    char data;
    struct node * next;
};
typedef struct node QueueNode;
struct node2//定义一个链队列
{
    QueueNode *front;
    QueueNode *rear;
};
typedef struct node2 Queue;
void Print(char str[]);
void Print1(char str1[]);
Queue InitQueue()//初始化队列
{
    Queue Q;
    Q.front=(QueueNode *)malloc(sizeof(QueueNode));
    Q.front->next=NULL;
    Q.rear=Q.front;
    return(Q);
}
Queue InserQ(Queue Q,char x)//x进队列
{
    QueueNode *p;
    p=(QueueNode *)malloc(sizeof(QueueNode));
    p->data=x;
    p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;
    return(Q);
}
Queue DeleteQ(Queue Q)//出队列
{
    int i=t++,j;
    QueueNode *p;
    char y=NULL;
    if(Q.front==Q.rear)
    {
        printf("队列为,无法出队列!");//判断队列是否为空
        return(Q);
    }
    p=Q.front->next;
    y=Q.front->next->data;//将队列中的元素赋值给y
    Q.front->next=p->next;
    if(p==Q.rear)
        Q.rear=Q.front;
    if((y>='A'&&y<='Z')||(y>='a'&&y<='z'))//利用ASCII判断y是否是属于字母,并存储在数组str中
        str[i]=y;
    else
    {
        if(str[0]!=NULL)//如果数组str非空,则输出
            Print(str);
        t=0;
        if(y=='+'||y=='-'||y=='*'||y=='/'||y==':'||y=='<'||y=='>')//判断字符是否是运算符
        {
            if(p->next->data=='=')//判断运算是否是由两个字符组成
            {
                printf("(运算符,%c%c)
",y,p->next->data);
                Q.front->next=p->next->next;
                free(p->next);
            }
            else
                printf("(运算符,%c)
",y);
        }
        else if(y=='=')//判断字符是否是运算符
            printf("(运算符,%c)
",y);
        else if(y>=48&&y<=57)//判断字符是否为数字
        {
            j=t1++;
            st[j]=y;//将字符数字赋值给数组st
            if(p->next->data<48||p->next->data>57)//判断队列中下一个字符是否为数字,如果是非字符数字,则输出数组st
                if(st[0]!='o')
                {
                     printf("(常数,%s)
",st);
                     memset(st,0,N);//清空数组str里的所有元素
                     t1=0;
                }
        }
        else if(y==33||y==34||(y>=39&&y<=41)||y==44||y==46||y==58||y==59||(y>=91&&y<=96)||(y>=123&&y<=125))//判断是否为界符
            printf("(界符,%c)
",y);
        else if(y==' ')
            printf("(空格符,%c)
",y);//判断空格符
        else
            printf("(特殊符号,%c)
",y);//其他的当作特殊符号处理
    }
    free(p);
    return Q;
}
int main()
{
    char x,y;
    Queue p,q;
    p=InitQueue();
    printf("请输入你想输入的字母、单词、短语、句子、字符等(必须以非数字结尾,否则程序出错):
");
    while(scanf("%c",&x)==1&&x!='
')//大神的方法,牛
        p=InserQ(p,x);
    q=p;
    while(p.front!=p.rear)
        p=DeleteQ(p);
    if(str[0]!='o')//判断数组str是否为空
        Print(str);
}
void Print(char str[])//调用函数来判断关键字与标识符
{

    int i=0;
    if(strcmp(str,"break")==0||strcmp(str,"case")==0||strcmp(str,"char")==0||strcmp(str,"const")==0||strcmp(str,"continue")==0||strcmp(str,"default")==0||strcmp(str,"do")==0||strcmp(str,"double")==0||strcmp(str,"else")==0||strcmp(str,"enum")==0||strcmp(str,"extern")==0||strcmp(str,"float")==0||strcmp(str,"for")==0||strcmp(str,"goto")==0||strcmp(str,"if")==0||strcmp(str,"int")==0||strcmp(str,"long")==0||strcmp(str,"register")==0||strcmp(str,"return")==0||strcmp(str,"short")==0||strcmp(str,"signed")==0||strcmp(str,"sizeof")==0||strcmp(str,"static")==0||strcmp(str,"struct")==0||strcmp(str,"switch")==0||strcmp(str,"typedef")==0||strcmp(str,"unsigned")==0||strcmp(str,"union")==0||strcmp(str,"void")==0||strcmp(str,"volatile")==0||strcmp(str,"while")==0||strcmp(str,"auto")==0)
        printf("(关键字,%s)
",str);
    else
    {
        if(str[0]=='')
            return;
        printf("(标识符,%s)
",str);
    }
    memset(str,0,N);//清空数组str里的所有元素
}

原文地址:https://www.cnblogs.com/2647409627qq/p/5921139.html