首页 > 代码库 > 词法分析

词法分析

#include<stdio.h>#include<string.h>#include<stdlib.h>#define Max 1000int j=1;long g=0;char nchar[Max]={""}; int k=0;char ch=NULL;char m[30]={""};int sym=-1;main(){    char zfc;    int n;    printf("请输入一串字符串:");    while((zfc=getchar())!=$)    {        nchar[g]=zfc;        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(j==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

<数字> B->1|2...|9

<整数常数> S->C|SB C->1|2|3|...|9

<标识符> F->A|SB|SA|S

<关键字> S->begin|if|then|while|do|end

<运算符> S->+|-|*|%|=|#|<|<=|>|>=|:=

<界符> S->(|)|,|;|.

词法分析