Exercise 2 + 3 #4

Merged
SinusFox merged 7 commits from exercise-2 into main 2023-05-15 12:36:20 +00:00
6 changed files with 191 additions and 0 deletions
+31
View File
@@ -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
}
+26
View File
@@ -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();
int readIdentifier();
int readNumLit();
int readOperator();
int readPunctuation();
int main();
+38
View File
@@ -0,0 +1,38 @@
#include <stdio.h>
#include "Lexer.h"
#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";
printf(sourcecode);
int lexer_state = _START_;
do {
switch (lexer_state) {
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;
}
+28
View File
@@ -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
}
+26
View File
@@ -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();
+42
View File
@@ -0,0 +1,42 @@
#include <stdio.h>
#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;
}