コンパイル原理:構文解析プログラム
14645 ワード
一、目的
文法解析と文法解析プログラムを作成し、TEST言語で作成されたソースプログラムを分析して文法解析と文法解析の結果を得た.
二、ソースプログラム
文法解析と文法解析プログラムを作成し、TEST言語で作成されたソースプログラムを分析して文法解析と文法解析の結果を得た.
二、ソースプログラム
#include
#include//ctype.h C , C ,
#include//conio.h ,
#include
#include
#define keywordSum 9
// ( 9 char)( )
char *keyword[keywordSum] = { "if","else","for","while","do","read","write","char","int" };//return,switch,case,break,continue
// ( )
char singleword[50] = { '+','-','*','(',')','{','}',',',';',':' ,'.'};
//
char doubleword[10] = { '>','= 'a'&&ch <= 'z' || ch >= 'A'&&ch <= 'Z' || ch == '_')//( )( )
{
token[0] = ch;
j = 1;
ch = getc(fin);
while (ch >= 'a'&&ch <= 'z' || ch >= 'A'&&ch <= 'Z' || ch == '_' || ch >= '0'&&ch <= '9')
{
token[j++] = ch;
ch = getc(fin);
}
token[j] = '\0';
//
n = 0;
while ((n= keywordSum)//
fprintf(fout, "%s\t%s
", "ID", token);
else
fprintf(fout, "%s\t%s
", token, token);
}
//
else if (ch >= '0'&&ch <= '9')
{
token[0] = ch;
j = 1;
ch = getc(fin);
while (ch >= '0'&&ch <= '9')
{
token[j++] = ch;
ch = getc(fin);
}
token[j] = '\0';
fprintf(fout, "%s\t%s
", "NUM", token);
}
//
else if (strchr(singleword, ch) > 0)// s ch , ch , s ch NULL。
{
token[0] = ch;
token[1] = '\0';
ch = getc(fin);
fprintf(fout, "%s\t%s
", token, token);
}
//
else if (strchr(doubleword, ch) > 0)
{
token[0] = ch;
ch = getc(fin);
if (ch == '=')
{
token[1] = ch;
token[2] = '\0';
ch = getc(fin);
}
else
token[1] = '\0';
fprintf(fout, "%s\t%s
", token, token);
}
//
else if (ch == '/')
{
ch = getc(fin);
if (ch == '*')
{
char ch1;
ch1 = getc(fin);
do
{
ch = ch1;
ch1 = getc(fin);
} while ((ch != '*' || ch1 != '/') && ch1 != EOF);
ch = getc(fin);
}
else
{
token[0] = '/';
token[1] = '\0';
fprintf(fout, "%s\t%s
", token, token);
}
}
//
else
{
token[0] = ch;
token[1] = '\0';
ch = getc(fin);// , 。
es = 3;
fprintf(fout, "%s\t%s
", "ERROR", token);
}
}
fclose(fin);
fclose(fout);
return(es);
}
//
int TESTparse()
{
int es = 0;
if ((fp = fopen(Scanout, "r")) == NULL)
{
printf("
!
");
es=10;
}
printf(" ( ):");
scanf("%s", Result);
if ((fr = fopen(Result, "w")) == NULL)
{
printf("
!
");
return(8);
}
if (es == 0)
es = program();
printf("
:
");
switch (es)
{
case 0:
printf(" !
");
fprintf(fr,"
%s
"," !
");
break;
case 1:
printf(" {
");
fprintf(fr, "
%s
", " {
");
break;
case 2:
printf(" }
");
fprintf(fr, "
%s
", " }
");
break;
case 3:
printf("
");//declaration_stat()
fprintf(fr, "
%s
", "
");
break;
case 4:
printf("
");
fprintf(fr, "
%s
", "
");
break;
case 5:
printf(" (
");
fprintf(fr, "
%s
", " (
");
break;
case 6:
printf(" )
");
fprintf(fr, "
%s
", " )
");
break;
case 7:
printf("
");//factor()
fprintf(fr, "
%s
", "
");
break;
case 8:
printf("
!
");
fprintf(fr, "
%s
", "
!
");
break;
case 10:
printf("
!
");
fprintf(fr, "
%s
", "
!
");
break;
}
fclose(fp);
fclose(fr);
return(es);
}
//
int program()
{
int es = 0;
fscanf(fp,"%s\t%s
",&token,&token1);//“ ”
fprintf(fr, "%s\t%s
", &token, &token1);
if (strcmp(token, "{"))// , “0”, , token
{
es = 1;
return es;
}
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es = declaration_list();//fscanf printf
if (es > 0)
return(es);
es = statement_list();
if (es > 0)
return(es);
if (strcmp(token, "}"))
{
es = 2;
return(es);
}
return(es);
}
int declaration_list()
{
int es = 0;
while ((strcmp(token,"int")==0)||(strcmp(token,"char")==0))// char
{
es = declaration_stat();
if (es > 0)
return(es);
}
return(es);
}
int declaration_stat()
{
int es = 0;
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
if (strcmp(token, "ID"))
return(es=3);//
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
if (strcmp(token, ";"))
return(es = 4);
fscanf(fp, "%s\t%s
", &token, &token1);// token, declaration_list()
fprintf(fr, "%s\t%s
", &token, &token1);
return(es);
}
int statement_list()
{
int es = 0;
while (strcmp(token, "}"))// token “}” ,while(0), , statement 。program !
// program()
{
es = statement();
if (es > 0)
return(es);
}
return(es);
}
int compound_stat()
{
int es = 0;
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es = statement_list();
return(es);
}
int statement()
{
int es = 0;
if (es == 0 && strcmp(token, "if") == 0)
{
fprintf(fr, "
%s
", "
---if ---
");
printf("
if
");
es = if_stat();
}
if (es == 0 && strcmp(token, "while") == 0)
{
fprintf(fr, "
%s
", "
---while ---
");
printf("
while
");
es = while_stat();
}
if (es == 0 && strcmp(token, "for") == 0)
{
fprintf(fr, "
%s
", "
---for ---
");
printf("
for
");
es = for_stat();
}
if (es == 0 && strcmp(token, "read") == 0)
{
fprintf(fr, "
%s
", "
---read ---
");
printf("
read
");
es = read_stat();
}
if (es == 0 && strcmp(token, "write") == 0)
{
fprintf(fr, "
%s
", "
---write ---
");
printf("
write
");
es = write_stat();
}
if (es == 0 && strcmp(token, "{") == 0)
{
fprintf(fr, "
%s
", "
--- ---
");
printf("
");
es = compound_stat();
}
if (es == 0 && strcmp(token, "ID") == 0 || strcmp(token, "NUM") == 0 || strcmp(token, "(") == 0)
{
printf("
");
es = expression_stat();
}
return(es);
}
int if_stat(){
int es=0;
fscanf(fp,"%s\t%s
",&token,&token1);
fprintf(fr, "%s\t%s
", &token, &token1);
if(strcmp(token,"("))
return(es=5);
fscanf(fp,"%s\t%s
",&token,&token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es=expression();
if(es>0)
return(es);
if(strcmp(token,")"))
return(es=6);
fscanf(fp,"%s\t%s
",&token,&token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es=statement();
if(es>0)
return(es);
if(strcmp(token,"else")==0)
{
fscanf(fp,"%s\t%s
",&token,&token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es=statement();
if(es>0)
return(es);
}
return(es);
}
int while_stat()
{
int es = 0;
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
if (strcmp(token, "("))
return(es = 5);
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es = expression();
if (es>0)
return(es);
if (strcmp(token, ")"))
return(es = 6);
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es = statement();
return(es);
}
int for_stat()
{
int es = 0;
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
if (strcmp(token, "("))
return(es = 5);
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es = expression();
if (es>0)
return(es);
if (strcmp(token, ";"))
return(es = 4);
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es = expression();
if (es>0)
return(es);
if (strcmp(token, ";"))
return(es = 4);
fscanf(fp, "%s\t%s
",&token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es = expression();
if (es>0)
return(es);
if (strcmp(token, ")"))
return(es = 6);
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es = statement();
return(es);
}
int write_stat()
{
int es = 0;
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es = expression();
if (es>0)
return(es);
if (strcmp(token, ";"))
return(es = 4);
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
return(es);
}
int read_stat()
{
int es = 0;
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
if (strcmp(token, "ID"))
return(es = 3);
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
if (strcmp(token, ";"))
return(es = 4);
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
return (es);
}
int expression_stat()
{
int es = 0;
if (strcmp(token, ";") == 0)
{
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
return(es);
}
es = expression();
if (es > 0)
return(es);
if (es == 0 && strcmp(token, ";") == 0)
{
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
return(es);
}
else
{
es = 4;
return(es);// ;
}
}
int expression()
{
int es = 0, fileadd;
char token2[20], token3[40];
if (strcmp(token, "ID") == 0)
{
fileadd = ftell(fp);
fscanf(fp, "%s\t%s
", &token2, &token3);
fprintf(fr, "%s\t%s
", &token2, &token3);
if (strcmp(token2, "=") == 0)
{
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es = bool_expr();
if (es>0)
return(es);
}
else
{
fseek(fp, fileadd, 0);
es = bool_expr();
if (es>0)
return(es);
}
}
else
es = bool_expr();
return(es);
}
int bool_expr()
{
int es = 0;
es = additive_expr();
if (es>0)
return(es);
if (strcmp(token, ">") == 0 || strcmp(token, ">=") == 0 || strcmp(token, "0)
return(es);
}
return(es);
}
int additive_expr(){
int es = 0;
es = term();
if (es>0)
return(es);
while (strcmp(token, "+") == 0 || strcmp(token, "-") == 0)
{
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es = term();
if (es>0) return(es);
}
return(es);
}
int term(){
int es = 0;
es = factor();
if (es>0)
return(es);
while (strcmp(token, "*") == 0 || strcmp(token, "/") == 0)
{
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es = factor();
if (es>0)
return(es);
}
return(es);
}
int factor(){
int es = 0;
if (strcmp(token, "(") == 0)
{
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
es = expression();
if (es>0)
return(es);
if (strcmp(token, ")"))
return(es = 6);
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
}
else
{
if (strcmp(token, "ID") == 0 || strcmp(token, "NUM") == 0)
{
fscanf(fp, "%s\t%s
", &token, &token1);
fprintf(fr, "%s\t%s
", &token, &token1);
return(es);
}
else
{
es = 7;
return(es);
}
}
return(es);
}
//
void main()
{
int es = 0;
es = TESTscan();
switch (es) {//switch
case 0:
printf(" !
");
break;
case 1:
printf(" , !
");
break;
case 2:
printf(" , !
");
break;
case 3:
printf(" , !
");
break;
}
if (es == 0)
{
es = TESTparse();
if (es == 0)
printf(" !
");
else
printf(" !
");
}
system("pause");
}