📄 final.c
字号:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define ID 1
#define INT 2
#define KEY 3
#define LT 4
#define LE 5
#define GE 6
#define GT 7
#define EQ 8
#define LB 9
#define RB 10
#define LBB 11
#define RBB 12
#define ADD 13
#define SUB 14
#define MUL 15
#define DIV 16
#define SEM 17
#define ASS 18
#define ERROR 0
char * key[16] =
{
"int", "main", "while", "char", "long", "short", "float", "struct", "union", "while", "if", "printf",
"scanf", "for", "else", "switch"
};
int lookupid( char * p, char * q[], int r );
char * lookupkey( char * p, char * q[16] );
void output( int i, char * p );
int switchnumber(char *p[],int i);
int calculate(int a,int b,char *p[]);
char * gettoken( int Method ,char * p[] );
int numbercheck( char * p[] );
int opcheck( char * p[] );
int idcheck( char * t[],int r, char * q );
void definition( char * t[],int r,char * p[] );
void assignment( char * t[],int r,char * p[] );
void condition( char * t[], int r,char * p[] );
void procedure( char * t[],int r,char * p[] );
void syntaxanalyse( char * t[],int r,char * p[] );
void report_error( void )
{
printf( "input a illegal charactor!\n" );
}
main()
{
FILE * fp, * fp1;
char ch, c[16], token[20];
char space[] ={' '}, space1[] = " ";
int i, a,b, j = 1, r = 1;
long int w;
char * tokentable[50], * idtable[10];
idtable[1] = space1;
printf("reading characters from 'a.c'......\n\n");
for (w=1;w<=100000000;w++)
{ ;
}
fp = fopen( "a.c", "r" );
while ( !feof( fp ) )
{
ch = fgetc( fp );
if ( isalpha( ch ) )
{
token[0] = ch;
ch = fgetc( fp );
i = 1;
while ( isalnum( ch ) )
{
token[i] = ch;
i++;
ch = fgetc( fp );
}
token[i] = '\0';
tokentable[j] = strdup( token );
j++;
fseek( fp, -1, 1 );
/* backward a byte */
strcpy( c, lookupkey( token, key ) );
if ( strcmp( c, "0" ) == 0 )
{
output( ID, token );
if ( r == 1 )
{idtable[r] = strdup( token );
r++;}
else
{
w = lookupid( token, idtable, r - 1 );
if ( w == 0 )
{
idtable[r] = strdup( token );
r++;
}
}
}
else
output( KEY, c );
}
/* identifier or key testing */
else if ( isdigit( ch ) )
{
token[0] = ch;
ch = fgetc( fp );
i = 1;
while ( isdigit( ch ) )
{
token[i] = ch;
i++;
ch = fgetc( fp );
}
token[i] = '\0';
tokentable[j] = strdup( token );
j++;
fseek( fp, -1, 1 );
output( INT, token );
}
/* digit testing */
else
{
if ( ch > '!' && ch < '~' )
switch ( ch )
{
case '<':
ch = fgetc( fp );
if ( ch == '=' )
{
strcpy( token, "<=" );
tokentable[j] = strdup( token );
j++;
output( LE, space );
}
else
{
fseek( fp, -1, 1 );
strcpy( token, "<" );
tokentable[j] = strdup( token );
j++;
output( LT, space );
}
break;
case '>':
ch = fgetc( fp );
if ( ch == '=' )
{
strcpy( token, ">=" );
tokentable[j] = strdup( token );
j++;
output( GE, space );
}
else
{
fseek( fp, -1, 1 );
strcpy( token, ">" );
tokentable[j] = strdup( token );
j++;
output( GT, space );
}
break;
case '=':
ch = fgetc( fp );
if ( ch == '=' )
{
output( EQ, space );
strcpy( token, "==" );
tokentable[j] = strdup( token );
j++;
}
else
{
fseek( fp, -1, 1 );
strcpy( token, "=" );
tokentable[j] = strdup( token );
j++;
output( ASS, space );
}
break;
case '(':
{
output( LB, space );
strcpy( token, "(" );
tokentable[j] = strdup( token );
j++;
}
break;
case ')':
{
output( RB, space );
strcpy( token, ")" );
tokentable[j] = strdup( token );
j++;
}
break;
case '{':
{
output( LBB, space );
strcpy( token, "{" );
tokentable[j] = strdup( token );
j++;
}
break;
case '}':
{
output( RBB, space );
strcpy( token, "}" );
tokentable[j] = strdup( token );
j++;
}
break;
case '+':
{
output( ADD, space );
strcpy( token, "+" );
tokentable[j] = strdup( token );
j++;
}
break;
case '-':
{
output( SUB, space );
strcpy( token, "-" );
tokentable[j] = strdup( token );
j++;
}
break;
case '*':
{
output( MUL, space );
strcpy( token, "*" );
tokentable[j] = strdup( token );
j++;
}
break;
case '/':
{
output( DIV, space );
strcpy( token, "/" );
tokentable[j] = strdup( token );
j++;
}
break;
case ';':
{
output( SEM, space );
strcpy( token, ";" );
tokentable[j] = strdup( token );
j++;
}
break;
default:
report_error();
break;
}
/* switch */
}
/* else */
}
/* while */
fclose( fp );
fp1 = fopen( "b.c", "a" );
fprintf( fp1, "\n\n\n List of tokentable:\n");
for ( w = 1; w <= ( j - 1 ); w++ )
fprintf( fp1, "%s\n", tokentable[w] );
fprintf( fp1, "\n\n\n List of idtable:\n");
for ( w = 1; w <= ( r - 1 ); w++ )
fprintf( fp1, "%s\n", idtable[w] );
printf("Accidence analysis completed! you can check it up in the file 'b.c'...\n\n");
fclose( fp1 );
printf("U want to continue to begin syntax analysis?\npress Enter to continue...\n");
ch=getchar();
if(ch=='\n')
{
printf("Syntax analysing......\n");
for(w=1;w<=100000000;w++)
{ ;
}
syntaxanalyse( idtable,r-1,tokentable );
}
printf("\n\nU can modify your program with the hints above,\nif there is no error, press Enter to continue...\n");
ch=getchar();
if(ch=='\n')
{
a=switchnumber(tokentable,16);
b=switchnumber(tokentable,20);
if(b==0&&strcmp(tokentable[31],"/")==0)
printf("Error,the divisor can not be 0!");
else
{
if(strcmp(tokentable[25],">")==0)
{
if(a>b)
{
w=calculate(a,b,tokentable);
printf("The result is %d.",w);
}
else printf("The condition was not satisfied,no result.");
}
else if(strcmp(tokentable[25],"<")==0)
{
if(a<b)
{
w=calculate(a,b,tokentable);
printf("The result is %d.",w);
}
else printf("The condition was not satisfied,no result.");
}
else if(strcmp(tokentable[25],"==")==0)
{
if(a==b)
{
w=calculate(a,b,tokentable);
printf("The result is %d.",w);
}
else printf("The condition was not satisfied,no result.");
}
else if(strcmp(tokentable[25],"<=")==0)
{
if(a<=b)
{
w=calculate(a,b,tokentable);
printf("The result is %d.",w);
}
else printf("The condition was not satisfied,no result.");
}
else
{
if(a>=b)
{
w=calculate(a,b,tokentable);
printf("The result is %d.",w);
}
else printf("The condition was not satisfied,no result.");
}
}/*else*/
} /*if*/
getch();
}
/* 词法分析部分 */
/* 下面做语法分析 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -