查找C语言中的基本语法错误,如圆括号,左括号,花括号不匹配等

时间:2021-12-13 06:15:51

版本一:

#include <stdio.h>

#define MAXLINE 1000 /* max input line size */
char line[MAXLINE]; /*current input line*/

int getline(void);  /* taken from the KnR book. */


int
main()
{
  int len=0;
  int t=0;
  int brace=0, bracket=0, parenthesis=0;
  int s_quote=1, d_quote=1;


  while ((len = getline()) > 0 )
    {
      t=0;
      while(t < len)
        {
          if( line[t] == '[')
            {
              brace++;
            }
          if( line[t] == ']')
            {
              brace--;
            }
          if( line[t] == '(')
            {
              parenthesis++;
            }
          if( line[t] == ')')
            {
              parenthesis--;
            }
          if( line[t] == '\'')
            {
              s_quote *= -1;
            }
          if( line[t] == '"')
            {
              d_quote *= -1;
            }
          t++;
        }
    }
  if(d_quote !=1)
    printf ("Mismatching double quote mark\n");
  if(s_quote !=1)
    printf ("Mismatching single quote mark\n");
  if(parenthesis != 0)
    printf ("Mismatching parenthesis\n");
  if(brace != 0)
    printf ("Mismatching brace mark\n");
  if(bracket != 0)
    printf ("Mismatching bracket mark\n");
  if( bracket==0 && brace==0 && parenthesis==0 && s_quote == 1 && d_quote == 1)
        printf ("Syntax appears to be correct.\n");
  return 0;
}


/* getline: specialized version */
int getline(void)
{
  int c, i;
  extern char line[];

  for ( i=0;i<MAXLINE-1 && ( c=getchar()) != EOF && c != '\n'; ++i)
    line[i] = c;
  if(c == '\n') 
    {
      line[i] = c;
      ++i;
    }
  line[i] = '\0';
  return i;

}

版本二:

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

#define MAX_STACK 1024

enum
{
    CODE,       /* nothing of the following */
    COMMENT,    /* inside a comment */
    QUOTE1,     /* inside '' */
    QUOTE2      /* inside "" */
};

int main(void)
{
    int ch;
    int state = CODE;
    char stack[MAX_STACK];
    size_t top = 0; /* points to the top of the stack :-) */
    size_t line = 1;
    int error = 0;  /* for ok-message */

    while ((ch = getchar()) != EOF)
    {
        if (ch == '\n')
        {
            line++;
        }

        switch (state)
        {
        case CODE:
            if (ch == '\'')
            {
                state = QUOTE1;
            }
            else if (ch == '"')
            {
                state = QUOTE2;
            }
            else if (ch == '/')
            {
                int second = getchar();

                if (second == '*')
                {
                    state = COMMENT;
                }
                else
                {
                    ungetc(second, stdin);
                }
            }
            else if (ch == '(' || ch == '[' || ch == '{')
            {
                if (top < MAX_STACK)
                {
                    stack[top++] = ch;
                }
                else
                {
                    printf("Stack too small!\n");
                    return EXIT_FAILURE; /* exit gracefully :-) */
                }
            }
            else if (ch == ')' || ch == ']' || ch == '}')
            {
                if (top == 0) /* found closing brace but stack is empty */
                {
                    printf("Line %lu: Closing '%c' found without "
                           "counterpart.\n", (unsigned long)line, ch);
                    error = 1;
                }
                else
                {
                    char open = stack[--top];

                    if ((ch == ')' && open != '(') ||
                        (ch == ']' && open != '[') ||
                        (ch == '}' && open != '{'))
                    {
                        printf("Line %lu: Closing '%c' does not match "
                             "opening '%c'.\n", (unsigned long)line, ch, open);
                        error = 1;
                    }
                }
            }
            break;
        case COMMENT:
            if (ch == '*')
            {
                int second = getchar();

                if (second == '/')
                {
                    state = CODE;
                }
                else
                {
                    ungetc(second, stdin);
                }
            }
            break;
        case QUOTE1:
            if (ch == '\\')
            {
                (void)getchar(); /* an escaped char inside '' throw it away */
            }
            else if (ch == '\'')
            {
                state = CODE;
            }
            break;
        case QUOTE2:
            if (ch == '\\')
            {
                (void)getchar(); /* an escaped char inside "" throw it away */
            }
            else if (ch == '"')
            {
                state = CODE;
            }
            break;
        }
    }

    if (state == COMMENT)
    {
        printf("Code ends inside comment!\n");
    }
    else if (state == QUOTE1)
    {
        printf("Code ends inside single quotes!\n");
    }
    else if (state == QUOTE2)
    {
        printf("Code ends inside double quotes!\n");
    }
    else if (top == 0 && error == 0)
    {
        printf("Code seems to be ok.\n");
    }
    if (top > 0) /* still something in the stack */
    {
        size_t i;
        for (i = 0; i < top; i++)
        {
            printf("Opening '%c' found without counterpart.\n", stack[i]);
        }
    }

    return 0;
}