1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
# include <stdio.h>
# include <string.h>
# include <ctype.h>
// ---------------
# define TOKEN_LENGTH 50
enum TokenType { UNRECOGNIZED, CHARACTER, DELIMITER, RESWORD, IDENT, NUM } ;
/* TokenType
DELIMITER
;
:
=
<
>
+
-
*
/
(
)
RESWORD
quit
*/
enum ResultType { ERROR, INT, BOOL } ;
int uTestNum ;
// ---------------
struct Token {
TokenType tokenType ;
char *str ;
} ;
struct NextToken {
bool exist ;
TokenType tokenType ;
char *str ;
} ;
// ---------------
Token GetToken() ;
bool IsDelimiter( char ch ) ;
NextToken Delimiter( Token token ) ;
// Command
int ScanWhiteSpace( char & ch ) ;
char *Char2Str( char ch ) ;
int ScanToEnter( char & ch ) ;
// --------------------
int main() {
scanf( "%d", & uTestNum ) ;
printf( "Program starts...\n" ) ;
printf( "> " ) ;
Token token = GetToken() ;
while ( strcmp( token.str, "quit" ) != 0 ) {
} // while
printf( "Program exits...\n" ) ;
return 0 ;
} // main()
// --------------------
Token GetToken() {
// TokenType { DELIMITER, RESWORD, IDENT, NUM }
static NextToken nextToken ; // CXX0017: Error: symbol "nextToken" not found
char ch ;
int scanNum = ScanWhiteSpace( ch ) ;
Token token ;
token.str = new char[TOKEN_LENGTH] ;
while ( scanNum != EOF ) {
if ( IsDelimiter( ch ) ) {
strcpy( token.str, Char2Str( ch ) ) ;
token.tokenType = DELIMITER ;
nextToken = Delimiter( token ) ;
return token ;
} // end if
/*
if ( IsResWord() ) {
} // end if
if ( IsIdent() ) {
} // end if
if ( IsNum() ) {
} // end if*/
else {
printf( "Unrecognized token with first char : '%c'\n", ch ) ;
scanNum = ScanToEnter( ch ) ;
} // end else
} // while
return token ;
} // GetToken()
// ----------
/*
DELIMITER
;
:
=
<
>
+
-
*
/
(
)
*/
bool IsDelimiter( char ch ) {
if ( ch == ';' || ch == ':' || ch == '=' || ch == '<' || ch == '>' || ch == '+' || ch == '-' || ch == '*'
|| ch == '/' || ch == '(' || ch == ')' ) {
return true ;
} // end if
else
return false ;
} // IsDelimiter()
// -----
NextToken Delimiter( Token token ) {
int scanNum ;
char ch ;
NextToken nextToken ;
nextToken.str = new char[TOKEN_LENGTH] ;
if ( token.str[0] == ':' ) {
scanNum = scanf( "%c", & ch ) ;
if ( ch == '=' ) { // ':=' legal
strcat( token.str, Char2Str( ch ) ) ;
} // end if
else { // ': ', ':+', ':+-*/', '::=', ':abc', ':123', ':quit' illegal
token.tokenType = UNRECOGNIZED ;
while ( scanNum != EOF && ch != '\t' ) {
scanNum = scanf( "%c", & ch ) ;
} // while
} // end else
nextToken.exist = false ;
return nextToken ;
} // end if
else if ( token.str[0] == '<' ) {
scanNum = scanf( "%c", & ch ) ;
if ( scanNum == EOF || ch == ' ' || ch == '\n' || ch == '\t' ) { // '< '
nextToken.exist = false ;
return nextToken ;
} // end if
else if ( ch == '>' ) { // '<>'
strcat( token.str, Char2Str( ch ) ) ;
nextToken.exist = false ;
return nextToken ;
} // end else if
else if ( ch == '=' ) { // '<='
strcat( token.str, Char2Str( ch ) ) ;
nextToken.exist = false ;
return nextToken ;
} // end else if
else {
if ( IsDelimiter( ch ) ) { // '<+', '<-123', '<+-*/', '<:>'
nextToken.tokenType = DELIMITER ;
} // end if
else if ( isdigit( ch ) ) { // '<123'
nextToken.tokenType = NUM ;
} // end else if
else { // '<abc', '<q', '<quit'
nextToken.tokenType = CHARACTER ;
} // end else
nextToken.exist = true ;
strcpy( nextToken.str, Char2Str( ch ) ) ;
return nextToken ;
} // end else
} // end else if
else if ( token.str[0] == '>' ) {
return nextToken ;
} // end else if
else { // ( token.str[0] == ';' || token.str[0] == '=' || token.str[0] == '+' || token.str[0] == '-'
// || token.str[0] == '*' || token.str[0] == '/' || token.str[0] == '(' || token.str[0] == ')' )
nextToken.exist = false ;
return nextToken ;
} // end else
} // Delimiter()
// --------------------
int ScanWhiteSpace( char & ch ) {
int num = EOF ;
num = scanf( "%c", & ch ) ;
if ( num == EOF )
return num ;
while ( num != EOF &&
( ch == ' ' || ch == '\n' || ch == '\t' ) ) {
num = scanf( "%c", & ch ) ;
} // while
return num ;
} // ScanWhiteSpace()
// -----
char *Char2Str( char ch ) {
char *str = new char[2] ;
sprintf( str, "%c", ch ) ;
return str ;
} // Char2Str()
// -----
int ScanToEnter( char & ch ) {
int num = scanf( "%c", & ch ) ;
while ( num != EOF && ch != '\n' ) {
num = scanf( "%c", & ch ) ;
} // while
return num ;
} // scanToEnter()
|