scanner: erster versuch (ohne tests)
authorBernhard Urban <lewurm@gmail.com>
Wed, 3 Mar 2010 19:33:58 +0000 (20:33 +0100)
committerBernhard Urban <lewurm@gmail.com>
Wed, 3 Mar 2010 19:33:58 +0000 (20:33 +0100)
.gitignore
scanner/Makefile [new file with mode: 0755]
scanner/scanner.lex [new file with mode: 0644]

index 1fc9142c9cedc503b8a988e0e6ce6112f6b71123..f68ecb8853efad88398609e13c90b2ce3932f5ce 100644 (file)
@@ -12,4 +12,8 @@ asma/asma
 #asmb
 asmb/asmb
 
+#scanner
+scanner/scanner.c
+scanner/scanner
+
 #weitere eintragen...
diff --git a/scanner/Makefile b/scanner/Makefile
new file mode 100755 (executable)
index 0000000..7efaed8
--- /dev/null
@@ -0,0 +1,20 @@
+NAME := scanner
+all: $(NAME)
+
+$(NAME): $(NAME).lex
+       @echo "  FLEX   $<"
+       @flex -o$(NAME).c $(NAME).lex
+       @echo "  COMPILE $<"
+       @gcc -pedantic -ansi -Wall -g -o $(NAME) $(NAME).c -D_GNU_SOURCE -lfl
+
+.PHONY: clean
+clean:
+       rm -f $(NAME).o $(NAME) $(NAME).c
+
+1test: $(NAME)
+       @echo "execute ./$(NAME)"
+       @./$(NAME)
+
+2test:
+       /usr/ftp/pub/ublu/test/$(NAME)/test
+
diff --git a/scanner/scanner.lex b/scanner/scanner.lex
new file mode 100644 (file)
index 0000000..ebf5f8f
--- /dev/null
@@ -0,0 +1,53 @@
+       #include <stdio.h>
+       #include <stdlib.h>
+       #include <string.h>
+
+KEYWORD struct|end|method|var|if|then|else|while|do|return|not|or|this
+SPECIAL_CHAR \;|\(|\)|\:=|\.|\-|\*|\<|\=|\.
+IDENTIFIER [a-zA-Z_][0-9a-zA-Z_]*
+NUMBER_HEX 0x[0-9A-Fa-f]+
+NUMBER_DEC [0-9]+
+WHITESPACE [\t\n\r ]
+COMMENT_START \/\*
+COMMENT_END    \*\/
+
+/* x ist unsere zustandsvariable! */
+%x COMMENT
+%option yylineno
+%%
+
+{COMMENT_START}        BEGIN(COMMENT);
+
+<COMMENT>{COMMENT_END} BEGIN(INITIAL);
+
+<COMMENT><<EOF>> { (void) fprintf(stderr, "kommentar nicht geschlossen\n"); exit(1); }
+
+<COMMENT>. /* alles im kommentar wird ignoriert */
+
+{KEYWORD} printf("%s\n", yytext);
+
+{SPECIAL_CHAR} printf("%s\n", yytext);
+
+{IDENTIFIER} printf("ident %s\n", yytext);
+
+{NUMBER_DEC} printf("num %x\n", strtol(yytext, (char **)NULL, 10));
+
+{NUMBER_HEX} {
+#if 0
+       char *copy=strdup(yytext);
+       copy[strlen(copy)-1]='\0';
+#endif
+       printf("num %x\n", strtol(yytext, (char **)NULL, 16));
+}
+
+{WHITESPACE} /* ignorieren */
+
+. { (void) fprintf(stderr, "Lexikalischer fehler auf Zeile %i\n", yylineno); exit(1); }
+
+%%
+
+int main(int argc, char **argv) {
+       yylex();
+       return 0;
+}
+