diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile.sln b/lexerOfCommentsByFile/lexerOfCommentsByFile.sln new file mode 100644 index 0000000..ff58d98 --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lexerOfCommentsByFile", "lexerOfCommentsByFile\lexerOfCommentsByFile.vcxproj", "{B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Debug|x64.ActiveCfg = Debug|x64 + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Debug|x64.Build.0 = Debug|x64 + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Debug|x86.ActiveCfg = Debug|Win32 + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Debug|x86.Build.0 = Debug|Win32 + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Release|x64.ActiveCfg = Release|x64 + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Release|x64.Build.0 = Release|x64 + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Release|x86.ActiveCfg = Release|Win32 + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0F1031D9-8477-45F8-876F-09D22FF8001E} + EndGlobalSection +EndGlobal diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c new file mode 100644 index 0000000..4d9812f --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c @@ -0,0 +1,105 @@ +#include "lexerOfComments.h" + +int changeStage(int statusTable[5][3], int stage, int symbol) { + int posSymbol = symbol % 42 == 0 ? 0 : symbol % 47 == 0 ? 1 : 2; + return statusTable[stage][posSymbol]; +} + +void clearBuffer(char buffer[], int* index) { + memset(buffer, 0, strlen(buffer)); + *index = 0; +} + +char* workWithStage(int statusTable[5][3], const char* fileNameWithStrings, Error* errorCheck) { + FILE* file = fopen(fileNameWithStrings, "r"); + if (file == NULL) { + *errorCheck = fileProblem; + return NULL; + } + char symbol = 0; + char bufferWithOneComment[1000] = { '\0' }; + char bufferWithAllComments[10000] = { '\0' }; + int stage = 1; + int indexBuffer = 0; + while (fscanf(file, "%c", &symbol) == 1) { + if (symbol == '/') { + switch (stage) { + case 1: + if (indexBuffer == 0) { + bufferWithOneComment[indexBuffer] = '/'; + ++indexBuffer; + } + break; + case 2: + clearBuffer(bufferWithOneComment, &indexBuffer); + bufferWithOneComment[0] = '/'; + indexBuffer = 1; + break; + case 3: + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + break; + case 4: + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + strcat(bufferWithAllComments, bufferWithOneComment); + memset(bufferWithOneComment, 0, strlen(bufferWithOneComment)); + indexBuffer = 0; + break; + } + } else if (symbol == '*') { + switch (stage) { + case 1: + clearBuffer(bufferWithOneComment, &indexBuffer); + break; + case 2: + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + break; + case 3: + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + break; + case 4: + clearBuffer(bufferWithOneComment, &indexBuffer); + break; + } + } else { + if (stage == 3) { + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + } else { + clearBuffer(bufferWithOneComment, &indexBuffer); + } + } + stage = changeStage(statusTable, stage, symbol); + } + fclose(file); + return bufferWithAllComments; +} + +char* lexerOfComments(const char* fileNameTable, const char* fileNameWithStrings, Error* errorCheck) { + FILE* file = fopen(fileNameTable, "r"); + if (file == NULL) { + *errorCheck = fileProblem; + return NULL; + } + int statusTable[5][3] = { 0 }; + char expression[4] = { '\0' }; + if (fscanf(file, "%s", expression) == 1) { + statusTable[0][0] = expression[0]; + statusTable[0][1] = expression[1]; + statusTable[0][2] = expression[2]; + } + + for (int i = 1; i < 5; ++i) { + if (fscanf(file, "%s", expression) == 1) { + statusTable[i][0] = expression[0] - 48; + statusTable[i][1] = expression[1] - 48; + statusTable[i][2] = expression[2] - 48; + } + } + fclose(file); + + return workWithStage(statusTable, fileNameWithStrings, errorCheck); +} \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h new file mode 100644 index 0000000..cf184d1 --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include +#include + +typedef enum Error { + ok, + fileProblem +} Error; + +//Lexer of comments. Return string with all comments. +char* lexerOfComments(const char* fileNameTable, const char* fileNameWithStrings, Error* errorCheck); \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj new file mode 100644 index 0000000..7f4f391 --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj @@ -0,0 +1,157 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {b8d1bf09-6f07-4e71-ba65-c7b78e7fe91a} + lexerOfCommentsByFile + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + false + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj.filters b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj.filters new file mode 100644 index 0000000..f17d6a6 --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj.filters @@ -0,0 +1,44 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Исходные файлы + + + Исходные файлы + + + + + Исходные файлы + + + Исходные файлы + + + \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/main.c b/lexerOfCommentsByFile/lexerOfCommentsByFile/main.c new file mode 100644 index 0000000..04817ae --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/main.c @@ -0,0 +1,29 @@ +#include "lexerOfComments.h" +#include +#include +#include "test.h" + +int main() { + if (test()) { + printf("Tests correct!\n"); + } else { + printf("Error\n"); + return -1; + } + Error errorCheck = ok; + printf("Input your file with extension. No more than 100 symbols\n"); + char nameFile[101] = { '\0' }; + int checkScanf = scanf_s("%s", nameFile, 100); + while (checkScanf != 1) { + while (getchar() != '\n') { + } + printf("Try again\n"); + checkScanf = scanf_s("%s", nameFile, 100); + } + char* buffer = lexerOfComments("table.txt", nameFile, &errorCheck); + if (errorCheck != ok) { + printf("Error\n"); + return -1; + } + printf("%s", buffer); +} \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/table.txt b/lexerOfCommentsByFile/lexerOfCommentsByFile/table.txt new file mode 100644 index 0000000..8c552c6 --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/table.txt @@ -0,0 +1,5 @@ +*/a +121 +321 +433 +111 \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/test.c b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.c new file mode 100644 index 0000000..cc3fdc5 --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.c @@ -0,0 +1,9 @@ +#include "test.h" +#include "lexerOfComments.h" + +bool test() { + Error errorCheck = ok; + char* stringForCheck = "/*fdfdfdfd*//**//*dfsdfsff*/"; + char* result = lexerOfComments("table.txt", "test.txt", &errorCheck); + return errorCheck == ok && strcmp(result, stringForCheck) == 0; +} \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/test.h b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.h new file mode 100644 index 0000000..0ca2603 --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +//Test for lexer of comments +bool test(); \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/test.txt b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.txt new file mode 100644 index 0000000..2cc66a2 --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.txt @@ -0,0 +1 @@ +dslfdsof'dksfodsf/*fdfdfdfd*/dfdfsdfdsf/**/dfssf/dfjdjf*/fdfdfd//*dfsdfsff*/dfdf*fdfd* \ No newline at end of file