📄 main.c
字号:
#include "stdio.h"
#include "Parse.h"
#include "Global.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <ctype.h>
#include <conio.h>
#define NULL 0
#define OK 1
#define size 100
FILE *fp;
char ch;
char *keyword[6]={"begin","else","end","if","then","var"};
char *comparison[5]={"<","<=",">",">=","<>"};
char *equal[1]={"="};
char *interpunction[10]={",",";",":=",".","(",")","+","-","*","/"};
char *now[2]={"while","do"};
int flag[1]={1};
char buffer[10][100];
int inc=-1;
bool search(char searchstr[],int wordtype)
{
int i;
switch (wordtype)
{
case 1:for(i=0;i<=5;i++)
{
if(strcmp(keyword[i],searchstr)==0)
return(true);
}
break;
case 2: for(i=0;i<=4;i++)
{
if(strcmp(comparison[i],searchstr)==0)
return(true);
}
break;
case 3: for(i=0;i<=9;i++)
{
if(strcmp(interpunction[i],searchstr)==0)
return(true);
}
break;
case 4:for(i=0;i<=1;i++)
{
if(strcmp(now[i],searchstr)==0)
return(true);
}
break;
case 5:{
if(strcmp(equal[0],searchstr)==0)
return(true);
}
break;
}
return(false);
}
char letterprocess (char ch)//字母处理函数
{
int i=-1,j=1;
char letter[20];
while (isalnum(ch)!=0)
{
letter[++i]=ch;
ch=fgetc(fp);
}
letter[i+1]='\0';
if (search(letter,1))
{
printf("<%s,->\n",letter);printf("aaaaa");
flag[0]=0;
}
else if(search(letter,4))
printf("<%s,->\n",letter);
else
{
printf("<indentifier,%s>\n",letter);
strcpy(buffer[++inc],letter);
}
return(ch);
}
char numberprocess(char ch)//数字处理程序
{
int i=-1;
char num[20];
// printf("该文法不在此次考虑中\n");
flag[0]=0;
while (isdigit(ch)!=0)
{
num[++i]=ch;
ch=fgetc(fp);
}
if(isalpha(ch)!=0)
{
while(isspace(ch)==0)
{
num[++i]=ch;
ch=fgetc(fp);
}
num[i+1]='\0';
printf("错误!非法标识符:%s\n",num);
goto u;
}
num[i+1]='\0';
printf("<num,%s>\n",num);
u: return(ch);
}
char otherprocess(char ch)
{
int i=-1;
char other[20];
if (isspace(ch)!=0)
{
ch=fgetc(fp);
goto u;
}
while ((isspace(ch)==0)&&(isalnum(ch)==0))
{
other[++i]=ch;
ch=fgetc(fp);
}
other[i+1]='\0';
if (search(other,3))
{
printf("<relop,%s>\n",other);
flag[0]=0;
}
else
if (search(other,2))
{
printf("<%s,->\n",other);
strcpy(buffer[++inc],other);
}
else if(search(other,5))
printf("<%s,->\n",other);
else
printf("错误!非法字符:%s\n",other);
u: return (ch);
}
void cifa()
{
char str;
printf(" ********** 词法分析器 **********\n");
if ((fp=fopen("lj.txt","r"))==NULL)
printf("源程序无法打开!\n");
else
{
str =fgetc(fp);
while (str!=EOF)
{
if (isalpha(str)!=0)
str=letterprocess(str);
else
{
if (isdigit(str)!=0)
str=numberprocess(str);
else
str=otherprocess(str);
}
};
printf("词法分析结束!\n");
printf(" ********** 语法分析器 **********\n");
}
}
/*
Get string from user, and
store the value into the global variable Buff */
void InputString()
{
int len = 0;
printf("表达式文法为: ");
printf(GRAMMAR);
printf("\n Please input any string to parse:\n");
scanf("%s", Buff);
len = strlen(Buff);
Buff[len] = '#';
Buff[len+1] = 0;
}
void main()
{
printf(" DO-WHILE循环语句的翻译\n ");
printf("循环语句的格式为:DO <赋值语句> WHILE <表达式>\n ");
printf("翻译的语句:do c=a+1 while a>b\n ");
cifa();
InputString();
Parse();
getch();
printf(" \n (1) (+,a,1,c)\n ");
printf(" (2) (>,a,b,t0)\n ");
printf(" (3) (=,t0,true,1)\n ");
printf(" (4) (-,-,-,-)\n ");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -