Joined: Sat Jan 24, 2009 1:04 pm Posts: 1
|
|
What is the c++ code for recognizing a valid c++ identifier? there are some conditions to be followed: 1. it should start with a letter or an underscore 2. no two succeeding underscore. 3.the length of the identifier is from 1 to 8 characters only. 4. a single underscore inputted as an identifier is invalid.
here are the codes that i coded that are very much familiar with the problem. all i need is to combine them as one code that solves the problem above.:
/*THIS IS A SIMPLE CODE TO RECOGNIZE AN IDENTIFIER*/ #include <stdio.h> #include <ctype.h> #include <iostream> #include <stdlib.h> #include <time.h> #include <conio.h> #include <math.h> using namespace std;
void error(); /*THIS IS A SIMPLE CODE TO RECOGNIZE AN IDENTIFIER*/ char in; void error() {
in=getchar();
if(isalpha(in)) in=getchar(); else {error();} while(isalpha(in)||isdigit(in)) in=getchar();
} int main () { error(); { getch(); } }
;;;;;;;;;;;;;; /*THIS IS A SIMPLE CODE that RECOGNIZE a REAL NUMBER*/ #include <ctype.h> #include <iostream> #include <stdlib.h> #include <time.h> #include <conio.h> #include <math.h> using namespace std; void error(); /*THIS IS A SIMPLE CODE that RECOGNIZE a REAL NUMBER*/ char in; void error() { in=getchar(); if(in=='+'||in=='-') in=getchar(); while (isdigit(in)) in=getchar(); if(in=='.') in=getchar(); else error(); if(isdigit(in)) in=getchar(); else error(); while(isdigit(in)) in=getchar(); cout<<"ok\n"; } int main () { error(); { getch(); } } ;;;;;;;;;;;;;;; /*THIS IS A SIMPLE CODE That implements a RECOGNIZER for AN IDENTIFIER*/ #include <ctype.h> #include <iostream> #include <stdlib.h> #include <time.h> #include <conio.h> #include <math.h> using namespace std;
/*THIS IS A SIMPLE CODE That implements a RECOGNIZER for AN IDENTIFIER*/ void error(); int main() { int state; char in;
state=1; in=getchar(); while(isalpha(in)||isdigit(in)) { switch(state) { case 1:if(isalpha(in)) state=2; else error(); break; case 2:state=2; break; } in=getchar(); } return(state==2); { getch(); } } void error() { getch(); }
|
|