From e55342acd2e7b380d9756c298b889637f7419b0a Mon Sep 17 00:00:00 2001 From: SinusFox Date: Tue, 9 May 2023 13:33:52 +0200 Subject: [PATCH 1/7] Create Lexer.h --- Exercise 2/C/Lexer.h | 17 +++++++++++++++++ Exercise 2/C/TestLexer.c | 8 ++++++++ 2 files changed, 25 insertions(+) create mode 100644 Exercise 2/C/Lexer.h create mode 100644 Exercise 2/C/TestLexer.c diff --git a/Exercise 2/C/Lexer.h b/Exercise 2/C/Lexer.h new file mode 100644 index 0000000..8aee3bf --- /dev/null +++ b/Exercise 2/C/Lexer.h @@ -0,0 +1,17 @@ +/* symbols */ +#define _LETTER_ 1 +#define _DIGIT_ 2 +#define _OPERATOR_ 3 +#define _PUNCTUATION_ 4 +#define _END_OF_LINE_ 5 +#define _UNKNOWN_ 10 + +/* state */ +#define _START_ 100 +#define _GET_NEXT_CHARACTER_ 110 +#define _READ_IDENTIFIER_ 120 +#define _READ_NUM_LIT_ 130 +#define _READ_OPERATOR_ 140 +#define _READ_PUNCTUATION_ 150 +#define _STOP_ 999 +#define _ERROR_ 1000 diff --git a/Exercise 2/C/TestLexer.c b/Exercise 2/C/TestLexer.c new file mode 100644 index 0000000..7212b40 --- /dev/null +++ b/Exercise 2/C/TestLexer.c @@ -0,0 +1,8 @@ +#include +#include "Lexer.h" + +int main() { + char* sourcecode = "Hello World the second - I coded this before. So yeah, FOXES ARE SUPERIOR.\nBut the professor wants us to write the following: \"index = 2 * count + 42;\""; + printf(sourcecode); + return 0; +} -- 2.52.0 From 9c870e74db55c0b15b38311347ca42eb5597a279 Mon Sep 17 00:00:00 2001 From: SinusFox Date: Tue, 9 May 2023 14:51:15 +0200 Subject: [PATCH 2/7] initial creation of lexer --- Exercise 2/C/TestLexer.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Exercise 2/C/TestLexer.c b/Exercise 2/C/TestLexer.c index 7212b40..f90000e 100644 --- a/Exercise 2/C/TestLexer.c +++ b/Exercise 2/C/TestLexer.c @@ -1,8 +1,27 @@ #include #include "Lexer.h" +int start(char* sourcecode) { + if (sourcecode != NULL) { + return _GET_NEXT_CHARACTER_; + } else { + return _STOP_; + } +} + int main() { char* sourcecode = "Hello World the second - I coded this before. So yeah, FOXES ARE SUPERIOR.\nBut the professor wants us to write the following: \"index = 2 * count + 42;\""; printf(sourcecode); + + int lexer_state = _START_; + do { + switch (lexer_state) { + case _START_: + lexer_state = start(sourcecode); + break; + default: lexer_state = _ERROR_; + } + } while (lexer_state != _STOP_); + return 0; } -- 2.52.0 From 39f2ee43942748653089234af9438f8dd7b761d4 Mon Sep 17 00:00:00 2001 From: SinusFox Date: Tue, 9 May 2023 15:06:26 +0200 Subject: [PATCH 3/7] added more states --- Exercise 2/C/TestLexer.c | 52 +++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/Exercise 2/C/TestLexer.c b/Exercise 2/C/TestLexer.c index f90000e..26fef6f 100644 --- a/Exercise 2/C/TestLexer.c +++ b/Exercise 2/C/TestLexer.c @@ -2,15 +2,37 @@ #include "Lexer.h" int start(char* sourcecode) { - if (sourcecode != NULL) { - return _GET_NEXT_CHARACTER_; - } else { - return _STOP_; - } + // if (sourcecode != NULL) { + // return _GET_NEXT_CHARACTER_; + // } else { + // return _STOP_; + // } + + return _STOP_; // placeholder +} + +int getNextCharacter() { + return _STOP_; // placeholder +} + +int readIdentifier() { + return _STOP_; // placeholder +} + +int readNumLit() { + return _STOP_; // placeholder +} + +int readOperator() { + return _STOP_; // placeholder +} + +int readPunctuation() { + return _STOP_; // placeholder } int main() { - char* sourcecode = "Hello World the second - I coded this before. So yeah, FOXES ARE SUPERIOR.\nBut the professor wants us to write the following: \"index = 2 * count + 42;\""; + char* sourcecode = "Hello World the second - I coded this before. So yeah, FOXES ARE SUPERIOR.\nBut the professor wants us to write the following: \"index = 2 * count + 42;\"\n\n"; printf(sourcecode); int lexer_state = _START_; @@ -19,9 +41,27 @@ int main() { case _START_: lexer_state = start(sourcecode); break; + case _GET_NEXT_CHARACTER_: + lexer_state = getNextCharacter(); + break; + case _READ_IDENTIFIER_: + lexer_state = readIdentifier(); + break; + case _READ_NUM_LIT_: + lexer_state = readNumLit(); + break; + case _READ_OPERATOR_: + lexer_state = readOperator(); + break; + case _READ_PUNCTUATION_: + lexer_state = readPunctuation(); + break; default: lexer_state = _ERROR_; } } while (lexer_state != _STOP_); + printf("Lexer exited with code "); + // printf(lexer_state); + return 0; } -- 2.52.0 From af79eed4ed7ea02d21810da595d806995da6a591 Mon Sep 17 00:00:00 2001 From: SinusFox Date: Tue, 9 May 2023 15:15:35 +0200 Subject: [PATCH 4/7] adding functions to .h --- Exercise 2/C/Lexer.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Exercise 2/C/Lexer.h b/Exercise 2/C/Lexer.h index 8aee3bf..8252bbf 100644 --- a/Exercise 2/C/Lexer.h +++ b/Exercise 2/C/Lexer.h @@ -15,3 +15,12 @@ #define _READ_PUNCTUATION_ 150 #define _STOP_ 999 #define _ERROR_ 1000 + +/* functions */ +int start(char* sourcecode); +int getNextCharacter(); +int readIdentifier(); +int readNumLit(); +int readOperator(); +int readPunctuation(); +int main(); -- 2.52.0 From d548a902548e723a5ee4078a3475790b7476b043 Mon Sep 17 00:00:00 2001 From: SinusFox Date: Tue, 9 May 2023 15:17:56 +0200 Subject: [PATCH 5/7] small fix --- Exercise 2/C/Lexer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Exercise 2/C/Lexer.h b/Exercise 2/C/Lexer.h index 8252bbf..e69e6f1 100644 --- a/Exercise 2/C/Lexer.h +++ b/Exercise 2/C/Lexer.h @@ -17,7 +17,7 @@ #define _ERROR_ 1000 /* functions */ -int start(char* sourcecode); +int start(char*); int getNextCharacter(); int readIdentifier(); int readNumLit(); -- 2.52.0 From 9cde0cf1dfaa1c4230347fd20ee5d640f832584c Mon Sep 17 00:00:00 2001 From: SinusFox Date: Tue, 9 May 2023 15:25:14 +0200 Subject: [PATCH 6/7] moved lexer functions to Lexer.c and included Lexer.c --- Exercise 2/C/Lexer.c | 31 +++++++++++++++++++++++++++++++ Exercise 2/C/TestLexer.c | 33 ++------------------------------- 2 files changed, 33 insertions(+), 31 deletions(-) create mode 100644 Exercise 2/C/Lexer.c diff --git a/Exercise 2/C/Lexer.c b/Exercise 2/C/Lexer.c new file mode 100644 index 0000000..d29ce55 --- /dev/null +++ b/Exercise 2/C/Lexer.c @@ -0,0 +1,31 @@ +#include "Lexer.h" + +int start(char* src) { + // if (src != NULL) { + // return _GET_NEXT_CHARACTER_; + // } else { + // return _STOP_; + // } + + return _STOP_; // placeholder +} + +int getNextCharacter() { + return _STOP_; // placeholder +} + +int readIdentifier() { + return _STOP_; // placeholder +} + +int readNumLit() { + return _STOP_; // placeholder +} + +int readOperator() { + return _STOP_; // placeholder +} + +int readPunctuation() { + return _STOP_; // placeholder +} diff --git a/Exercise 2/C/TestLexer.c b/Exercise 2/C/TestLexer.c index 26fef6f..8fdb189 100644 --- a/Exercise 2/C/TestLexer.c +++ b/Exercise 2/C/TestLexer.c @@ -1,35 +1,6 @@ #include #include "Lexer.h" - -int start(char* sourcecode) { - // if (sourcecode != NULL) { - // return _GET_NEXT_CHARACTER_; - // } else { - // return _STOP_; - // } - - return _STOP_; // placeholder -} - -int getNextCharacter() { - return _STOP_; // placeholder -} - -int readIdentifier() { - return _STOP_; // placeholder -} - -int readNumLit() { - return _STOP_; // placeholder -} - -int readOperator() { - return _STOP_; // placeholder -} - -int readPunctuation() { - return _STOP_; // placeholder -} +#include "Lexer.c" int main() { char* sourcecode = "Hello World the second - I coded this before. So yeah, FOXES ARE SUPERIOR.\nBut the professor wants us to write the following: \"index = 2 * count + 42;\"\n\n"; @@ -60,7 +31,7 @@ int main() { } } while (lexer_state != _STOP_); - printf("Lexer exited with code "); + // printf("Lexer exited with code "); // printf(lexer_state); return 0; -- 2.52.0 From be9e69b459cf45874c2c46deda8f265365ff1cd7 Mon Sep 17 00:00:00 2001 From: SinusFox Date: Mon, 15 May 2023 14:34:44 +0200 Subject: [PATCH 7/7] exercise 3 --- Exercise 3/Lexer.c | 28 ++++++++++++++++++++++++++++ Exercise 3/Lexer.h | 26 ++++++++++++++++++++++++++ Exercise 3/TestLexer.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 Exercise 3/Lexer.c create mode 100644 Exercise 3/Lexer.h create mode 100644 Exercise 3/TestLexer.c diff --git a/Exercise 3/Lexer.c b/Exercise 3/Lexer.c new file mode 100644 index 0000000..12993d4 --- /dev/null +++ b/Exercise 3/Lexer.c @@ -0,0 +1,28 @@ +#include "Lexer.h" + +int start(char* src) { + if (src != 0 && src != '\0') { // checking if the string is empty or invalid + return _GET_NEXT_CHARACTER_; + } + return _STOP_; +} + +int getNextCharacter(char* src) { + return _STOP_; // placeholder +} + +int readIdentifier(char* src) { + return _STOP_; // placeholder +} + +int readNumLit(char* src) { + return _STOP_; // placeholder +} + +int readOperator(char* src) { + return _STOP_; // placeholder +} + +int readPunctuation(char* src) { + return _STOP_; // placeholder +} diff --git a/Exercise 3/Lexer.h b/Exercise 3/Lexer.h new file mode 100644 index 0000000..3e6fbe7 --- /dev/null +++ b/Exercise 3/Lexer.h @@ -0,0 +1,26 @@ +/* symbols */ +#define _LETTER_ 1 +#define _DIGIT_ 2 +#define _OPERATOR_ 3 +#define _PUNCTUATION_ 4 +#define _END_OF_LINE_ 5 +#define _UNKNOWN_ 10 + +/* state */ +#define _START_ 100 +#define _GET_NEXT_CHARACTER_ 110 +#define _READ_IDENTIFIER_ 120 +#define _READ_NUM_LIT_ 130 +#define _READ_OPERATOR_ 140 +#define _READ_PUNCTUATION_ 150 +#define _STOP_ 999 +#define _ERROR_ 1000 + +/* functions */ +int start(char*); +int getNextCharacter(char*); +int readIdentifier(char*); +int readNumLit(char*); +int readOperator(char*); +int readPunctuation(char*); +int main(); diff --git a/Exercise 3/TestLexer.c b/Exercise 3/TestLexer.c new file mode 100644 index 0000000..65e7559 --- /dev/null +++ b/Exercise 3/TestLexer.c @@ -0,0 +1,42 @@ +#include +#include "Lexer.h" +#include "Lexer.c" + +int main() { + // variables + char* sourcecode = "index = 2 * count + 42;"; // input string + int lexer_state = _START_; // int var for the state machine + + // checking var values + printf(sourcecode); // printing the input string to check it + + // main loop + do { + switch (lexer_state) { // state machine starts here + case _START_: + lexer_state = start(sourcecode); + break; + case _GET_NEXT_CHARACTER_: + lexer_state = getNextCharacter(sourcecode); + break; + case _READ_IDENTIFIER_: + lexer_state = readIdentifier(sourcecode); + break; + case _READ_NUM_LIT_: + lexer_state = readNumLit(sourcecode); + break; + case _READ_OPERATOR_: + lexer_state = readOperator(sourcecode); + break; + case _READ_PUNCTUATION_: + lexer_state = readPunctuation(sourcecode); + break; + default: lexer_state = _ERROR_; + } + } while (lexer_state != _STOP_); // state machine ends here + + // printf("Lexer exited with code "); + // printf(lexer_state); + + return 0; +} -- 2.52.0