48 lines
800 B
Plaintext
48 lines
800 B
Plaintext
%{
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
void yyerror(const char *s);
|
|
int yylex(void);
|
|
%}
|
|
|
|
%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() {
|
|
printf("Enter a polynomial expression:\n");
|
|
yyparse();
|
|
return 0;
|
|
} |