C语言小子集报告,C语言小子集的词法分析程序.doc

#include #define ID 12

#define INT 13

#define JF 14

#define YSF 15

#define N 30

char TOKEN[N];

FILE *write;

int looksame(char *a)

{

int i;

Char*key[22]={"begin","end","if","then","else","for","do","while","and","or","not",

"BEGIN","END","IF","THEN","ELSE","FOR","DO","WHILE","AND","OR","NOT"};

for(i = 0;i < 22;i++)

{

if(strcmp(key[i],a) == 0)/*该字符串是否与关键字相匹配*/

return (i%11+1);

}

return 0;

}

/*本函数的意思是,查询一个字符串,看其是否与指定的字符相匹配,如果匹配返回1个非零的值,如果不匹配,则返回一个0值*/

void out(int a,char *b)

{

FILE *write;

write = fopen("E:\\b.txt","a+");

if(write == NULL)

{

printf("文件打开失败");

exit(0);

}

fprintf(write,"%d\t", a);

fwrite(b,strlen(b),1,write);

fprintf(write,"\n");

fclose(write);

printf("%d %20s\t\t",a,b);

}

//这个函数的意思就是吧a输入到指定文件中,然后从该文件中读出字符串,放到一个数组中输出//

int error()

{

printf("书写格式错误,未被识别\n");

return 0;

}

void function(FILE *fp)

{

char ch=' ';

int i,c;

while(ch!= EOF)

{

ch = fgetc(fp);

while(ch == ' ' || ch == '\t' || ch == '\n') {

ch = fgetc(fp);

}

if(isalpha(ch)) //isalpha()判断是否为英文字母,是则返回非零值,否则返回零值//

{

TOKEN[0] = ch;

ch = fgetc(fp);

i = 1;

while(isalnum(ch)) //isalnum()判断字符是否为英文字母或数字,如果是则返 回非零值,如果不是则返回零值//

{

TOKEN[i] = ch;

i++;

ch = fgetc(fp); }

TOKEN[i] = '\0';

fseek(fp,-1,1);

c = looksame(TOKEN);

if(c == 0)

{

out(ID,TOKEN);printf("标示符\n");

}

else

{

out(c,TOKEN);printf("关键字\n");

}

}

else if(isdigit(ch)) //isdigit()判断是否为a0-9的数字//

{

TOKEN[0] = ch;

ch = fgetc(fp);

i = 1;

while(isdigit(ch))

{

TOKEN[i] = ch;

i++;

ch = fgetc(fp);

}

TOKEN[i] = '\0';

fseek(fp,-1,1);

out(INT,TOKEN);

printf("常数\n");

}

else

{

switch(ch)

{

case'+':out(YSF,"+");printf("运算符\n");

break;

case'-':out(YSF,"-");printf("运算符\n");

break;

case';':out(JF,";");printf("界符\n");

break;

case',':out(JF,",");printf("界符\n");

break;

case'|':out(YSF,"|");printf("运算符\n");

break;

case'{':out(JF,"{");printf("界符\n");

break;

case'(':out(JF,"(");printf("界符\n");

break;

case'!':out(JF,"!");printf("界符\n");

break;

case'^':out(JF,"^");printf("界符\n");

break;

case')':out(JF,")");printf("界符\n");

break;

case'}':out(JF,"}");printf("界符\n");

break;

case'')

{

out(YSF,"<>");

printf("运算符\n");

}

else

{

fseek(fp,-1,1);

out(YSF,"':ch = fgetc(fp);

if(ch == '=')

{

out(YSF,">=");

printf("运算符\n");

}

else

{

fseek(fp,-1,1);

out(YSF,">");

printf("运算符\n");

}

break;

case':':ch = fgetc(fp);

if(ch == '=')

{

out(YSF,":=");

printf("运算符\n");

}

else

{

fseek(fp,-1,1);

out(JF,":");

printf("界符\n");

}

break;

case'/':ch = fgetc(fp);

if(ch == '*')

{

out(JF,"/*");

printf("界符\n");

while(1) // 注释的内容不反应在词法分析中// {

while(ch != '/')

ch = fgetc(fp);

fseek(fp, -2, 1);

ch = fgetc(fp);

if(ch == '*')

{

fseek(fp, 1, 1);

break;

}

else

{

fseek(fp, 2, 1);

ch = fgetc(fp);

}

}

fseek(fp, -2, 1);

}

else

{

fseek(fp,-1,1);

out(JF,"/");

printf("界符\n");

}

break;

case'*':ch = fgetc(fp);

if(ch == '/')

{

out(JF,"*/");

printf("界符\n");

}

else

{

fseek(fp,-1,1);

out(YSF,"*");

printf("运算符\n");

}

break;

case EOF:break;

default:error();

break;

}

}

}

}

