编译原理——词法分析器

来源:互联网 发布:北京二手淘宝市场 编辑:程序博客网 时间:2024/05/02 00:07

这是个词法分析器的雏形.....嘿嘿

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


typedef 
struct Lnode
{
    
char data[20
];
    
struct Lnode *
next;
}
Lnode,*LinkList;
LinkList symtable,consttable;

void CreateList(LinkList &
L)
{
    L
=(LinkList)malloc(sizeof
(Lnode));
    L
->next=
NULL;
}


void ListInsert(LinkList &L,int &number,char *strToken)
{
    LinkList  p,s;
    p
=
L;
    number
=1
;
    
while (p->
next)
    
{
        p
=p->
next;
        number
++
;
       }

    s
=(LinkList)malloc(sizeof(Lnode));
    strcpy(s
->
data,strToken);
    
//s->next=p->next;

    p->next=s;
    s
->next=
NULL;
    p
=
s;

}



char *keyword[32]={"auto","break","case","char","const","contunue","default","do",
                    
"double","else","enum","extern""float","for","goto","if"
,
                    
"int","long","register","return","short","signed","sizeof","static"
,
                   
"struct","switch","typedef","union","unsigned","void","volatile","while"

                  }
;
bool IsDigit(char
 ch)
{//判断是否为字母
    return(ch>='0'&&ch<='9');
}

bool IsLetter(char ch)
{//判断是否为数字
    return((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'));
}


void GetChar(FILE *fp,char &ch)
{//将下一输入字符读到ch中
    ch=fgetc(fp);
}

void GetBC(FILE *fp,char &ch)
{//检查ch中的字符是否为空白,是空白则调用GetChar知道ch不是空白
    while (ch==' '||ch==' '||ch==' ')
        GetChar(fp,ch);
}



int Reserve(char *strToken)
{//对strToken中的字符串查找保留字
    int i;
    
for(i=1;i<=32;i++
)
    
{    
        
if(!strcmp(keyword[i-1
],strToken))
            
return
 i;
    }

    
return 0;
}

void Concat(char *strToken,char ch)
{//将ch中的字符链接到strToken之后
    int i=0;
    
while (strToken[i]!='

原创粉丝点击