%top { /** * Tokenizer for the ULNAv2-C instruction set. * * Generates an image file suitable to be read by Logisim. * * Copyright(c) 2019-2021,2023-2024 Jason Tang * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ #define _POSIX_C_SOURCE 200809L } %{ #include #include #include #include #include #include "ulnav2-c-as.h" #include "ulnav2-c-parser.h" void yyerror(char *s); %} %option yylineno %option nounput %option noinput %option noyywrap %x CMNT %% add[.] { return ADD; } addi[.] { return ADDI; } and[.] { return AND; } andi[.] { return ANDI; } ash { return ASH; } b { return B; } b([.])?eq { return BEQ; } b([.])?ge { return BGE; } b([.])?gt { return BGT; } b([.])?le { return BLE; } b([.])?lt { return BLT; } b([.])?ne { return BNE; } bl { return BL; } br { return BR; } brl { return BRL; } cmp[.] { return CMP; } cmpi[.] { return CMPI; } halt { return HALT; } ldw { return LDW; } ldwi { return LDWI; } lsh { return LSH; } movi { return MOVI; } movis { return MOVIS; } mul. { return MUL; } negi[.] { return NEGI; } or[.] { return OR; } ori[.] { return ORI; } rot { return ROT; } stw { return STW; } stwi { return STWI; } [rR][0-7] { yylval.regnum = atoi(yytext+1); return RNUM; } [a-z_.][a-z0-9_]+: { yylval.sval = strdup(yytext); yylval.sval[strlen(yytext) - 1] = '\0'; return LABEL; } [a-z_.][a-z0-9_]+ { yylval.sval = strdup(yytext); return TARGET; } #(-)?[0-9]+ { yylval.lval = strtol(yytext+1, NULL, 10); return IMM; } #0x[0-9a-f]+ { yylval.lval = strtoul(yytext+1, NULL, 16); return IMM; } (-)?[0-9]+ { yylval.lval = strtol(yytext, NULL, 10); return IMM; } 0x[0-9a-f]+ { yylval.lval = strtoul(yytext, NULL, 16); return IMM; } "//".* ; /* line comment */ "/*" { BEGIN CMNT; } /* block comment */ .|\n ; "*/" { BEGIN INITIAL; } [ \t\r]+ ; .|\n { return yytext[0]; } %% void yyerror(char *s) { yyerrorv(true, "%s", s); } void yyerrorv(bool show_token, const char *fmt, ...) { va_list ap; va_start(ap, fmt); fprintf(stderr, "Line %d: ", yylineno); vfprintf(stderr, fmt, ap); if (show_token) { fprintf(stderr, ": \"%s\"\n", yytext); } else { fprintf(stderr, "\n"); } va_end(ap); }