📄 scan.c~
字号:
/* * scan.c * * Created on: 2009-3-11 * Author: xiaohe */#include<string.h>#include<ctype.h>#include<stdlib.h>#include"readfile.h"#include"scan.h"#include"option.h"#include"tokens.h"struct kwords keywords[]={ {"int",INT}, {"char",CHAR}, {"float",FLOAT}, {"void",VOID}, {"const",CONST}, {"for",FOR}, {"if",IF}, {"else",ELSE}, {"then",THEN}, {"while",WHILE}, {"switch",SWITCH}, {"break",BREAK}, {"begin",BEGIN}, {"end",END}, {NULL,0}};char oper[]={'+','-','*','/','%','<','>','=','!','0'};char operator[][3]={"+","-","*","/","%","(",")","[","]","<",">","<=",">=","=","!=",";",",","/=","+=","-=","*=","%=","||","&&","!","0"};int scanner_init(){ pchar=line; fline=0; nerror=0; save=NULL; source=NULL; type=0; count=0; return 0;}int scanner(){ char str[20]; int n; char ch=' '; while(isspace(ch)) ch=readchar(); if(isalpha(ch)||ch=='_') { n=0; do { if(n<20) str[n++]=ch; ch=readchar(); }while(isalpha(ch)||isalnum(ch)); str[n]='\0'; pchar--; if(!Iskeywords(str)) { Isidentfier(str); } } else if(isalnum(ch)) { n=0; do { if(n<20) str[n++]=ch; ch=readchar(); }while(isalpha(ch)||isalnum(ch)); str[n]='\0'; Isnumber(str); } else { str[0]=ch; str[1]='\0'; Isoperator(str); } return 0;}int Iskeywords(char *str){ int i=0; while(keywords[i].sp) { if(!strcmp(str,keywords[i].sp)) { type=keywords[i].sy; strcpy(identfier[count].name,str); identfier[count].code=i+1; count ++; return 1; } i++; } return 0;}int Isidentfier(char *str){ int i; for(i=0;i<count;i++) { if(!strcmp(str,identfier[i].name)) { return 1; } } if(identfier[count].type!=type) { strcpy(identfier[count].name,str); identfier[count].type=type; identfier[count].code=ID; count++; return 1; } return 0;}int Isnumber(char *str){ int i; char *temp=str; for(i=0;i<strlen(str);i++) { if(!isdigit(*temp)) { error(str,1); return 0; } temp++; } strcpy(identfier[count].name,str); identfier[count].code=NUM; count++; return 1;}int Isoperator(char *str){ int i; char ch; if(Isoper(str)) //+ - * / % < > = ! { ch=readchar(); if(ch=='=') //+= -= *= /= %= <= >= == != { strcat(str,"="); strcpy(identfier[count].name,str); i=0; while(strcmp(str,operator[i])) { i++; } identfier[count].code=i+19; count++; return 1; } else { i=0; pchar--; strcpy(identfier[count].name,str); while(strcmp(str,operator[i])) { i++; } identfier[count].code=i+19; count++; return 1; } } else { if(*str=='|') { ch=readchar(); if(ch=='|') { i=0; strcat(str,"|"); strcpy(identfier[count].name,str); while(strcmp(str,operator[i])) { i++; } identfier[count].code=i+19; count++; return 1; } else { pchar--; error(str,2); return 0; } } if(*str=='&') { ch=readchar(); if(ch=='&') { i=0; strcat(str,"&"); strcpy(identfier[count].name,str); while(strcmp(str,operator[i])) { i++; } identfier[count].code=i+19; count++; return 1; } else { pchar--; error(str,2); return 0; } } i=0; while(strcmp(str,operator[i])) { if(!strcmp(operator[i],"0")) return 0; i++; } strcpy(identfier[count].name,str); identfier[count].code=i+19; count++; return 1; } return 1;}int Isoper(char *str){ int i=0; while(oper[i]!='0') { if(*str==oper[i]) return 1; i++; } return 0;}void error(char *str,int num){ nerror++; switch(num) { case 1: fprintf(stderr,"\"%s\" is unknow identfier line : %d\n",str,fline); break; case 2: fprintf(stderr,"\"%s\" is unknow operator line : %d\n",str,fline); break; case 3: break; }}void show_ident(){
int i; printf("The total errors is %d\n",nerror); printf("Identfier\n"); printf("Name\t\t\tType\t\t\n"); for(i=0;i<count;i++) { if(identfier[i].code==ID) printf("%s\t\t\t%d\n",identfier[i].name,identfier[i].type); }}void show_keywords(){
int i; printf("Keywords\n"); printf("Name\t\t\tCode\t\t\n"); for(i=0;i<count;i++) { if(identfier[i].code<ID) printf("%s\t\t\t%d\n",identfier[i].name,identfier[i].code); }}void show_number(){
int i; printf("Number\n"); printf("Name\t\t\tCode\t\t\n"); for(i=0;i<count;i++) { if(identfier[i].code==NUM) printf("%s\t\t\t%d\n",identfier[i].name,identfier[i].code); }}void show_operator(){
int i; printf("Operator\n"); printf("Name\t\t\tCode\t\t\n"); for(i=0;i<count;i++) { if(identfier[i].code>NUM) printf("%s\t\t\t%d\n",identfier[i].name,identfier[i].code); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -