i don't know why this program doesn't work.. it just keep on saying that it have errors..... pleae help!! please help!!
#include <stdio.h>
#define LEX_LEN 1024
enum chars{
LETTER, DIGIT, UNKNOWN,ZERO,TULDOK,
WHITESPACE,SEMICOLON,QUOTED,PLUS,MINUS,
DIVIDE,LT,GT,EQ,ASS2,OPARENT,CPARENT
};
enum Token {
STOP, NUM,
STRING,STMTSEP,QUOTED_CHAR,ERROR,ADDOPERATOR,MINUSOPERATOR,DIVIDEOPERATOR,SIGNEDNUM,
SEMI,LTHAN,GTHAN,EQUAL,ASS,OPEN_PARENT,CLOSE_PARENT
};
chars charclass;
char nextchar;
char parse_me[] = "helLO; 100; 1 + +1 \'a\' 1.1 - 1 /4 * <> = () ;
lun; hello ; 1.2a;" ;
long pcnt = 0;
char lexeme[LEX_LEN];
long lexcnt = 0;
void getChar ()
{
char curchar;
//get one char
curchar = parse_me [pcnt];
pcnt++;
//printf( "getchar: %c\n", curchar );
if (curchar >= 'a' &&
curchar <= 'z' ||
curchar >= 'A'&&
curchar <= 'Z')
{
charclass = LETTER;
nextchar = curchar;
}
else if (curchar >= '0' &&curchar <= '9' )
{
charclass = DIGIT;
nextchar = curchar;
}
else if (curchar =='\0' )
{
charclass = ZERO;
nextchar = curchar;
}
else if (curchar =='.')
{
charclass = TULDOK ;
nextchar = curchar;
}
else if (curchar =='+')
{
charclass = PLUS ;
nextchar = curchar;
}
else if (curchar =='*')
{
charclass = ASS2 ;
nextchar = curchar;
}
else if (curchar =='<')
{
charclass = LT ;
nextchar = curchar;
}
else if (curchar =='=')
{
charclass = EQ ;
nextchar = curchar;
}
else if (curchar =='>')
{
charclass = GT ;
nextchar = curchar;
}
else if (curchar =='-')
{
charclass = MINUS ;
nextchar = curchar;
}
else if (curchar =='/')
{
charclass = DIVIDE ;
nextchar = curchar;
}
else if (curchar ==';')
{
charclass = SEMICOLON ;
nextchar = curchar;
}
else if (curchar =='(')
{
charclass = OPARENT ;
nextchar = curchar;
}
else if (curchar ==')')
{
charclass = CPARENT ;
nextchar = curchar;
}
else if (curchar == '\'')
{
charclass = QUOTED;
nextchar = curchar;
}
else if
(curchar == '\n'||
curchar == '\t'||
curchar == ' ')
{
charclass = WHITESPACE;
nextchar = curchar;
}
else
{
charclass = UNKNOWN;
}
lun addChar ()
//printf( "addchar: %c\n", nextchar );
lexeme [lexcnt] = nextchar;
lexeme [lexcnt+1]='\0';
++lexcnt;
Token lookup (const char * s)
//fixme late
return STRING;
Token lex (){
lexcnt = 0;
getChar ();
while (charclass == WHITESPACE){
getChar ();}
switch (charclass){
case LETTER:
addChar ();
getChar();
while (charclass == LETTER ||
charclass == DIGIT){
addChar ();
getChar ();
}
pcnt--;
return lookup (lexeme);
break;
case DIGIT:
addChar ();
getChar ();
while (charclass == DIGIT )
{
addChar ();
getChar ();
printf( "inside first loop\n" );
}
if (charclass == TULDOK){
addChar ();
getChar ();
}
while ( charclass==DIGIT)
{
addChar ();
getChar ();
}
pcnt --;
return NUM;
break;
case SEMICOLON:
addChar ();
getChar ();
pcnt--;
return SEMI;
break;
case DIVIDE:
addChar ();
getChar ();
pcnt--;
return DIVIDEOPERATOR; break;
case LT:
addChar ();
getChar ();
pcnt--;
return LTHAN;
break;
case EQ:
addChar ();
getChar ();
pcnt--;
return EQUAL;
break;
case GT:
addChar ();
getChar ();
pcnt--;
return GTHAN;
break;
case ASS2:
addChar ();
getChar ();
pcnt--;
return ASS;
break;
case OPARENT:
addChar ();
getChar ();
pcnt--;return OPEN_PARENT;
break;
case CPARENT:
addChar ();
getChar ();
pcnt--;
return CLOSE_PARENT;
break;
case PLUS:
addChar ();
getChar ();
if (charclass == WHITESPACE )
{
addChar ();
getChar ();
pcnt--;
return ADDOPERATOR;
}
else if (charclass == DIGIT )
{
addChar ();
getChar ();
}
if (charclass == TULDOK){
addChar ();
getChar ();
}
while ( charclass==DIGIT)
{
addChar (); getChar ();
}
pcnt --;
return SIGNEDNUM;
break;
case MINUS:
addChar ();
getChar ();
if (charclass == WHITESPACE )
{
addChar ();
getChar ();
pcnt--;
return MINUSOPERATOR;
}
else if (charclass == DIGIT )
{
addChar ();
getChar ();
}
if (charclass == TULDOK){
addChar ();
getChar ();
}
while ( charclass==DIGIT)
{
addChar ();
getChar ();
}
pcnt --;
return SIGNEDNUM;break;
case ZERO:
return STOP;
break;
case QUOTED:
addChar ();
getChar ();
if (charclass==LETTER || charclass==DIGIT){
addChar ();
getChar ();
}
if (charclass==QUOTED){
addChar ();
getChar ();
pcnt--;
return QUOTED_CHAR;
}
return ERROR;
pcnt --;
case UNKNOWN:
return ERROR;
}
const char * num2tok (Token t)
switch (t)
{
case STRING:
return "STRING";case NUM:
return "NUM";
case STMTSEP:
return "STMTSEP";
case STOP:
return "STOP";
case QUOTED_CHAR:
return "QUOTED_CHAR";
case ADDOPERATOR:
return "ADDOPERATOR";
case MINUSOPERATOR:
return "MINUSOPERATOR";
case DIVIDEOPERATOR:
return "DIVIDEOPERATOR";
case SIGNEDNUM
return "SIGNEDNUM";
case SEMI:
return "SEMI";
case LTHAN:
return "LTHAN";
case GTHAN:
return "GTHAN";
case ASS:
return "ASS";
case OPEN_PARENT:
return "OPEN_PARENT";
case CLOSE_PARENT:
return "CLOSE_PARENT";
case EQUAL:
return "EQUAL";
case ERROR:
return "ERROR";
default:{
printf("%d", t);
return "UNKNOWN";
}
}
int main ()
//initialized lexeme
for (int i = 0; i < LEX_LEN; ++i )
lexeme [i] = '\0';
lexcnt = 0;
int i=1;
Token x;
while ((x=lex ())!=STOP)
{
printf ("Token: %s lexeme: %s\n", num2tok (x),lexeme);
++i;
}
return 0;