# # ifndef SCANNER_H
# define SCANNER_H
#include
#include
#include
#include
#include
#include
Enum Token_Type
{
ORIGIN, SCALE, ROT, IS, TO the STEP, the DRAW, FOR, FROM, T, SEMICO, L_BRACKET, R_BRACKET, COMMA,
PLUS, DETRACT the MUL, DIV, FUNC, ID,
NONTOKEN,
ERRTOKEN
};
Typedef double MathFuncPtr (*) (double);
Struct Token
{
Token_Type type;
Char * lexeme.
A double value.
Double (* FuncPtr) (double);
};
The static Token TokenTab []=
{
{CONST_ID, "PI", 3.1415926, NULL},// line 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},
{the FOR, "FOR", 0.0, NULL},
{the FROM, "FROM", 0.0, NULL},
{the TO, "TO", 0.0, NULL},
{STEP, "STEP", 0.0, NULL},
{the DRAW, "DRAW", 0.0, NULL},
};
# endif
and then is the main CPP
# include "scanner. H"
# define LIMIT 100
Function declaration/* */
Int InitScanner (const char * address);
Int ScanFile (FILE * FILE);
Int PrintOut (int the word [LIMIT] [LIMIT], int I);
Int JudgeWord (const char * IDString);
Void CloseScanner (FILE * FILE);
The static extern int WordLine [LIMIT] [LIMIT];//global variables, store the words read
Int main (int agrc, char * argv [])
{
Int a=0;
Address=const char * argv [1].//create a file pointer to the main function of the second parameter
The FILE * FILE=fopen (address, "r");
InitScanner (file);//initialize the lexical analyzer line 20
A=ScanFile (file);//get characters and store
PrintOut (WordLine [LIMIT] [LIMIT], a);//analysis and print the results 22
CloseScanner (file);//close the lexer
return 0;
}
Int InitScanner (char * address)
{
The FILE * test_file;
Test_file=fopen (address, "w");
if(! Address) exit (1);//file does not exist
If (test_file!=NULL) return 0;//file available
The else exit (1);//empty file
}
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;
}
The else j++;
}
return i;
}//returns the number words
Int PrintOut (int the word [LIMIT] [LIMIT], int I)
{
Int j=0, k=0;
Char WordCache [LIMIT];//character buffer, used to analyze the words
Token t;
for(j=0; j<=I; J++)
{
If (isalpha (word [j] [k]))//to letter
{
While (WordCache [k]!='\ 0')
{
WordCache [k]=toupper (word [j] [k]);
K++;
}
JudgeWord (WordCache);
continue;
}
Else if (isdigit (word [j] [k]))//for the digital
{
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 (& amp; Errortoken, 0, sizeof (Token));
Errortoken. Type=ERRTOKEN;
Printf (" % % 5 d, 10 s, % 5 f, 10 x % \ n ", errortoken. Type, errortoken, lexeme, errortoken. Value, errortoken. FuncPtr);
}
The else while (isdigit (word [j] [k]))
{
A=a + word [j] [k]/(pow (10, n));
n++;
}
}
Memset (& amp; T, 0, sizeof (Token));
T.t ype=ID;
t.value=https://bbs.csdn.net/topics/a;
Printf (" % % 5 d, 10 s, % 5 f, 10 x % \ n ", t.t ype, t.l exeme, t.v alue of t.F uncPtr);
continue;
}
The else//for punctuation
{
Memset (& amp; T, 0, sizeof (Token));
Switch (word [j] [k])
{
Case ";" : t.t ype=SEMICO; break;//line 108
A case of "(" : t.t ype=L_BRACKET; break;
Case ") ": t.t ype=R_BRACKET; break;
Case ", ": t.t ype=COMMA; break;
The case "+" : t.t ype=PLUS; break;
Case "-" : t.t ype=DETRACT; break;
A case of "*" : t.t ype=the MUL; break;
Case "/" : the t.t ype=DIV; break;
Default: t.t ype=ERRTOKEN; break;
}
Printf (" % % 5 d, 10 s, % 5 f, 10 x % \ n ", t.t ype, t.l exeme, t.v alue of t.F uncPtr);
}
}//for
return 0;
}
Int JudgeWord (const char * IDString)
{
Int loop;
For (loop=0; Loop
If (STRCMP (TokenTab [loop] lexeme, IDString)==0)
{
Printf (" % % 5 d, 10 s, % 5 f, 10 x % \ n ", TokenTab [r]. Loop type, TokenTab [loop] lexeme, TokenTab [loop] value, TokenTab [loop]. FuncPtr);
return 0;
}
}
Token errortoken;
Memset (& amp; Errortoken, 0, sizeof (Token));
Errortoken. Type=ERRTOKEN;
Printf (" % % 5 d, 10 s, % 5 f, 10 x % \ n ", errortoken. Type, errortoken, lexeme, errortoken. Value, errortoken. FuncPtr);
return 0;
}
Void CloseScanner (FILE * FILE)
{
If (the file!=NULL) fclose (file);
}
and then a lot of mistakes, but she didn't understand a half-day vc I say what
The main. CPP
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull