64 lines
1.1 KiB
Plaintext
64 lines
1.1 KiB
Plaintext
%{
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
void yyerror(const char *s);
|
|
int yylex(void);
|
|
|
|
extern FILE* yyin;
|
|
%}
|
|
|
|
%union { int num; char var; }
|
|
|
|
%token <num> NUMBER
|
|
%token <var> VARIABLE
|
|
%token LPAREN RPAREN PLUS MINUS MULTIPLY
|
|
|
|
%%
|
|
|
|
input:
|
|
expr { printf("\033[92mParsing complete\033[0m\n"); }
|
|
;
|
|
|
|
expr:
|
|
term
|
|
| expr PLUS term { printf("Found operator: +\n"); }
|
|
| expr MINUS term { printf("Found operator: -\n"); }
|
|
;
|
|
|
|
term:
|
|
factor
|
|
| term MULTIPLY factor { printf("Found operator: *\n"); }
|
|
;
|
|
|
|
factor:
|
|
NUMBER { printf("Found number: %d\n", $1); }
|
|
| VARIABLE { printf("Found variable: %c\n", $1); }
|
|
| LPAREN expr RPAREN
|
|
;
|
|
|
|
%%
|
|
|
|
void yyerror(const char *s) {
|
|
fprintf(stderr, "Error: %s\n", s);
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc != 2) {
|
|
fprintf(stderr, "Usage: %s <input_file>\n", argv[0]);
|
|
return 1;
|
|
}
|
|
FILE* input_file = fopen(argv[1], "r");
|
|
if (!input_file) {
|
|
perror("Failed to open input file");
|
|
return 1;
|
|
}
|
|
|
|
yyin = input_file;
|
|
|
|
printf("Parsing expression from file: %s\n", argv[1]);
|
|
yyparse();
|
|
fclose(input_file);
|
|
|
|
return 0;
|
|
} |