%{ #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_SYMBOL %token T_INSTRUCTION %token T_ENDL %token T_COMMA %token T_OPENPAREN %token T_CLOSEPAREN %% asm: statements ; statements: statements statement | statement ; statement: instructions | T_ENDL ; instructions: rb_type | i_type | s_type | u_type | j_type ; rb_type: T_INSTRUCTION T_SYMBOL T_COMMA T_SYMBOL T_COMMA T_SYMBOL T_ENDL { printf("Read instruction: %s(%s, %s, %s)\n", $1, $2, $4, $6); }; i_type: T_INSTRUCTION T_SYMBOL T_COMMA T_SYMBOL T_COMMA T_INTEGER T_ENDL { printf("Read instruction: %s(%s, %s, %d)\n", $1, $2, $4, $6); }; s_type: T_INSTRUCTION T_SYMBOL T_COMMA T_INTEGER T_OPENPAREN T_SYMBOL T_CLOSEPAREN T_ENDL { printf("Read instruction: %s(%s, %s + %d)\n", $1, $2, $6, $4); }; u_type: T_INSTRUCTION T_SYMBOL T_COMMA T_INTEGER T_ENDL { printf("Read instruction: %s(%s, %d)\n", $1, $2, $4); }; j_type: T_INSTRUCTION T_SYMBOL T_COMMA T_SYMBOL T_ENDL { printf("Read instruction: %s(%s, %s)\n", $1, $2, $4); }; %% 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(); }