i'm trying to make a small lexical analyser that recognizes numbers as it is in the following regular expression:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
char C[500];
bool temp;
bool Q0(char k[]);
bool Q1(char k[]);
bool Q2(char k[]);
bool Q3(char k[]);
void substr (char C[]);
char nextchar(char C[]);
int main()
{
printf("enter a string: ");
scanf("%s",C);
temp=Q0(C);
if (temp==true)
{
printf("it's number");
}
else
{
printf("false");
}
return 0;
}
bool Q0(char k[])
{
printf("%s\n",k);
printf("%c\n",k[0]);
if (k[0]=='+'||k[0]=='-')
{
return Q1(nextchar(k));
}
else if (k[0]=='0'||k[0]=='1'||k[0]=='2'||k[0]=='3'||k[0]=='4'||k[0]=='5'||k[0]=='6'||k[0]=='7'||k[0]=='8'||k[0]=='9')
{
return Q1(nextchar(k));
}
else
{
return false;
}
}
bool Q1(char k[])
{
if (k[0]=='0'||k[0]=='1'||k[0]=='2'||k[0]=='3'||k[0]=='4'||k[0]=='5'||k[0]=='6'||k[0]=='7'||k[0]=='8'||k[0]=='9')
{
return Q1(nextchar(k));
}
else if (k[0]=='.')
{
return Q2(nextchar(k));
}
else if (k[0]=='\0')
{
return true;
}
else
{
return false;
}
}
bool Q2(char k[])
{
if (k[0]=='0'||k[0]=='1'||k[0]=='2'||k[0]=='3'||k[0]=='4'||k[0]=='5'||k[0]=='6'||k[0]=='7'||k[0]=='8'||k[0]=='9')
{
return Q2(nextchar(k));
}
else if (k[0]=='e'||k[0]=='E')
{
return Q3(nextchar(k));
}
else
{
return false;
}
}
bool Q3(char k[])
{
if (k[0]=='+'||k[0]=='-')
{
return Q3(nextchar(k));
}
else if (k[0]=='0'||k[0]=='1'||k[0]=='2'||k[0]=='3'||k[0]=='4'||k[0]=='5'||k[0]=='6'||k[0]=='7'||k[0]=='8'||k[0]=='9')
{
return Q3(nextchar(k));
}
else if (k[0]=='\0')
{
return true;
}
else
{
return false;
}
}
void substr (char *C)
{
(C)++;
}
char nextchar(char *C){
substr(C);
return C[0];
}
why the code execute else section in the Q0 function ? in the result k[0] equals 5 and it should execute the else if(k[0] == ...'5'...) and go to the next state Q1 also i got lot of warnings about type casting and passing parameters but i can't understand why PS: i printed the whole string and the first letter of it in order to make sure that there isn't a problem with the array