不会用
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KEYSIZE 11
#define SYMBOLSIZE 7
#define MODIFIERSIZE 8
#define KEYWORD 1
#define DEFINED 2
#define SYMBOL 3
#define NUM 4
#define MODIFIER 5
#define ERROR 6
#define POINT_TYPE 7
#define FUNC_TYPE 8
#define ARR_TYPE 9
#define VAR_TYPE 10
#define LONG_KEY_POSITION 3
#define SHORT_KEY_POSITION 2
#define LONG_MODI_POSITION 1
#define SHORT_MODI_POSITION 0
#define ISCHARACTER ((97<=*str&&*str<=122)||(65<=*str&&*str<=90))
#define ISCHAR_NUM_UNDERLINE ((97<=*(str+i)&&*(str+i)<=122)||(65<=*(str+i)&&*(str+i)<=90)||(48<=*(str+i)&&*(str+i)<=57)||(*(str+i)=='_'))
#define IS_NUM(A) (48<=A&&A<=57)
#define IS_LEFT_BRACKET(i) exp[i]==SYMBOL&&exp[i+1]==1
#define IS_RIGHT_BRACKET exp[i]==SYMBOL&&exp[i+1]==3
#define IS_LEFT_SQUARE_BRACKET exp[i]==SYMBOL&&exp[i+1]==2
#define IS_RIGHT_SQUARE_BRACKET exp[i]==SYMBOL&&exp[i+1]==4
#define IS_ASTERISK_IN_STORE(A) exp_store_sequence[A]==SYMBOL&&exp_store_sequence[A+1]==7
#define IS_ARRARY exp[i+2]==SYMBOL&&exp[i+3]==2&&exp[i+4]==NUM&&exp[i+6]==SYMBOL&&exp[i+7]==4
#define IS_VARIABLE(i) i==size||(IS_LEFT_BRACKET(i))
#define IS_COMMA(exp,i) exp[i]==SYMBOL&&exp[i+1]==5
#define PUSH(A,B) exp_store_sequence[exp_store_seq_flag++]=A;\
exp_store_sequence[exp_store_seq_flag++]=B
typedef struct defined{
char definedname[10];
int definedtype;
int position;
struct defined *next;
}defined;
typedef struct{
char keyname[10];
char keyexp[100];
}keyword;
char dealword[100];
keyword key[KEYSIZE]={
"char","字符型",
"int","整型",
"short","短整型",
"long","长整型",
"float","浮点型",
"double","双精度浮点型",
"union","共用体",
"struct","结构体",
"enum","枚举体",
"void","无返回值或者无参或者无类型",
};
keyword modifierkey[MODIFIERSIZE]={
"short","短整型的",
"long","长整型的",
"signed","有符号的",
"unsigned","无符号的",
"auto","自动的",
"extern","全局的",
"static","静态的",
"typedef","别名",
};
char symbol[]={'(','[',')',']',',',' ','*'};
char exp_sequence[40];
char exp_store_sequence[40];
char exp_symbol_sequence[20];
char exp_keyword_sequence[20];
char exp_error_sequence[20][20];
char exp_defined_sequence[20][20];
char exp_modifier_sequence[20];
int exp_num_sequence[20];
int exp_sequence_flag=0;
int exp_symbol_seq_flag=0;
int exp_keyword_seq_flag=0;
int exp_num_seq_flag=0;
int exp_error_seq_flag=0;
int exp_modifier_seq_flag=0;
int exp_defined_seq_flag=0;
int exp_store_seq_flag=0;
struct defined def;
int issymbol(char c)
{
int i=0,flag=0;
while(i<SYMBOLSIZE){
if(c==symbol[i]){
flag=i+1;
break;
}
i++;
}
return flag;
}
int isdefined(char *str)
{
int i=1,num,flag;
flag=KEYSIZE>MODIFIERSIZE ? KEYSIZE:MODIFIERSIZE;
if( ISCHARACTER ){
while(*(str+i)){
if(!ISCHAR_NUM_UNDERLINE){
exp_sequence[exp_sequence_flag]=ERROR;
exp_sequence_flag++;
exp_sequence[exp_sequence_flag]=0;
exp_sequence_flag++;
return 0;
}
i++;
}
for(i=0;i<flag;i++){
if(i<KEYSIZE){
if(!strcmp(key[i].keyname,str)){
exp_sequence[exp_sequence_flag]=KEYWORD;
exp_sequence_flag++;
//exp_keyword_sequence[exp_keyword_seq_flag]=i;
//exp_keyword_seq_flag++;
exp_sequence[exp_sequence_flag]=i;
exp_sequence_flag++;
break;
}
}
if(i<MODIFIERSIZE){
if(!strcmp(modifierkey[i].keyname,str)){
exp_sequence[exp_sequence_flag]=MODIFIER;
exp_sequence_flag++;
//exp_modifier_sequence[exp_modifier_seq_flag]=i;
//exp_modifier_seq_flag++;
exp_sequence[exp_sequence_flag]=i;
exp_sequence_flag++;
break;
}
}
}
if(i==flag){
exp_sequence[exp_sequence_flag]=DEFINED;
exp_sequence_flag++;
strcpy(exp_defined_sequence[exp_defined_seq_flag],dealword);
exp_sequence[exp_sequence_flag]=exp_defined_seq_flag;
exp_sequence_flag++;
exp_defined_seq_flag++;
}
}
else if(IS_NUM(*str)){
num=*str-48;
while(*(str+i)){
if(!IS_NUM(*(str+i))){
exp_sequence[exp_sequence_flag]=ERROR;
exp_sequence_flag++;
exp_sequence[exp_sequence_flag]=0;
exp_sequence_flag++;
return 0;
}
num*=10+(*(str+i)-48);
i++;
}
exp_sequence[exp_sequence_flag]=NUM;
exp_sequence_flag++;
exp_num_sequence[exp_num_seq_flag]=num;
exp_sequence[exp_sequence_flag]=exp_num_seq_flag;
exp_num_seq_flag++;
exp_sequence_flag++;
}
else{
exp_sequence[exp_sequence_flag]=ERROR;
exp_sequence_flag++;
exp_sequence[exp_sequence_flag]=0;
exp_sequence_flag++;
return 0;
}
return 1;
}
int readword(char *str,int i)
{
int n,j=0;
if(*(str+i)=='\0')
return 0;
if((n=issymbol(*(str+i)))){
if(*(str+i)!=' '){
exp_sequence[exp_sequence_flag]=SYMBOL;
exp_sequence_flag++;
exp_symbol_sequence[exp_symbol_seq_flag]=n;
exp_sequence[exp_sequence_flag]=n;
exp_sequence_flag++;
exp_symbol_seq_flag++;
}
i++;
}
else{
for(;*(str+i)!='\0';i++){
if(!issymbol(*(str+i))){
dealword[j]=*(str+i);
j++;
}
else{
dealword[j]='\0';
break;
}
}
if(!isdefined(dealword)){
strcpy(exp_error_sequence[exp_error_seq_flag],dealword);
exp_error_seq_flag++;
}
memset(dealword,0,100);
}
readword(str,i);
}
int symbolmatching()
{
int flag1=0,flag2=0,i=0,error_flag=0;
for(;i<exp_symbol_seq_flag;i++){
if(exp_symbol_sequence[i]==1)
flag1++;
else if(exp_symbol_sequence[i]==3)
flag1--;
else if(exp_symbol_sequence[i]==2)
flag2++;
else if(exp_symbol_sequence[i]==4)
flag2--;
else
;
if(flag1<0||flag2<0 \
||(exp_symbol_sequence[i]==1&&exp_symbol_sequence[i+1]==4) \
||(exp_symbol_sequence[i]==2&&exp_symbol_sequence[i+1]==3)){
printf("符号不匹配");
return 0;
}
}
if(flag1>0||flag2>0){
printf("符号号不匹配");
return 0;
}
return 1;
}
int have_no_error()
{
int i,j=0,flag=0;
for(i=0;i<exp_sequence_flag;i+=2){
if(exp_sequence[i]==ERROR){
printf("[%s]:表达式错误 ",exp_error_sequence[j]);
j++;
}
if(exp_sequence[i]==DEFINED)
flag=1;
}
if(flag==0)
printf("表达式中没有标识符\n");
if(j==0&&flag==1)
return 1;
else
return 0;
}
/*int exp_segment(char *exp,int size,char *segment)
{
int i,j=0,bracket_flag=1;
for(i=0;i<size;i+=2){
if(IS_LEFT_BRACKET)
bracket_flag++;
if(IS_RIGHT_BRACKET)
bracket_flag--;
if(bracket_flag==0)
break;
segment[j++]=exp[i];
segment[j++]=exp[i+1];
}
}*/
int syntax_simple(char *exp,int size)
{
int i=0,j=0,asterisk_num=0,n=0,bracket_flag,n1,n2,z;
char expsegment[40],func_list[40];
for(;i<size;i+=2){
if(IS_LEFT_BRACKET(i)){
bracket_flag=1;j=0;
for(i+=2;i<size;i+=2){
if(IS_LEFT_BRACKET(i))
bracket_flag++;
if(IS_RIGHT_BRACKET)
bracket_flag--;
if(bracket_flag==0)
break;
expsegment[j++]=exp[i];
expsegment[j++]=exp[i+1];
}
if( exp_store_seq_flag>=2&&
(exp_store_sequence[exp_store_seq_flag-2]==VAR_TYPE||
exp_store_sequence[exp_store_seq_flag-2]==POINT_TYPE||
exp_store_sequence[exp_store_seq_flag-2]==ARR_TYPE
)
){
if(j==0&&(i+4)<size){
printf("解析出是个函数,但是函数列表后存有不名符号\n");
PUSH(ERROR,0);
return 0;
}
n2=exp_store_seq_flag;
PUSH(FUNC_TYPE,0);
while(n<j){
n1=0;
for(;n<j;n+=2){
if(IS_COMMA(expsegment,n))
break;
func_list[n1++]=expsegment[n];
func_list[n1++]=expsegment[n+1];
}
syntax_simple(func_list,n1);
if(n!=j){
//printf("xad%d %d\n",n,j);
PUSH(SYMBOL,5);
}
exp_store_sequence[n2+1]++;
n+=2;
}
}
else{
if(j==0){
printf("表达式错误:存在空括号\n");
PUSH(ERROR,0);
return 0;
}
syntax_simple(expsegment,j);
memset(expsegment,0,40);
}
}
else{
if(exp[i]==DEFINED){
if(IS_ARRARY){
if(IS_ASTERISK_IN_STORE(exp_store_seq_flag-2)){
asterisk_num++;
for(exp_store_seq_flag-=2;exp_store_seq_flag>=0;exp_store_seq_flag-=2)
if(IS_ASTERISK_IN_STORE(exp_store_seq_flag-2))
asterisk_num++;
else
break;
PUSH(exp[i],exp[i+1]);
PUSH(POINT_TYPE,asterisk_num);
PUSH(ARR_TYPE,exp[i+5]);
}
else{
PUSH(exp[i],exp[i+1]);
PUSH(ARR_TYPE,exp[i+5]);
}
i+=6;
}
else if(IS_VARIABLE(i+2)){
if(IS_ASTERISK_IN_STORE(exp_store_seq_flag-2)){
asterisk_num++;
for(exp_store_seq_flag-=2;exp_store_seq_flag>=0;exp_store_seq_flag-=2)
if(IS_ASTERISK_IN_STORE(exp_store_seq_flag-2))
asterisk_num++;
else
break;
PUSH(exp[i],exp[i+1]);
PUSH(POINT_TYPE,asterisk_num);
PUSH(VAR_TYPE,0);
}
else{
PUSH(exp[i],exp[i+1]);
PUSH(VAR_TYPE,0);
}
}
else{
PUSH(ERROR,0);
printf("错误\n");
return 0;
}
}
else{
PUSH(exp[i],exp[i+1]);
}
}
}
}
/*int no_error_for_syntax()
{
int i=0,flag;
for(;i<exp_store_seq_flag;i+=2){
if(exp_store_sequence[i]==ERROR)
return 0;
}
for(i=2;i<exp_store_seq_flag;i+=2){
//if(exp_store_sequence[i]==exp_store_sequence[i-2]){
// printf("存在两个以上相同类型的符号\n");
// return 0;
//}
if(exp_store_sequence[i]=[i]==DEFINED&&exp_store_sequence[i-2]!=KEYWORD){
printf("标识符类型未知\n");
return 0;
}
}
return 1;
}*/
int main(int argc,char **argv)
{
int i,j=0;
char *str="unsigned int A(char *a,char *b[20])";
readword(str,0);
if(have_no_error()&&symbolmatching()){
syntax_simple(exp_sequence,exp_sequence_flag);
}
for(i=0;i<exp_store_seq_flag;i+=2){
printf("%d.",i/2);
if(exp_store_sequence[i]==KEYWORD)
printf("关键字 :%s \n",key[exp_store_sequence[i+1]].keyname);
else if(exp_store_sequence[i]==DEFINED)
printf("标识符名:%s\n",exp_defined_sequence[exp_store_sequence[i+1]]);
else if(exp_store_sequence[i]==ARR_TYPE)
printf("数组:%d\n",exp_num_sequence[exp_store_sequence[i+1]]);
else if(exp_store_sequence[i]==VAR_TYPE)
printf("变量\n");
else if(exp_store_sequence[i]==POINT_TYPE)
printf("指针\n");
else if(exp_store_sequence[i]==FUNC_TYPE)
printf("函数\n函数列表如下:\n第一个参数为:\n");
else if(exp_store_sequence[i]==ERROR)
printf("这个地方有错误\n");
else if(exp_store_sequence[i]==MODIFIER)
printf("修饰词 :%s\n",modifierkey[exp_store_sequence[i+1]].keyname);
else if(exp_store_sequence[i]==SYMBOL&&exp_store_sequence[i+1]==5)
printf("下一个参数是:\n");
else
printf("我也不知道是什么\n");
}
}
[ 本帖最后由 朱大哥 于 2015-1-31 03:27 编辑 ]