-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex_parser.y
More file actions
60 lines (50 loc) · 1.29 KB
/
Copy pathcomplex_parser.y
File metadata and controls
60 lines (50 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
void yyerror(const char *s);
%}
%define parse.error verbose
%token INTEGER IMAGINARY
%token NEWLINE
%token PLUS MINUS TIMES DIVIDE
%token LPAREN RPAREN
%left PLUS MINUS
%left TIMES DIVIDE
%right UMINUS
%%
program:
program_line
| program program_line
;
program_line: NEWLINE { printf("Enter expressions in the form of complex numbers \"(a ± bi)\". (Ctrl+D to exit):\n"); }
| multi_expression NEWLINE { printf("Seems Good!\n"); }
| error NEWLINE { yyerrok; printf("Recovered from error.\n");}
;
multi_expression: enclosed_expression
| multi_expression addop enclosed_expression
| multi_expression multop enclosed_expression
;
enclosed_expression: LPAREN expression RPAREN
;
expression: term
| expression addop term
;
term: factor
| term multop factor
;
factor: INTEGER
| IMAGINARY
| MINUS factor %prec UMINUS
| LPAREN expression RPAREN
;
addop: PLUS | MINUS ;
multop: TIMES | DIVIDE ;
%%
void yyerror(const char *s) {
fprintf(stderr, "Parser Error: %s\n", s);
}
int main(void) {
printf("Enter expressions in the form of complex numbers \"(a ± bi)\". (Ctrl+D to exit):\n");
return yyparse();
}