#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KEYSIZE 17
#define SYMBOLSIZE 6
#define KEYWORD 1
#define DEFINED 2
#define SYMBOL 3
#define NUM 4
#define ERROR 5
#define POINT_TYPE 1
#define FUNC_TYPE 2
#define ARR_TYPE 3
#define VAR_TYPE 4
#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)
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","双精度浮点型",
"signed","有符号的",
"unsigned","无符号的",
"auto","自动的",
"extern","全局的",
"static","静态的",
"typedef","别名",
"union","共用体",
"struct","结构体",
"enum","枚举体",
"void","无返回值或者无参或者无类型",
"*","指针",
};
char symbol[]={'(','[',')',']',',',' '};
char exp_sequence[40];
char exp_symbol_sequence[20];
char exp_keyword_sequence[20];
char exp_error_sequence[20][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;
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;
if( ISCHARACTER ){
while(*(str+i)){
if(!ISCHAR_NUM_UNDERLINE){
exp_sequence[exp_sequence_flag]=ERROR;
exp_sequence_flag++;
return 0;
}
i++;
}
for(i=0;i<KEYSIZE;i++)
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++;
break;
}
if(i==KEYSIZE){
exp_sequence[exp_sequence_flag]=DEFINED;
exp_sequence_flag++;
strcpy(def.definedname,str); //确定标识符名字,当表达式存在多个标识符时,无法说出是哪几个 以后需更改 strcpy需改写
}
}
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++;
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_num_seq_flag++;
}
else{
exp_sequence[exp_sequence_flag]=ERROR;
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_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 definedmatching()
{
signed int i,flag=-1,position;
for(i=0;i<exp_sequence_flag;i++)
if(exp_sequence[i]==DEFINED){
flag++;
position=i;
}
if(!(flag==0)){
printf("存在零个或者两个以上标识符,表达式不合法");
return 0;
}
def.position=position;
return 1;
}
int have_no_error()
{
int i,j=0;
for(i=0;i<exp_sequence_flag;i++)
if(exp_sequence[i]==ERROR){
printf("[%s]:表达式错误 ",exp_error_sequence[j]);
j++;
}
if(j==0)
return 1;
else
return 0;
}
int main(int argc,char **argv)
{
int i,j=0;
char *str="int 1a[10]";
readword(str,0);
if(have_no_error()&&symbolmatching()&&definedmatching())
printf("可以进行语义分析了\n");
}
[ 本帖最后由 朱大哥 于 2015-1-28 18:04 编辑 ]
#include <stdlib.h>
#include <string.h>
#define KEYSIZE 17
#define SYMBOLSIZE 6
#define KEYWORD 1
#define DEFINED 2
#define SYMBOL 3
#define NUM 4
#define ERROR 5
#define POINT_TYPE 1
#define FUNC_TYPE 2
#define ARR_TYPE 3
#define VAR_TYPE 4
#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)
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","双精度浮点型",
"signed","有符号的",
"unsigned","无符号的",
"auto","自动的",
"extern","全局的",
"static","静态的",
"typedef","别名",
"union","共用体",
"struct","结构体",
"enum","枚举体",
"void","无返回值或者无参或者无类型",
"*","指针",
};
char symbol[]={'(','[',')',']',',',' '};
char exp_sequence[40];
char exp_symbol_sequence[20];
char exp_keyword_sequence[20];
char exp_error_sequence[20][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;
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;
if( ISCHARACTER ){
while(*(str+i)){
if(!ISCHAR_NUM_UNDERLINE){
exp_sequence[exp_sequence_flag]=ERROR;
exp_sequence_flag++;
return 0;
}
i++;
}
for(i=0;i<KEYSIZE;i++)
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++;
break;
}
if(i==KEYSIZE){
exp_sequence[exp_sequence_flag]=DEFINED;
exp_sequence_flag++;
strcpy(def.definedname,str); //确定标识符名字,当表达式存在多个标识符时,无法说出是哪几个 以后需更改 strcpy需改写
}
}
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++;
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_num_seq_flag++;
}
else{
exp_sequence[exp_sequence_flag]=ERROR;
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_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 definedmatching()
{
signed int i,flag=-1,position;
for(i=0;i<exp_sequence_flag;i++)
if(exp_sequence[i]==DEFINED){
flag++;
position=i;
}
if(!(flag==0)){
printf("存在零个或者两个以上标识符,表达式不合法");
return 0;
}
def.position=position;
return 1;
}
int have_no_error()
{
int i,j=0;
for(i=0;i<exp_sequence_flag;i++)
if(exp_sequence[i]==ERROR){
printf("[%s]:表达式错误 ",exp_error_sequence[j]);
j++;
}
if(j==0)
return 1;
else
return 0;
}
int main(int argc,char **argv)
{
int i,j=0;
char *str="int 1a[10]";
readword(str,0);
if(have_no_error()&&symbolmatching()&&definedmatching())
printf("可以进行语义分析了\n");
}
[ 本帖最后由 朱大哥 于 2015-1-28 18:04 编辑 ]