-
Notifications
You must be signed in to change notification settings - Fork 0
Lexical analyzer #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Lexical analyzer #74
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 16 | ||
| VisualStudioVersion = 16.0.31410.357 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lexicalAnalysis", "lexicalAnalysis\lexicalAnalysis.vcxproj", "{4C58C0AB-268F-4588-8690-F3B9D99B950C}" | ||
| 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 | ||
| {4C58C0AB-268F-4588-8690-F3B9D99B950C}.Debug|x64.ActiveCfg = Debug|x64 | ||
| {4C58C0AB-268F-4588-8690-F3B9D99B950C}.Debug|x64.Build.0 = Debug|x64 | ||
| {4C58C0AB-268F-4588-8690-F3B9D99B950C}.Debug|x86.ActiveCfg = Debug|Win32 | ||
| {4C58C0AB-268F-4588-8690-F3B9D99B950C}.Debug|x86.Build.0 = Debug|Win32 | ||
| {4C58C0AB-268F-4588-8690-F3B9D99B950C}.Release|x64.ActiveCfg = Release|x64 | ||
| {4C58C0AB-268F-4588-8690-F3B9D99B950C}.Release|x64.Build.0 = Release|x64 | ||
| {4C58C0AB-268F-4588-8690-F3B9D99B950C}.Release|x86.ActiveCfg = Release|Win32 | ||
| {4C58C0AB-268F-4588-8690-F3B9D99B950C}.Release|x86.Build.0 = Release|Win32 | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {C864152D-6DC5-4FB3-B326-DBAFFAE504BB} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,107 @@ | ||||||
| #include "LexicalAnalysis.h" | ||||||
| #include <string.h> | ||||||
|
|
||||||
| bool isDigit(char symbol) | ||||||
| { | ||||||
| return symbol <= '9' && symbol >= '0'; | ||||||
| } | ||||||
|
|
||||||
| bool checkExpression(char* string) | ||||||
| { | ||||||
| const int lenghtString = strlen(string); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Оно "length" |
||||||
| int state = 0; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут бы enum с человекочитаемыми состояниями |
||||||
| for (int i = 0; i < lenghtString + 1; i++) | ||||||
| { | ||||||
| switch (state) | ||||||
| { | ||||||
| case 0 : | ||||||
| { | ||||||
| if (isDigit(string[i])) | ||||||
| { | ||||||
| state = 1; | ||||||
| break; | ||||||
| } | ||||||
| return false; | ||||||
| } | ||||||
| case 1: | ||||||
| { | ||||||
| if (isDigit(string[i])) | ||||||
| { | ||||||
| break; | ||||||
| } | ||||||
| if (string[i] == '\0') | ||||||
| { | ||||||
| return true; | ||||||
| } | ||||||
| if (string[i] == '.') | ||||||
| { | ||||||
| state = 2; | ||||||
| break; | ||||||
| } | ||||||
| return false; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не-а, по регэкспу часть с точкой не обязательна, тут может быть сразу E |
||||||
| } | ||||||
| case 2: | ||||||
| { | ||||||
| if (isDigit(string[i])) | ||||||
| { | ||||||
| state = 3; | ||||||
| break; | ||||||
| } | ||||||
| return false; | ||||||
| } | ||||||
| case 3: | ||||||
| { | ||||||
| if (isDigit(string[i])) | ||||||
| { | ||||||
| break; | ||||||
| } | ||||||
| if (string[i] == '\0') | ||||||
| { | ||||||
| return true; | ||||||
| } | ||||||
| if (string[i] == 'E') | ||||||
| { | ||||||
| state = 4; | ||||||
| break; | ||||||
| } | ||||||
| return false; | ||||||
| } | ||||||
| case 4: | ||||||
| { | ||||||
| if(isDigit(string[i])) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
| state = 6; | ||||||
| break; | ||||||
| } | ||||||
| if (string[i] == '+' || string[i] == '-') | ||||||
| { | ||||||
| state = 5; | ||||||
| break; | ||||||
| } | ||||||
| return false; | ||||||
| } | ||||||
| case 5: | ||||||
| { | ||||||
| if (isDigit(string[i])) | ||||||
| { | ||||||
| state = 6; | ||||||
| break; | ||||||
| } | ||||||
| return false; | ||||||
| } | ||||||
| case 6: | ||||||
| { | ||||||
| if (isDigit(string[i])) | ||||||
| { | ||||||
| break; | ||||||
| } | ||||||
| if (string[i] == '\0') | ||||||
| { | ||||||
| return true; | ||||||
| } | ||||||
| return false; | ||||||
|
Comment on lines
+98
to
+102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :( |
||||||
| } | ||||||
| } | ||||||
| } | ||||||
| return false; | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #pragma once | ||
| #include <stdbool.h> | ||
|
|
||
| // Function to check whether an expression is a real number | ||
| bool checkExpression(char* string); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #include "TestLexicalAnalysis.h" | ||
| #include <stdio.h> | ||
| #include "ReadString.h" | ||
| #include "LexicalAnalysis.h" | ||
| #include <stdlib.h> | ||
|
|
||
| int main() | ||
| { | ||
| if (!testCheckTestExpression()) | ||
| { | ||
| printf("Test failed"); | ||
| return -1; | ||
| } | ||
| printf("enter the string\n"); | ||
| int error = 0; | ||
| char* string = readString(&error); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const |
||
| if (error == 1) | ||
| { | ||
| printf("failed to enter string\n"); | ||
| return -1; | ||
| } | ||
| bool result = checkExpression(string); | ||
| free(string); | ||
| if (!result) | ||
| { | ||
| printf("the entered string is not a real number\n"); | ||
| return -1; | ||
| } | ||
| printf("the entered string is a real number\n"); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #include "ReadString.h" | ||
| #include <malloc.h> | ||
| #include <string.h> | ||
| #include <stdio.h> | ||
|
|
||
| char* readString(int* error) | ||
| { | ||
| int index = 0; | ||
| int size = 100; | ||
| char* array = calloc(size, sizeof(char)); | ||
| if (array == NULL) | ||
| { | ||
| *error = 1; | ||
| return NULL; | ||
| } | ||
| char symbol = getchar(); | ||
| while (symbol != '\n') | ||
| { | ||
| array[index] = symbol; | ||
| if (strlen(array) == size) | ||
| { | ||
| size = size * 2; | ||
| char* temporary = realloc(array, size * sizeof(char)); | ||
| if (temporary == NULL) | ||
| { | ||
| *error = 1; | ||
| free(array); | ||
| return NULL; | ||
| } | ||
| array = temporary; | ||
| } | ||
| symbol = getchar(); | ||
| index++; | ||
| } | ||
| array[index] = '\0'; | ||
| return array; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #pragma once | ||
|
|
||
| // Function for entering a line with memory allocation (for entering a line of arbitrary size) | ||
| char* readString(int* error); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #include "TestLexicalAnalysis.h" | ||
| #include "LexicalAnalysis.h" | ||
|
|
||
| bool testCheckTestExpression() | ||
| { | ||
| bool firstResult = checkExpression("3"); | ||
| bool secondResult = checkExpression("3.734432"); | ||
| bool thirdResult = checkExpression("33412.253253"); | ||
| bool fourthResult = checkExpression("234.2424E-10"); | ||
| bool fifthResult = checkExpression("3.0001E+10"); | ||
| bool sixthResult = checkExpression("3."); | ||
| bool seventhResult = checkExpression("3.E"); | ||
| bool eighthResult = checkExpression("3324.23455E+"); | ||
| bool ninthResult = checkExpression(""); | ||
| bool tenthResult = checkExpression("3E-1111.1221"); | ||
| return firstResult && secondResult && thirdResult && fourthResult && fifthResult | ||
| && !sixthResult && !seventhResult && !eighthResult && !ninthResult && !tenthResult; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #pragma once | ||
| #include <stdbool.h> | ||
|
|
||
| // Function for testing the defining function is and the string is a real number | ||
| bool testCheckTestExpression(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.