added starter files

This commit is contained in:
Sven Vogel 2023-11-21 14:42:23 +01:00
parent dd6392138b
commit 326d148d9f
4 changed files with 70 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
build/
*.c
*.cc
*.cpp

21
Makefile Normal file
View File

@ -0,0 +1,21 @@
BUILDDIR = build
TARGET_SRC = main
build: mkdir main
all: mkdir main run
mkdir: # create build directory
mkdir -p $(BUILDDIR)
run: # run binary
./$(BUILDDIR)/main
main: lex.yy.c # compile c file
cc $(BUILDDIR)/lex.yy.c -o $(BUILDDIR)/$(TARGET_SRC) -lfl
lex.yy.c: main.lex # generate c file
flex -o $(BUILDDIR)/lex.yy.c $(TARGET_SRC).lex
clean: # wipe the build directory
rm -f $(BUILDDIR)/*

16
devbox.json Normal file
View File

@ -0,0 +1,16 @@
{
"packages": [
"bison",
"flex",
"clang"
],
"env": {
"cc": "clang"
},
"shell": {
"init_hook": null
},
"nixpkgs": {
"commit": "f80ac848e3d6f0c12c52758c0f25c10c97ca3b62"
}
}

29
main.lex Normal file
View File

@ -0,0 +1,29 @@
%option noyywrap
%{
#include<stdio.h>
#define YYDEBUG 1
int yyLineNumber = 1;
%}
%%
[a-zA-Z]+ myprint("Identifier"); //matcht auch if und IF!
IF|if myprint("keyword");
\n yyLineNumber++;
[ \t] //nix machen, "spaces und tabs überlesen"
. myprint("unknown Symbol");
%%
int main()
{
printf("Programm eingeben: \n");
yylex();
return 0;
}
int myprint(char token[]) {
printf("\nZeile %d >>> ", yyLineNumber);
printf("%s ", token);
printf("%s\n", yytext); // yytext: String, der gerade gelesen wurde
}