0

When I read from file I want to check the word do not contains the number, for example, 2sum or 23b2 but is OK if the file read the mathematical operation like

sum = x + 5

I try to put inside the if statement [a-zA-Z]* put it does not work.

This part of my code:

#include <stdio.h> 
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int charClass;
char lexeme [100]; 
void addChar(); 
void getChar();
void getNonBlank(); 
int lex();

int main() {
  if ((in_fp = fopen("/Users/Desktop/ff.txt", "r")) == NULL) 
    printf("ERROR - cannot open front.in \n"); 
  else {
    getChar(); 
    do {
      if(strcmp(lexeme, "[a-zA-Z]*") == 0){
        printf("error");
        break;
      }
      lex(); 
    } while (nextToken != EOF); 
  }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
saas
  • 71
  • 1
  • 1
  • 6
  • 4
    looks like you are trying to use a regular expression. strcmp just does a straight string compare. See [here](https://stackoverflow.com/questions/1085083/regular-expressions-in-c-examples) for examples – OldProgrammer Mar 21 '20 at 17:07
  • 1
    `strcmp` does not work like that: the string has to be literally `"[a-zA-Z]*"` to match. – Weather Vane Mar 21 '20 at 17:07
  • lexeme is never set BTW, so contains null characters because global, in_fp is set / file is open for nothing – bruno Mar 21 '20 at 17:08
  • if the strcmp doesn't work what should i put i am Beginners in c language – saas Mar 21 '20 at 17:09
  • look at the function _isalpha_ iterating on your lexeme's characters, or use "isupper(c) || islower(c)" where _c_ is the characters of the lexeme in case additional characters are added for your locale – bruno Mar 21 '20 at 17:13
  • i try if (isalpha(lexeme) == 0) put give me error " incompatible pointer to integer conversion passing 'char [100]' to parameter of type 'int' " – saas Mar 21 '20 at 17:18
  • 1
    isalpha must be applied on an int, not on a string, this is why you need to iterate on your string. Anyway the lexeme 5 in your example is a valid lexeme, your global problem is more complicated. How do you read formula ? the couple bison / flex can help you – bruno Mar 21 '20 at 17:19
  • my program about a lexical analyzer system for simple arithmetic expressions you said isalpha must be applied in int not string how is this work? – saas Mar 21 '20 at 17:24
  • again you have to iterate on each character of lexeme and to apply isalpha on each character and decide what to do depending on the result. What is the complexity of the expression you have to manage ? what about to look at flex / bison to help you if any complexity ? – bruno Mar 21 '20 at 17:25
  • you have any resources can explain this point – saas Mar 21 '20 at 17:27
  • and this my code https://github.com/huichen-cs/sebesta/blob/master/parser/front.c – saas Mar 21 '20 at 17:33
  • Anyone have answer – saas Mar 21 '20 at 19:59

1 Answers1

0

Regarding:

if(strcmp(lexeme, "[a-zA-Z]*") == 0){

This is comparing the char array [a-zA-Z]* to the array pointed at by lexeme

There are two ways to approach this problem.

First, use an regular expression, which requires several complexities that will just confuse you (or me)

Second, iterate over the expression pointed to by: lexeme as in:

for( size_t i=0; lexeme[i]; i++ )
{
    if( isalpha( lexeme[i] )
    {
        ....
    }
}

The functions in ctype.h work on indivdual int values and when referencing: lexeme[i] that single character gets prompted to an 'int'

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • it is work but the problem when see any number the program stop like x = 5 i do not know why it is stop must not stop – saas Mar 22 '20 at 04:08
  • i do this for( size_t i=0; lexeme[i]; i++ ){ if((!isalpha(lexeme[0]) ) && (isdigit (lexeme[i])) ){ printf("erreo "); exit(0); } else{ }. but the problem here when see the number is stop how i do if just the first number – saas Mar 22 '20 at 05:39
  • the function: `isdigit()` says the digit is a number. So that likely will not work for what your looking to accomplish – user3629249 Mar 23 '20 at 02:14