以下是scanner.h
#ifndef SCANNER_H
#define SCANNER_H
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<stdarg.h>
#include<math.h>
enum Token_Type
{
ORIGIN,SCALE,ROT,IS,TO,STEP,DRAW,FOR,FROM,T,SEMICO,L_BRACKET,R_BRACKET,COMMA,
PLUS,DETRACT,MUL,DIV,FUNC,ID,
NONTOKEN,
ERRTOKEN
};
typedef double(*MathFuncPtr)(double);
struct Token
{
Token_Type type;
char *lexeme;
double value;
double (*FuncPtr)(double);
};
static Token TokenTab[]=
{
{CONST_ID,"PI",3.1415926,NULL},//第30行
{CONST_ID,"E",2.71828,NULL},
{T,"T",0.0,NULL},
{FUNC,"SIN",0.0,NULL},
{FUNC,"COS",0.0,NULL},
{FUNC,"TAN",0.0,NULL},
{FUNC,"LN",0.0,NULL},
{FUNC,"EXP",0.0,NULL},
{FUNC,"SQRT",0.0,NULL},
{ORIGIN,"ORIGIN",0.0,NULL},
{SCALE,"SCALE",0.0,NULL},
{ROT,"ROT",0.0,NULL},
{IS,"IS",0.0,NULL},
{FOR,"FOR",0.0,NULL},
{FROM,"FROM",0.0,NULL},
{TO,"TO",0.0,NULL},
{STEP,"STEP",0.0,NULL},
{DRAW,"DRAW",0.0,NULL},
};
#endif
然后是main.cpp
#include "scanner.h"
#define LIMIT 100
/*函式宣告*/
int InitScanner(const char *address);
int ScanFile(FILE *file);
int PrintOut(int word[LIMIT][LIMIT],int i);
int JudgeWord(const char *IDString);
void CloseScanner(FILE *file);
static extern int WordLine[LIMIT][LIMIT]; //全域變數,存盤讀入的單詞
int main(int agrc,char *argv[])
{
int a=0;
const char *address=argv[1]; //創建檔案指標指向主函式第二個引數
FILE *file=fopen(address,"r");
InitScanner(file); //初始化詞法分析器 第20行
a=ScanFile(file); //獲取字符并存盤
PrintOut(WordLine[LIMIT][LIMIT],a); //分析并列印結果 第22行
CloseScanner(file); //關閉詞法分析器
return 0;
}
int InitScanner(char *address)
{
FILE *test_file;
test_file=fopen(address,"w");
if(!address) exit(1); //檔案不存在
if(test_file!=NULL) return 0; //檔案可用
else exit(1); //空檔案
}
int ScanFile(FILE *file)
{
int i=0,j=0;
while(WordLine[i][j]!=EOF)
{
WordLine[i][j]=fgetc(file);
if(WordLine[i][j]=' ')
{
WordLine[i][j]='\0';
i++;
j=0;
}
else j++;
}
return i;
}//回傳單詞數量
int PrintOut(int word[LIMIT][LIMIT],int i)
{
int j=0,k=0;
char WordCache[LIMIT]; //字符快取區,用于分析單詞
Token t;
for(j=0;j<=i;j++)
{
if(isalpha(word[j][k])) //為字母
{
while(WordCache[k]!='\0')
{
WordCache[k]=toupper(word[j][k]);
k++;
}
JudgeWord(WordCache);
continue;
}
else if(isdigit(word[j][k])) //為數字
{
double a;
int n=1;
while(isdigit(word[j][k]))
{
a=a*10+word[j][k];
k++;
}
if(word[j][k]!='\0')
{
if(word[j][k]!='.')
{
Token errortoken;
memset(&errortoken,0,sizeof(Token));
errortoken.type=ERRTOKEN;
printf("%5d,%10s,%5f,%10x\n",errortoken.type,errortoken.lexeme,errortoken.value,errortoken.FuncPtr);
}
else while(isdigit(word[j][k]))
{
a=a+word[j][k]/(pow(10,n));
n++;
}
}
memset(&t,0,sizeof(Token));
t.type=ID;
t.value=https://bbs.csdn.net/topics/a;
printf("%5d,%10s,%5f,%10x\n",t.type,t.lexeme,t.value,t.FuncPtr);
continue;
}
else //為標點
{
memset(&t,0,sizeof(Token));
switch(word[j][k])
{
case ";":t.type=SEMICO; break;//第108行
case "(":t.type=L_BRACKET; break;
case ")":t.type=R_BRACKET; break;
case ",":t.type=COMMA; break;
case "+":t.type=PLUS; break;
case "-":t.type=DETRACT; break;
case "*":t.type=MUL; break;
case "/":t.type=DIV; break;
default:t.type=ERRTOKEN; break;
}
printf("%5d,%10s,%5f,%10x\n",t.type,t.lexeme,t.value,t.FuncPtr);
}
}//for
return 0;
}
int JudgeWord(const char *IDString)
{
int loop;
for(loop=0;loop<sizeof(TokenTab)/sizeof(TokenTab[0]);loop++)
{
if(strcmp(TokenTab[loop].lexeme,IDString)==0)
{
printf("%5d,%10s,%5f,%10x\n",TokenTab[loop].type,TokenTab[loop].lexeme,TokenTab[loop].value,TokenTab[loop].FuncPtr);
return 0;
}
}
Token errortoken;
memset(&errortoken,0,sizeof(Token));
errortoken.type=ERRTOKEN;
printf("%5d,%10s,%5f,%10x\n",errortoken.type,errortoken.lexeme,errortoken.value,errortoken.FuncPtr);
return 0;
}
void CloseScanner(FILE *file)
{
if(file!=NULL) fclose(file);
}
然后出了大堆錯誤,我卻半天也沒搞清楚vc說的啥
main.cpp
e:\visualc++\msdev98\myprojects\scanner\scanner.h(30) : error C2065: 'CONST_ID' : undeclared identifier
e:\visualc++\msdev98\myprojects\scanner\scanner.h(30) : error C2440: 'initializing' : cannot convert from 'int' to 'enum Token_Type'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
e:\visualc++\msdev98\myprojects\scanner\scanner.h(30) : error C2440: 'initializing' : cannot convert from 'char [3]' to 'enum Token_Type'
There is no context in which this conversion is possible
e:\visualc++\msdev98\myprojects\scanner\scanner.h(30) : error C2440: 'initializing' : cannot convert from 'const double' to 'enum Token_Type'
Conversions between enumeration and floating point values are no longer allowed
e:\visualc++\msdev98\myprojects\scanner\scanner.h(30) : error C2440: 'initializing' : cannot convert from 'const int' to 'enum Token_Type'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
e:\visualc++\msdev98\myprojects\scanner\scanner.h(31) : error C2440: 'initializing' : cannot convert from 'int' to 'enum Token_Type'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
e:\visualc++\msdev98\myprojects\scanner\scanner.h(31) : error C2440: 'initializing' : cannot convert from 'char [2]' to 'enum Token_Type'
There is no context in which this conversion is possible
e:\visualc++\msdev98\myprojects\scanner\scanner.h(31) : error C2440: 'initializing' : cannot convert from 'const double' to 'enum Token_Type'
Conversions between enumeration and floating point values are no longer allowed
e:\visualc++\msdev98\myprojects\scanner\scanner.h(31) : error C2440: 'initializing' : cannot convert from 'const int' to 'enum Token_Type'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
e:\visualc++\msdev98\myprojects\scanner\main.cpp(13) : error C2159: more than one storage class specified
e:\visualc++\msdev98\myprojects\scanner\main.cpp(20) : error C2664: 'InitScanner' : cannot convert parameter 1 from 'struct _iobuf *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
e:\visualc++\msdev98\myprojects\scanner\main.cpp(22) : error C2664: 'PrintOut' : cannot convert parameter 1 from 'int' to 'int [][100]'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
e:\visualc++\msdev98\myprojects\scanner\main.cpp(108) : error C2051: case expression not constant
e:\visualc++\msdev98\myprojects\scanner\main.cpp(109) : error C2051: case expression not constant
e:\visualc++\msdev98\myprojects\scanner\main.cpp(110) : error C2051: case expression not constant
e:\visualc++\msdev98\myprojects\scanner\main.cpp(111) : error C2051: case expression not constant
e:\visualc++\msdev98\myprojects\scanner\main.cpp(112) : error C2051: case expression not constant
e:\visualc++\msdev98\myprojects\scanner\main.cpp(113) : error C2051: case expression not constant
e:\visualc++\msdev98\myprojects\scanner\main.cpp(114) : error C2051: case expression not constant
e:\visualc++\msdev98\myprojects\scanner\main.cpp(115) : error C2051: case expression not constant
e:\visualc++\msdev98\myprojects\scanner\main.cpp(117) : warning C4065: switch statement contains 'default' but no 'case' labels
執行 cl.exe 時出錯.
知道很長,所以希望有興趣的哥們指點一下,謝謝各位了
uj5u.com熱心網友回復:
不明覺厲地擼過
uj5u.com熱心網友回復:
求指教
uj5u.com熱心網友回復:
型別不匹配。。uj5u.com熱心網友回復:
樓上你真牛,8個月的帖子也能翻出來,估計樓主都忘了csdn的網址了。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/117242.html
標籤:基礎類
上一篇:C# 決議DLL檔案的問題
