C program to check syntax of ‘for’ loop

As defined by C standards, the for loop syntax is:

for (initialisation; condition; increment/decrement)
        ... 

Syntactically, there should be two semicolons, one opening parenthesis, one closing parenthesis, and correct spelling of “for”. Hence, to check only the syntax of for loop, what a compiler does is check the following conditions:

  • Only “for” is written, and not “For”, “FOR”, “foR” or any of its variants.
  • Total statement consists of two semicolons “;” before the closing parenthesis “)” ends.
  • Presence of an opening parenthesis “(” after the “for” keyword, and presence of a closing parenthesis “)” at the end of statement.

Examples:



Input : for (i = 10; i < 20 i++) 
Output : Semicolon Error

Input : for(i = 10; i < 20; i++
Output : Closing parenthesis absent at end 

Code –

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
  
//array to copy first three characters of string str
char arr[3]; 
  
void isCorrect(char *str)
{
      
    //semicolon, bracket1, bracket2 are used 
        //to count frequencies of
    //';', '(', and ')' respectively
    //flag is set to 1 when an error is found, else no error
    int semicolon = 0, bracket1 = 0, bracket2 = 0, flag = 0;
      
    int i;
    for (i = 0; i < 3; i++)
        arr[i] = str[i];
          
    //first 3 characters of the for loop statement is copied
    if(strcmp(arr, "for") != 0)
    {
        printf("Error in for keyword usage");
        return;
    }
      
    //Proper usage of "for" keyword checked
    while(i != strlen(str))
    {
        char ch = str[i++];
        if(ch == '(')
        {
            //opening parenthesis count
            bracket1 ++;
        }
        else if(ch == ')')
        {
            //closing parenthesis count
            bracket2 ++;
              
        }
        else if(ch == ';')
        {
            //semicolon count
            semicolon ++;
        }
        else continue;
          
    }
      
    //check number of semicolons
    if(semicolon != 2)
    
        printf("\nSemicolon Error");
        flag++;
    }
      
    //check closing Parenthesis
    else if(str[strlen(str) - 1] != ')')
    
        printf("\nClosing parenthesis absent at end");
        flag++;
    }
      
    //check opening parenthesis
    else if(str[3] == ' ' && str[4] != '(' )
    
        printf("\nOpening parenthesis absent after for keyword");
        flag++;
    }
      
    //check parentheses count
    else if(bracket1 != 1 || bracket2 != 1 || bracket1 != bracket2)
    
        printf("\nParentheses Count Error");
        flag++;
    }
      
    //no error 
    if(flag == 0)
        printf("\nNo error");
              
}
  
int main(void) {
      
    char str1[100] = "for (i = 10; i < 20; i++)";
    isCorrect(str1);
      
    char str2[100] = "for i = 10; i < 20; i++)";
    isCorrect(str2);
      
    return 0;
}

chevron_right


Output :

No error
Opening parenthesis absent after for keyword 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.