-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
94 lines (79 loc) · 1.78 KB
/
Copy pathparser.y
File metadata and controls
94 lines (79 loc) · 1.78 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
%{
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "symbol_table.h"
extern void yyerror(char *s);
extern int yylex();
%}
%union {
double d_value;
symbol_table_data_t *symbol;
}
%token PLUS MINUS DIV MULT OPEN_PARENTHESES CLOSE_PARENTHESES EQUALS EOL
%token <d_value> NUMBER
%type <d_value> expression
%type <d_value> factor
%type <d_value> term
%token <symbol> NAME
%left '+' '-'
%left '*' '/'
%nonassoc UNIMUS
%%
statement_list:
{ printf("> "); }
|
statement_list statement EOL
|
statement_list EOL { printf("> "); }
;
statement:
NAME EQUALS expression { $1->value = $3; printf("> "); }
|
expression { printf("> %g\n> ", $1); }
;
expression:
factor
|
expression PLUS expression { $$ = $1 + $3; }
|
expression MINUS expression { $$ = $1 - $3; }
|
MINUS expression %prec UNIMUS { $$ = -$2; }
;
factor:
term
|
factor MULT factor { $$ = $1 * $3; }
|
factor DIV factor { if ($3 == 0.0) yyerror("divid by zero"); else $$ = $1 / $3; }
;
term:
NUMBER
|
NAME { $$ = $1->value; }
|
OPEN_PARENTHESES expression CLOSE_PARENTHESES { $$ = $2; }
|
NAME OPEN_PARENTHESES expression CLOSE_PARENTHESES
{
if ($1->func_ptr) {
$$ = ($1->func_ptr)($3);
} else {
printf("%s not a function\n", $1->name);
$$ = 0.0;
}
}
;
%%
void yyerror(
char *s)
{
fprintf(stderr, "%s\n", s);
}
int main(void)
{
symbol_table_init();
yyparse();
symbol_table_free();
}