这学期修了严厉的Mr.J的编译原理,不定期更新笔记!
欢迎指正!
标识符:字母打头,后接任意字母或数字
保留字:标识符的子集,包括:if,else,for,while,do, int,write,read,
无符号整数:由数字组成,但最高位不能为0,允许一位的0,
分界符:(、)、;、{、}
运算符:+、-、*、/、=、<、>、>=、<=、!=、==
注释符:/* */
标识符: ( a|b|……|z|A|B……|Z )( 0|1|……|9| a|b|……|z|A|B……|Z )*
保留字: 标识符的子集
无符号整数: ( (1……|9 )( 0|1|……|9)* )|0
分界符: ( | ) | ; | { | }
运算符: + | - | * | / | = | < | > | >= | <= | != | ==
/* (other)* */
将NFA确定化之后得到的DFA
依据DFA编写词法分析程序
/*This a test program.*/
int abc;
int 123;
int A$@;
int i;
int n;
int b,c;
int 2a;
int a2;
read n;
n = 012345;
for (i=1;i<=n; i= i+1)
{
abc=abc+i;
}
if (!n) b = b+c;
/*The loop ended
write abc;
}
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define sum 8
FILE *fin, *fout;
char Dword[10] = "!><=";
char Infile[300], Outfile[300];
char Sword[50] = "+-*(){};";
char *keyword[sum] = { "if", "else", "for", "while", "do", "int", "read", "write" };
int test()
{
char ch, Buff[40];
int flag = 0, n, line = 1;
int m = 0;
printf("Name of Pro:");
scanf("%s", Infile);
printf("Name of Outfile:");
scanf("%s", Outfile);
if ((fin = fopen(Infile, "r")) == NULL)
{
printf("\nPro file open error!\n");
return (1);
}
if ((fout = fopen(Outfile, "w")) == NULL)
{
printf("\nOutfile open error!\n");
return (2);
}
ch = getc(fin);
while (ch != EOF)
{
m = 0;
memset(Buff, 0, 40);
while (ch == ‘ ‘ || ch == ‘\n‘ || ch == ‘\t‘)
{
if (ch == ‘\n‘)
{
line++;
}
ch = getc(fin);
}
if (isalpha(ch))
{
while (isalpha(ch) || isdigit(ch))
{
Buff[m] = ch;
m++;
ch = getc(fin);
}
n = 0;
while ((n < sum) && strcmp(Buff, keyword[n]))
{
n++;
}
if (n < sum)
{
fprintf(fout, "%s\t%s\n", "ID", Buff);
}
else
{
fprintf(fout, "%s\t%s\n", Buff, Buff);
}
}
else if (isdigit(ch))
{
if (ch == ‘0‘)
{
Buff[0] = ‘0‘;
Buff[1] = ‘\0‘;
fprintf(fout, "%s\t%s\n", "NUM", Buff);
ch = getc(fin);
}
else
{
while (isdigit(ch))
{
Buff[m] = ch;
m++;
ch = getc(fin);
}
Buff[m] = ‘\0‘;
fprintf(fout, "%s\t%s\n", "NUM", Buff);
}
}
else if (strchr(Sword, ch) > 0)
{
Buff[0] = ch;
Buff[1] = ‘\0‘;
ch = getc(fin);
fprintf(fout, "%s\t%s\n", Buff, Buff);
}
else if (strchr(Dword, ch) > 0)
{
Buff[0] = ch;
ch = getc(fin);
if (ch == ‘=‘)
{
Buff[1] = ch;
Buff[2] = ‘\0‘;
ch = getc(fin);
fprintf(fout, "%s\t%s\n", Buff, Buff);
}
else
{
Buff[1] = ‘\0‘;
if (Buff[0] == ‘!‘)
{
printf("Line %d\t%s\t%s\n", line, "ERROR:", Buff);
}
else
{
fprintf(fout, "%s\t%s\n", Buff, Buff);
}
}
}
else if (ch == ‘/‘)
{
ch = getc(fin);
if (ch == ‘*‘)
{
char ch1;
ch1 = getc(fin);
while (true)
{
if (ch1 == EOF)
{
printf("Line %d\t%s\tNote does‘t match!\n", line,"ERROR:" );
break;
}
ch = ch1;
ch1 = getc(fin);
if (ch == ‘*‘&&ch1 == ‘/‘)
{
break;
}
}
ch = getc(fin);
}
else
{
Buff[0] = ‘/‘;
Buff[1] = ‘\0‘;
fprintf(fout, "%s\t%s\n", Buff, Buff);
}
}
else
{
Buff[0] = ch;
Buff[1] = ‘\0‘;
ch = getc(fin);
flag = 3;
printf("Line %d\t%s\t%s\n", line, "ERROR:", Buff);
}
}
fclose(fin);
fclose(fout);
return(flag);
}
void main()
{
int flag = 0;
flag = test();
if (flag > 0)
{
printf("Compile Error\n");
}
else
{
printf("No Error\n");
}
}
原文地址:http://blog.csdn.net/u013298384/article/details/45675019