error detection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rola248
    New Member
    • Jul 2009
    • 8

    error detection

    I'd like to ask,if i have some equations in a text file such as:
    1) 2+45-43
    2) 28+50-3
    3) 12++36
    i need a function that detects the error (eq. 3) when reading the file.is there any possible way to do this in c????
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Certainly this kind of syntax checking can be done in C. However, you need to carefully and completely specify the grammer.

    For example, the unary minus (negation) can cause a lot of confusion. Do you want these expressions to be legal?
    12+-36 (12 plus -36)
    4*-8 (4 times -8)

    Grammar checking can involve pretty complicated C code. You might want to look at tools made specifically for that job (such as lex or flex) or a language that reduces the edit/build/test/fix cycle time (such as perl).

    Comment

    • rola248
      New Member
      • Jul 2009
      • 8

      #3
      the first expression is not legal,it should be:
      12-36
      this is what came to my mind,i know it's not efficient because it doesn't take (,) into consideration,i haven't heard of (lex and flex) before,how can i use them for this purpose??
      void finderror(FILE *in,char line[])
      {
      int i;
      int max=countequ(li ne,in);

      for(i=0;i<max;i ++)
      {read(line,in);
      if ((line[i]=='+' &&line[i+1]=='+') || (line[i]=='+' &&line[i+1]=='-') || (line[i]=='+' &&line[i+1]=='*') || (line[i]=='+' &&line[i+1]=='/') || (line[i]=='*' &&line[i+1]=='*') |(line[i]=='*' &&line[i+1]=='+') || (line[i]=='*' &&line[i+1]=='-') || (line[i]=='*' &&line[i+1]=='/')|| (line[i]=='-' &&line[i+1]=='-') || (line[i]=='-' &&line[i+1]=='+') || (line[i]=='-' &&line[i+1]=='*') || (line[i]=='-' &&line[i+1]=='/') || (line[i]=='/' &&line[i+1]=='/') || (line[i]=='/' &&line[i+1]=='+') || (line[i]=='/' &&line[i+1]=='-') || (line[i]=='/' &&line[i+1]=='*'))
      printf("ERROR IN LINE : %d",i+1);
      else
      printf("ALL SUCCESSFUL");
      }
      }

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Look here for details on lex and flex. lex was the original program; flex is a free (but non-Gnu) version.

        Comment

        Working...