diff --git a/CMakeLists.txt b/CMakeLists.txt index fba36ad..3805c22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.0) project(learningc LANGUAGES C) set(CHAPTER 1) -set(EXERCISE 23) +set(EXERCISE 24) add_executable(learningc chapters/${CHAPTER}/exercises/${EXERCISE}.c) diff --git a/chapters/1/exercises/24.c b/chapters/1/exercises/24.c new file mode 100644 index 0000000..dd4ac66 --- /dev/null +++ b/chapters/1/exercises/24.c @@ -0,0 +1,22 @@ +#include +#include "shared/functions.h" +#include "shared/functions/synthaxcheck.c" + +int main() { + char errors[6][30] = { + "none", + "unmatched round bracket", + "unmatched curly bracket", + "unmatched single quote", + "unmatched double quote", + "unmatched square bracket" + }; + int result; + if ((result = synthaxcheck()) == 0) { + printf("No synthax errors found!\n"); + return 0; + } else { + printf("Sythax error found at line %i column %i\nReason: %s\n", unmline, unmcol, errors[result]); + return result; + } +} diff --git a/chapters/1/exercises/24.c~ b/chapters/1/exercises/24.c~ new file mode 100644 index 0000000..09732d3 --- /dev/null +++ b/chapters/1/exercises/24.c~ @@ -0,0 +1,22 @@ +#include +#include "shared/functions.h" +#include "shared/functions/synthaxcheck.c" + +int main() { + char errors[6][30] = { + "none", + "unmatched round bracket", + "unmatched curly bracket", + "unmatched single quote", + "unmatched double quote", + "unmatched square bracket" + }; + int result; + if ((result = synthaxcheck()) == 0) { + printf("No synthax errors found!\n"); + return 0; + } else { + printf("Sythax error found at line %i column %i\nReason: %s\n", unmline, unmcol, errors[result]); + return result; + } +}//) diff --git a/chapters/1/exercises/shared/functions.h b/chapters/1/exercises/shared/functions.h index 6abc644..63ff830 100644 --- a/chapters/1/exercises/shared/functions.h +++ b/chapters/1/exercises/shared/functions.h @@ -4,7 +4,8 @@ float convert2cels(int fahr); void reverse(char s[MAXLINE], char d[strlen(s)]); void copy(char to[], char from[]); int getlines(char line[], int maxline); -int detab(char s[],int lim, int col); +int detab(char s[], int lim, int col); int entab(char s[], int lim, int col); int fold(char s[], int lim, int cpr); -int uncomment(char s[],int lim); +int uncomment(char s[], int lim); +int synthaxcheck(void); diff --git a/chapters/1/exercises/shared/functions.h~ b/chapters/1/exercises/shared/functions.h~ new file mode 100644 index 0000000..037e5ff --- /dev/null +++ b/chapters/1/exercises/shared/functions.h~ @@ -0,0 +1,11 @@ +#include +#include "limits.h" +float convert2cels(int fahr); +void reverse(char s[MAXLINE], char d[strlen(s)]); +void copy(char to[], char from[]); +int getlines(char line[], int maxline); +int detab(char s[], int lim, int col); +int entab(char s[], int lim, int col); +int fold(char s[], int lim, int cpr); +int uncomment(char s[], int lim); +int synthaxcheck(); diff --git a/chapters/1/exercises/shared/functions/synthaxcheck.c b/chapters/1/exercises/shared/functions/synthaxcheck.c new file mode 100644 index 0000000..7c6e69b --- /dev/null +++ b/chapters/1/exercises/shared/functions/synthaxcheck.c @@ -0,0 +1,122 @@ +#include + +int unmline, unmcol; + +int synthaxcheck(void) { + int c, // Character integer + lastchar = 0, // Last character buffers, for escapes + dq = 0, // Double quote status + sq = 0, // Single quote status + cb = 0, // Curly bracket count + sb = 0, // Square bracket count + rb = 0, // Round bracket count + mc = 0, // Multi-line comment status + sc = 0, // Single line comment status + line = 0, // Current line + col = 0, // Current column + lce = 0, // Last character escape + result = 0; // Result of evaluation + while ((c=getchar()) != EOF && result == 0) { + if (lastchar != '\\' || lce != 0) { + switch (c) { + case '"': { + if (sq == 0 && mc == 0 && sc == 0) { + if (dq == 1) { + dq = 0; + } else { + dq = 1; + } + } + break; + } + case '\'': { + if (dq == 0 && mc == 0 && sc == 0) { + if (sq == 1) { + sq = 0; + } else { + sq = 1; + } + } + break; + } + if (sq == 0 && dq == 0) { + if (mc == 0 && sc == 0) { + case '{': { + cb++; + break; + } + case '}': { + cb--; + break; + } + case '[': { + sb++; + break; + } + case ']': { + sb--; + break; + } + case '(': { + rb++; + break; + } + case ')': { + rb--; + break; + } + } + case '/': { + if (lastchar == '/') { + sc = 1; + } else if (lastchar == '*') { + mc = 0; + } + break; + } + case '*': { + if (lastchar == '/' && mc == 0) { + mc = 1; + } + break; + } + } + case '\n': { + if (dq == 0 && sq == 0) { + col = 0; + line++; + } else if (dq != 0) { + result = 4; + } else if (sq != 0) { + result = 3; + } + if (sc != 0) { + sc = 0; + } + break; + } + default: break; + } + } + col++; + if (lastchar == '\\') { + lce = 1; + } else { + lce = 0; + } + lastchar = c; + } + unmcol = col; + unmline = line; + // Don't overwrite results if previous loop finished with a error + if (result == 0) { + if (cb != 0) { + result = 2; + } else if (sb != 0) { + result = 5; + } else if (rb != 0) { + result = 1; + } + } + return result; +} diff --git a/chapters/1/exercises/shared/functions/synthaxcheck.c~ b/chapters/1/exercises/shared/functions/synthaxcheck.c~ new file mode 100644 index 0000000..8306720 --- /dev/null +++ b/chapters/1/exercises/shared/functions/synthaxcheck.c~ @@ -0,0 +1,122 @@ +#include + +int unmline, unmcol; + +int synthaxcheck(void) { + int c, // Character integer + lastchar = 0, // Last character buffers, for escapes + dq = 0, // Double quote status + sq = 0, // Single quote status + cb = 0, // Curly bracket count + sb = 0, // Square bracket count + rb = 0, // Round bracket count + mc = 0, // Multi-line comment status + sc = 0, // Single line comment status + line = 0, // Current line + col = 0, // Current column + lce = 0, // Last character escape + result = 0; // Result of evaluation + while ((c=getchar()) != EOF && result == 0) { {woopsextra + if (lastchar != '\\' || lce != 0) { + switch (c) { + case '"': { + if (sq == 0 && mc == 0 && sc == 0) { + if (dq == 1) { + dq = 0; + } else { + dq = 1; + } + } + break; + } + case '\'': { + if (dq == 0 && mc == 0 && sc == 0) { + if (sq == 1) { + sq = 0; + } else { + sq = 1; + } + } + break; + } + if (sq == 0 && dq == 0) { + if (mc == 0 && sc == 0) { + case '{': { + cb++; + break; + } + case '}': { + cb--; + break; + } + case '[': { + sb++; + break; + } + case ']': { + sb--; + break; + } + case '(': { + rb++; + break; + } + case ')': { + rb--; + break; + } + } + case '/': { + if (lastchar == '/') { + sc = 1; + } else if (lastchar == '*') { + mc = 0; + } + break; + } + case '*': { + if (lastchar == '/' && mc == 0) { + mc = 1; + } + break; + } + } + case '\n': { + if (dq == 0 && sq == 0) { + col = 0; + line++; + } else if (dq != 0) { + result = 4; + } else if (sq != 0) { + result = 3; + } + if (sc != 0) { + sc = 0; + } + break; + } + default: break; + } + } + col++; + if (lastchar == '\\') { + lce = 1; + } else { + lce = 0; + } + lastchar = c; + } + unmcol = col; + unmline = line; + // Don't overwrite results if previous loop finished with a error + if (result == 0) { + if (cb != 0) { + result = 2; + } else if (sb != 0) { + result = 5; + } else if (rb != 0) { + result = 1; + } + } + return result; +}