词法分析程序

词法分析程序的功能:
用户输入一段源程序,通过词法分析程序进行对这段源程序的相关的分解,通过对
符号的对比,进行相应的符号与种别码的对照,最后进行源程序的分析后答案的输入。

符号与种别码对照表:
单词符号 种别码 单词符号 种别码
begin     1        :          17
if           2        :=        18
then      3        <          20
while      4       <=        22
do         5
end       6         >         23
l(l|d)*   10       >=       24
dd*      11       =         25
+         13         ;        26
-          14        (         27
*          15        )        28
/          16        #         0

用文法描述词法规则:

<字母〉L->a|b|c|...|y|z|

<数字>D->1|2|3|...|8|9|
s->D|sD|s0|
<标识符>
A->1|2|3|...|8|9|
B->a|b|c|...|y|z|
S->AB
<运算符>
A->+|-|*|/|
<分隔符>
A->:|:=|<|<=|<>|>|>=|=|;|(|)


已完成的代码:

#include <stdio.h>
#include <string.h>
char prog[80],token[8],ch;
int syn,p,m,n,sum;
char *rwtab[6]={"begin","if","then","while","do","end"};
void scaner();
main()
{p=0;
printf("please input a string(end with '#'):");
do{
scanf("%c",&ch);
prog[p++]=ch;
}while(ch!='#');
}

原文地址:https://www.cnblogs.com/cgz123/p/5924784.html