int main()

{

FILE *read;

read = fopen("E:\\a.txt","r");

write = fopen("E:\\b.txt","a+");

if(read == NULL)

{

printf("FILE OPEN FAIL!");//

exit(0);

}

printf("===========================================================\n");

printf("====================词法分析程序===========================\n");

printf("===========该分析程序的文件存放在D:\\a.txt目录下==========\n");

printf("===========该程序的分析结果存放在D:\\b.txt目录下===========\n");

printf("============================================================\n");

function(read);

fclose(read);

system("pause");

return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言小子集是指C语言子集,只包含C语言的一部分语法。下面是一个简单的C语言小子集的词法分析程序示例: ```c #include <stdio.h> #include <ctype.h> #define MAX_LEN 100 enum Token_Type { TOKEN_INVALID, TOKEN_IDENTIFIER, TOKEN_NUMBER, TOKEN_PLUS, TOKEN_MINUS, TOKEN_MULTIPLY, TOKEN_DIVIDE, TOKEN_ASSIGN, TOKEN_SEMICOLON, TOKEN_LPAREN, TOKEN_RPAREN, }; struct Token { enum Token_Type type; char value[MAX_LEN + 1]; }; int is_keyword(char *str) { char *keywords[] = {"int", "float", "double", "char", "if", "else", "while"}; int num_keywords = sizeof(keywords) / sizeof(char *); for (int i = 0; i < num_keywords; i++) { if (strcmp(str, keywords[i]) == 0) { return 1; } } return 0; } int is_operator(char c) { char *operators = "+-*/="; int num_operators = strlen(operators); for (int i = 0; i < num_operators; i++) { if (c == operators[i]) { return 1; } } return 0; } int is_digit(char c) { return isdigit(c); } int is_alpha(char c) { return isalpha(c); } int is_alnum(char c) { return isalnum(c); } int is_space(char c) { return isspace(c); } struct Token get_next_token(char *input) { struct Token token = {TOKEN_INVALID, ""}; int len = strlen(input); int i = 0; while (i < len) { if (is_operator(input[i])) { if (input[i] == '+') { token.type = TOKEN_PLUS; } else if (input[i] == '-') { token.type = TOKEN_MINUS; } else if (input[i] == '*') { token.type = TOKEN_MULTIPLY; } else if (input[i] == '/') { token.type = TOKEN_DIVIDE; } else if (input[i] == '=') { token.type = TOKEN_ASSIGN; } strncpy(token.value, input + i, 1); i++; break; } else if (is_digit(input[i])) { token.type = TOKEN_NUMBER; int j = i; while (j < len && is_digit(input[j])) { j++; } strncpy(token.value, input + i, j - i); token.value[j - i] = '\0'; i = j; break; } else if (is_alpha(input[i])) { token.type = TOKEN_IDENTIFIER; int j = i; while (j < len && is_alnum(input[j])) { j++; } strncpy(token.value, input + i, j - i); token.value[j - i] = '\0'; if (is_keyword(token.value)) { token.type = TOKEN_IDENTIFIER; } i = j; break; } else if (is_space(input[i])) { i++; continue; } else if (input[i] == ';') { token.type = TOKEN_SEMICOLON; strncpy(token.value, input + i, 1); i++; break; } else if (input[i] == '(') { token.type = TOKEN_LPAREN; strncpy(token.value, input + i, 1); i++; break; } else if (input[i] == ')') { token.type = TOKEN_RPAREN; strncpy(token.value, input + i, 1); i++; break; } else { token.type = TOKEN_INVALID; strncpy(token.value, input + i, 1); i++; break; } } return token; } int main() { char input[MAX_LEN + 1]; printf("Enter input: "); fgets(input, MAX_LEN + 1, stdin); struct Token token; do { token = get_next_token(input); printf("Type: %d, Value: %s\n", token.type, token.value); strncpy(input, input + strlen(token.value), MAX_LEN - strlen(token.value) + 1); } while (token.type != TOKEN_INVALID); return 0; } ``` 这个程序可以解析简单的C语言小子集的表达式,包括标识符、数字、加减乘除运算符和赋值运算符等。它使用了一个`Token`结构体表示词法单元,以及`get_next_token`函数来解析输入字符串并返回下一个词法单元。在`main`函数中,我们可以循环调用`get_next_token`函数来逐个解析输入字符串中的词法单元,并输出它们的类型和值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值