Advertisement
naorzr

Untitled

Apr 6th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void skipcomment(void);
  5. void skipstring(void);
  6. int getnext(void);
  7. void readline(void);
  8. int matchbracket(char);
  9. int addbracket(char);
  10. int openbrackets(int);
  11. void cleanstack(void);
  12.  
  13. #define MAXLINE 101
  14. #define START 1
  15. #define COMMENT_START '\'
  16. #define NEW_LINE '\n'
  17. #define STRING '"'
  18. #define END 2
  19. #define RUNNING 1
  20.  
  21. int main(){
  22.    
  23.     while(c=getnext() != EOF)
  24.         switch(c){
  25.             case STRING:
  26.                 skipstring();
  27.                 break;
  28.                
  29.             case COMMENT_START:
  30.                 if((c=getnext()) == '*')
  31.                     skipcomment();
  32.                 break;
  33.                
  34.             case '{': case '(': case '[':
  35.                 addbracket(c);
  36.                 break;
  37.                
  38.             case '}': case ')': case ']':
  39.                 if(!bracketmatch(c))
  40.                     printerr();
  41.                 break;
  42.                
  43.             case NEW_LINE:
  44.                 if(openbrackets(RUNNING))
  45.                     printerr();
  46.                 cleanstack();
  47.                 break;
  48.         }
  49.     if(openbrackets(END))
  50.         printerr();
  51. }
  52.  
  53.  
  54.  
  55. char line[MAXLINE+1];
  56. int ind = START;
  57.  
  58.  
  59. char getnext(){
  60.     if(ind==START || ind>MAXLINE || line[ind] == '\0')
  61.         readline();
  62.     return line[ind++];
  63. }
  64.  
  65. void readline(){
  66.     for(ind = START ; ind <= MAXLINE && (line[ind] = getchar()) != '\n' && line[ind] != EOF ; ind++)
  67.         ;
  68.     line[++ind] = '\0';
  69.     ind = START;
  70. }
  71.  
  72. void skipstring(){
  73.     char c;
  74.     while((c=getnext()) != '"')
  75.         if(c=='\\' && (c=getnext()) == '"')
  76.             continue;
  77. }
  78.  
  79. void skipcomment(){
  80.     char c;
  81.     while((c=getnext()))
  82.         if(c == '*'){
  83.             while(c == '*')     /* skip all asterisks */
  84.                 c = getnext();
  85.             if(c == '/')        /* if closed with an '/' thats the end of the comment */
  86.                 return;
  87.         }
  88. }
  89.  
  90. char brackstack[MAXLINE];
  91. int k = START;
  92. int c_bracket = 0;      /* holds the amount of curly/round/square brackets */
  93.  
  94.  
  95. void addbracket(char brack){
  96.     if(brack == '[')
  97.         brackstack[k++] = ']';
  98.     else if(brack == '(')
  99.         brackstack[k++] = ')';
  100.     else
  101.         c_bracket++;
  102. }
  103.  
  104. int matchbracket(char brack){
  105.     if(k<START)
  106.         return 0;
  107.     if(brack == ']' || brack == ')')
  108.         return brack == brackstack[k]?k--:FALSE;
  109.     else if(c_bracket<=0)
  110.         return 0;
  111.     else {
  112.         c_bracket++;
  113.         return 1;
  114.     }
  115. }
  116.  
  117. void cleanstack(){
  118.     k = START; 
  119. }
  120.  
  121. int openbrackets(int state){
  122.     if(state == RUNNING)
  123.         return k != START;
  124.     else if(state == END)
  125.         return k != START && c_bracket==0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement