📄 analysisproc.cpp
字号:
#include "string.h"
#include "stdlib.h"
#include "stdio.h"
#include "iostream"
#define MAX_LENGTH 120
using namespace std;
int index = 0;
char constable[100][MAX_LENGTH];
int constcount = 0;//常数表
char GetChar(char *str){
char c;
c = str[index++];
return c;
}
int Oct2Dec(char *str){//转换8进制为10进制
int i = 0, j = 0;
int c = 1,sum = 0;
char ch;
ch = *(++str);
while(ch != '\0'){
i++;
ch = *(++str);
}
ch =*(--str);
for(j = i;j > 0;j--){
ch -='0';
sum+=c*ch;
c*=8;
ch = *(--str);
}
return sum;
}
int Hex2Dec(char *str){//转换16进制为10进制
int i = 0, j = 0;
int c = 1,sum = 0;
char ch;
str+=2;
ch = *str;
while(ch != '\0'){
i++;
ch = *(++str);
}
ch =*(--str);
for(j = i;j > 0;j--){
if(ch >= '0'&&ch <='9')
ch -='0';
else if(ch >= 'a'&&ch <='f'){
ch -='a';
ch+=10;
}
else if(ch >= 'A'&&ch <='F'){
ch -='A';
ch+=10;
}
sum+=c*ch;
c*=16;
ch = *(--str);
}
return sum;
}
int InsertConst(char* strToken){
strcpy(constable[constcount++],strToken);
puts(constable[constcount]);
return constcount;
}
void insertID(){
}
void ErrorProc(){
}
int Reserve(char *str){//检查是否为保留字并返回保留字的编号
int i;
char *ResTable[] = {""
,"if"
,"then"
,"else"
,"while"
,"do"}
,**pt;
pt = ResTable;
for(i = 1;i < 6;i++){
if(strcmp(pt[i],str) == 0)
return i;
}
return 0;
}
int scan(){
int code,value;
char ch;
char strToken[MAX_LENGTH]={'\0'};
char str[MAX_LENGTH] = "12 asdf 0x if 21 ij ";
int i = 0;
/*
FILE *fp;
fp = fopen("c:\123.txt","r");
if(!fp){
printf("Can not open file! Program exit\n");
exit(1);
}
*/
ch = GetChar(str);
do{
if(ch >='1'&&ch<='9'){ //如果为十进制数
strToken[i++] = ch;
ch = GetChar(str);
while(ch >='0'&&ch<='9'){ //如果是十进制的数的后n位继续填入
strToken[i++] = ch;
ch = GetChar(str);
}
strToken[i] = '\0';//如果是其他字符尾部置空
printf("INT10\t%s\n",strToken);
i = 0;
// InsertConst(strToken);
}
else if(ch == '0'){
strToken[i++] = ch;
ch = GetChar(str);
if(ch == 'x'){//HEX的处理
strToken[i++] = ch;
ch = GetChar(str);
while((ch >= '0'&& ch <= '9')||(ch >='a'&&ch <='f')){
strToken[i++] = ch;
ch = GetChar(str);
}
strToken[i] = '\0';
i = 0;
printf("INT16\t%d\n",Hex2Dec(strToken));
continue;
// InsertConst(strToken);
}
else if(ch >= '0'&& ch <= '7'){ //处理8进制问题
strToken[i++] = ch;
ch = GetChar(str);
while(ch >= '0'&& ch <= '7'){
strToken[i++] = ch;
ch = GetChar(str);
}
strToken[i] = '\0';
i = 0;
printf("INT8\t%d\n",Oct2Dec(strToken));
continue;
//InsertConst(strToken);
}
else
strToken[i] = '\0';
i = 0;
printf("INT10\t%s\n",strToken);
//InsertConst(strToken);
}
else if(ch == '+'||ch == '-'||ch == '*'||ch == '/'||ch == '>'||ch == '<'||ch == '='||ch == '('|| ch == ')'){
strToken[i++] = ch;//处理运算符
strToken[i] = '\0';
printf("%s\t_\n",strToken);
i = 0;
ch = GetChar(str);
}
else if((ch >= 'a'&& ch <= 'z')||(ch >='A'&&ch <= 'Z')){
strToken[i++] = ch;
ch = GetChar(str);
while((ch >= 'a'&& ch <= 'z')||(ch >='A'&&ch <= 'Z')){ //如果是字母继续填入后n位
strToken[i++] = ch;
strToken[i]= '\0';
if(code = Reserve(strToken))
break;// 如果是保留字跳出单词的循环
ch = GetChar(str);
}
strToken[i] = '\0';//如果是其他字符尾部置空
if(code){//对于保留字的处理
printf("Reserve\t%s\n",strToken);
ch = GetChar(str);
}
else
printf("IDN\t%s\n",strToken);
i = 0;
code = 0;
}
else if(ch == ' '){
ch = GetChar(str);
}
}while(ch);
return 0;
}
void main(){
char s[100];
scan();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -