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 –
#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
filter_none
Output :
No error Opening parenthesis absent after for keyword
Recommended Posts:
- Introduction to Syntax Analysis in Compiler Design
- FIRST Set in Syntax Analysis
- FOLLOW Set in Syntax Analysis
- Syntax Directed Translation in Compiler Design
- C++ Program to implement Symbol Table
- C program to detect tokens in a C program
- Program to calculate First and Follow sets of given grammar
- Lex Program For checking a valid URL
- Compiler Design | Detection of a Loop in Three Address Code
- S - attributed and L - attributed SDTs in Syntax directed translation
- Lex program to count the number of lines, spaces and tabs
- Lex Program to accept a valid integer and float value
- Lex Program to check valid email
- Lex Program to accept string starting with vowel
- Lex program to find the length of the longest word
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