From 0c81ee06d19d1d7f67dab3c2c9da268b0c55e3c0 Mon Sep 17 00:00:00 2001 From: Jacob McDonnell Date: Sat, 14 Mar 2026 18:44:18 -0400 Subject: feat: Initial parsing of instructions Initial ability to parse basic instructions. Currently hex numbers are broken and immediates with labels (movl $str, %ecx) are broken. --- src/parser.y | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/parser.y (limited to 'src/parser.y') diff --git a/src/parser.y b/src/parser.y new file mode 100644 index 0000000..719643d --- /dev/null +++ b/src/parser.y @@ -0,0 +1,101 @@ +%{ +#include +#include +#include +#include + +size_t line_number = 1; + +extern int yylex(); +extern int yyparse(); +extern FILE *yyin; +void yyerror(const char *s); +%} + +%union { + int i_val; + float f_val; + char *s_val; + char *symbol; +} + +%token T_INTEGER +%token T_FLOAT +%token T_STRING +%token T_LABEL +%token T_INSTRUCTION +%token T_ENDL +%token T_COMMA +%token T_IMMEDIATE +%% +asm: + statements + ; + +statements: + statements statement + | statement + ; + +statement: + instruction_one_param_symbol T_ENDL + | instruction_one_param_immediate T_ENDL + | instruction_two_param_symbols T_ENDL + | instruction_symbol_immediate T_ENDL + | instruction_immediate_symbol T_ENDL + | T_ENDL + ; + +instruction_one_param_symbol: + T_INSTRUCTION T_LABEL { + printf("Found instruction %s(%s)\n", $1, $2); + }; + +instruction_one_param_immediate: + T_INSTRUCTION T_IMMEDIATE T_INTEGER { + printf("Found instruction %s(%d)\n", $1, $3); + }; +instruction_two_param_symbols: + T_INSTRUCTION T_LABEL T_COMMA T_LABEL { + printf("Found instruction %s(%s, %s)\n", $1, $2, $4); + }; + +instruction_symbol_immediate: + T_INSTRUCTION T_LABEL T_COMMA T_IMMEDIATE T_INTEGER { + printf("Found instruction %s(%s, %d)\n", $1, $2, $5); + } + +instruction_immediate_symbol: + T_INSTRUCTION T_IMMEDIATE T_INTEGER T_COMMA T_LABEL { + printf("Found instruction %s(%d, %s)\n", $1, $3, $5); + } +%% + +void yyerror(const char *s) { + printf("Parser Error: %lu: %s\n", line_number, s); +} + +int main(int argc, char *argv[]) { + FILE *input = stdin; + + int j = 0; + while (j != -1) { + j = getopt(argc, argv, "o:"); + + switch (j) { + case 'o': + input = fopen(optarg, "r"); + if (input == NULL) { + perror(argv[0]); + return -1; + } + break; + default: + break; + } + } + + yyin = input; + yyparse(); +} + -- cgit v1.2.3