Created
January 7, 2016 15:22
-
-
Save krysseltillada/7e14412f8a12c759fabf to your computer and use it in GitHub Desktop.
syntax checker (ex 1 -24 ) (the c programming language book 2nd ed)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void scanComment (int); | |
void scanBraces (); | |
void scanParen (); | |
void scanDquote (); | |
void scanSquote (); | |
int main () | |
{ | |
int ch; | |
while ( (ch = getchar ()) != EOF ) { | |
if (ch == '/') | |
scanComment(ch); | |
if (ch == '{') | |
scanBraces(); | |
if (ch == '(') | |
scanParen(); | |
if (ch == '"') | |
scanDquote (); | |
if (ch == 39) | |
scanSquote (); | |
} | |
return 0; | |
} | |
void scanComment(int c) { | |
int ch = c; | |
int ch3; | |
if (ch == '/' && getchar () == '*') { | |
while ( (ch3 = getchar ()) != EOF) { | |
if (ch3 == '*') { | |
if ( (ch3 = getchar ()) == '/'); | |
return; | |
} | |
if (ch3 == ' ' && getchar () == '\n' || | |
ch3 == '\n') | |
break; | |
} | |
} | |
printf ("\nuncomplete comment"); | |
} | |
void scanBraces() { | |
int ch1; | |
while ( (ch1 = getchar ()) != EOF) { | |
if (ch1 == '}') | |
return; | |
if (ch1 == '(') | |
scanParen (); | |
if (ch1 == '"') | |
scanDquote (); | |
if (ch1 == 39) | |
scanSquote (); | |
if (ch1 == ' ' && getchar () == '\n' || | |
ch1 == '\n') | |
break; | |
} | |
printf ("\nuncomplete braces"); | |
} | |
void scanParen () { | |
int ch1; | |
while ( (ch1 = getchar ()) != EOF ) { | |
if (ch1 == ')') | |
return; | |
if (ch1 == '"') | |
scanDquote (); | |
if (ch1 == 39) | |
scanSquote (); | |
if (ch1 == ' ' && ch1 == '\n' || | |
ch1 == '\n') | |
break; | |
} | |
printf ("\nuncomplete parenthesis"); | |
} | |
void scanDquote () { | |
int ch1; | |
while ( (ch1 = getchar ()) != EOF ) { | |
if (ch1 == '"') | |
return; | |
if (ch1 == ' ' && ch1 == '\n' || | |
ch1 == '\n') | |
break; | |
} | |
printf ("\nuncomplete double quote"); | |
} | |
void scanSquote () { | |
int ch1; | |
int count = 0; | |
while ( (ch1 = getchar ()) != EOF ) { | |
if (ch1 == 39 && count == 1) | |
return; | |
++count; | |
if (ch1 == ' ' && ch1 == '\n' || | |
ch1 == '\n' || count > 1) | |
break; | |
} | |
printf ("\nuncomplete single quote"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment