1

i'm trying to make a small lexical analyser that recognizes numbers as it is in the following regular expression:

enter image description here

#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];
}

enter image description here

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

enter image description here enter image description here

  • Edit the question to include those warnings. – Harith Dec 16 '22 at 18:39
  • 1
    ```if (k[0]==('0'||'1'||'2'||'3'||'4'||'5'||'6'||'7'||'8'||'9'))```. That is not how C works. The expression inside the braces will be evaluated first, and then it's result will be compared to ```k[0]```. – Harith Dec 16 '22 at 18:40
  • You have the same problem in the rest of the code. – Harith Dec 16 '22 at 18:46
  • 1
    ... and the expression inside the parentheses has the value 1, as was mentioned the last time you asked this same question. In C, `a || b` returns true (i.e. 1) or false (i.e. 0), depending on whether `a` and `b` are true (i.e. anything other than 0) or false (i.e. 0). Since `'1'` is 49, which is not 0, the value of the expression is 1. But `k[0]` is not equal to 1. It's `'5'` (53). – rici Dec 16 '22 at 18:47
  • @Haris i deleted the braces and the code runs just for the + and - signs and the code breaks after entering the if section – Houssam Eddine Dec 16 '22 at 18:55
  • @rici i got that and deleted the last post since the pointers distracted me i also edited the code now and it breaks for some reason – Houssam Eddine Dec 16 '22 at 18:57
  • Deleting the parenthesis is equivalent to saying ```(k[0] == '0') || `1' || `2`...```, which computed the results of the expressions ```k[0] == '0'``` and then applies to logical ```OR``` to the rest. You have to explicitly test for each of them: ```if ((k[0] == `1) || (k[0] == '2')```. – Harith Dec 16 '22 at 18:58
  • @Haris in fact i did that yeah but the code still breaks – Houssam Eddine Dec 16 '22 at 18:59
  • Why are you using a global array? And then passing it around the rest of the code, when it's visible to all the functions? – Harith Dec 16 '22 at 19:00
  • @Haris and how should i use it ? – Houssam Eddine Dec 16 '22 at 19:03
  • See this question for some insights. https://stackoverflow.com/questions/62794734/global-variables-as-arguments-in-function-c – Harith Dec 16 '22 at 19:03
  • 1
    And it's a poor SO etiquette to edit the question after it has received comments/answers, and thus invalidate them all. – Harith Dec 16 '22 at 19:04
  • @Houssam: One other problem you have is that `substr` does nothing. (Also, its name is completely misleading, even if it did what you probably thought it would do.) – rici Dec 16 '22 at 19:08
  • @rici yeah it seems the problem is in the substr function i mean by substr a function that take a string and make sub string without the first letter of the old string so i can easily traverse the string – Houssam Eddine Dec 16 '22 at 19:11
  • @HoussamEddine: calling the next function with the argument `s+1` would have that effect, much more directly. And would probably be easier for everyone to understand. – rici Dec 16 '22 at 19:13
  • @rici i deleted the substr function and call the Qi functions with k+1 but the code still break the call now look like return Q1(nextchar(k+1)); and the function look like: char nextchar(char *C){ return (*C)++ } – Houssam Eddine Dec 16 '22 at 19:25
  • That will produce more compiler warnings. You can't just make random changes. Think things through. If types don't match, you haven't thought it out correctly. (In this case, I meant `Q1(k+1)`, as a hint. But you need to get clear in your head how pointers work. Get out a big piece of paper and a pencil, and pretend you're the computer, and run your program step by step.) – rici Dec 16 '22 at 21:09

0 Answers0