diff --git a/Makefile b/Makefile deleted file mode 100644 index 1289f05..0000000 --- a/Makefile +++ /dev/null @@ -1,21 +0,0 @@ - -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)/* diff --git a/ast-lex-yacc/Makefile b/ast-lex-yacc/Makefile new file mode 100644 index 0000000..7cdc77b --- /dev/null +++ b/ast-lex-yacc/Makefile @@ -0,0 +1,26 @@ + +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 yacc.yy.c + cc $(BUILDDIR)/yacc.yy.c $(BUILDDIR)/lex.yy.c -o $(BUILDDIR)/$(TARGET_SRC) -lfl + +# generate c file for lex +lex.yy.c: + flex -o $(BUILDDIR)/lex.yy.c $(TARGET_SRC).l + +# generate c file for yacc +yacc.yy.c: + yacc -d -o $(BUILDDIR)/yacc.yy.c $(TARGET_SRC).y + +clean: # wipe the build directory + rm -f $(BUILDDIR)/* diff --git a/ast-lex-yacc/main.l b/ast-lex-yacc/main.l new file mode 100644 index 0000000..0f0acfe --- /dev/null +++ b/ast-lex-yacc/main.l @@ -0,0 +1,37 @@ +%option noyywrap +%{ + #include + #include "yacc.yy.h" + #define YYDEBUG 1 + int yyLineNumber = 1; +%} + +%% +\n yyLineNumber++; +0|[1-9][0-9]* return(Number); // integer numbers +"VAR"|"var" return(Variable); +"INT"|"int" return(Integer); +[a-zA-Z]+ return(Ident); +"," return(','); +":" return(':'); +";" return(';'); +"+" return('+'); +"-" return('-'); +"(" return('('); +")" return(')'); +[ \t] +. print_stdout("unknown Symbol"); +%% + +/* +int main() +{ + printf("Programm eingeben: \n"); + yylex(); + return 0; +} +*/ + +int print_stdout(char token[]) { + printf("\nFLEX/[INF] >>> Token (%d) AS %s ```%s```\n", yyLineNumber, token, yytext); +} diff --git a/ast-lex-yacc/main.y b/ast-lex-yacc/main.y new file mode 100644 index 0000000..db701c0 --- /dev/null +++ b/ast-lex-yacc/main.y @@ -0,0 +1,28 @@ +%{ + #include + extern int yylineno; +%} + +%token Variable +%token Integer +%token Ident +%token Number + +%% +program: decllist exprlist; +decllist: decl ';' | decllist decl ';' | /* empty */ | error ';' { yyerror(" im Deklarationsteil\n"); }; +decl: Variable idlist ':' type; +idlist: Ident | idlist ',' Ident; +type: Integer; +exprlist: /* empty */ ; +%% + +int main() { + printf("Bitte geben Sie ein Program ein: \n"); + yyparse(); + return 0; +} + +int yyerror(char *s) { + printf("YACC/[ERR] >>> Failed parsing grammar (%d): ```%s```", yylineno, s); +} diff --git a/main.lex b/test.lex similarity index 100% rename from main.lex rename to test.lex