首页 > 代码库 > 词法分析程序

词法分析程序

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100
int a=1;
long g=0;
char nchar[N]
int k=0;
char ch=NULL;
char m[30]={""};
int sym=-1;
main()
{
char cr;
int n;
printf("请输入一串字符串:");
while((cr=getchar())!=‘$‘)
{
nchar[g]=cr;
g++;
}
do
{
n=getsym();
}while(n);
}
int getsym()
{
int i;
char a[2]={""};
char word[][15]={"begin","do","end","if","then","while"};
int wsym[6]={1,2,3,4,5,6};
int ssym[9]={25,26,27,28,0,13,14,15,16};
char b[9]={‘=‘,‘;‘,‘(‘,‘)‘,‘#‘,‘+‘,‘-‘,‘*‘,‘/‘};
if(a==1&&k<g)
{
ch=nchar[k];
j++;
k++;
}
while(ch==‘ ‘||ch==10||ch==9||ch==13)
{
if(k==g)
{
return 0;
}
ch=nchar[k];
k++;
}

技术分享

用文法描述词法规则
•<字母>   A->a|b|c|d|……|z|A|B...|Z
•<数字>  B->0|1|2|….|9
•<整数常数>  S->C|SB

      C->1|2|3|…|9

•<标识符>  S->A|SB|SA|S_
•<关键字>  S->begin|if|then|while|do|end
•<运算符>  S->+|-|*|/|=|#|<|<=|>|>=|:=
•<界符>  S->(|),|;|.

词法分析程序