-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
46 lines (46 loc) · 1.13 KB
/
Copy pathparser.y
File metadata and controls
46 lines (46 loc) · 1.13 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
%include
{
#include <assert.h>
#include <stdlib.h>
#include "parser.h"
#include "ast.h"
#define LIST 1000
Node * linkn(Node *a, Node *b,int type){
Node *d=malloc(sizeof(Node));
d->a=a;
d->b=b;
d->name=0;
d->type=type;
return d;
}
void dump(Node *n){
if (n->a)
dump(n->a);
if (n->b)
dump(n->b);
if (n->name)
printf(n->name);
}
}
%token_type {Node*}
%parse_failure {
fprintf(stderr,"Giving up. Parser is hopelessly lost...\n");
}
%syntax_error{
extern void syntax_error(void);
syntax_error();
}
%left PLUS MINUS.
%left DIVIDE TIMES.
program ::= statlist(A). {dump(A);}
statlist(D) ::= stat(A) statlist(B). {D=linkn(A,B,LIST);}
statlist(D) ::= stat(A). {D=A;}
stat(D) ::= ident(A) EQUA expr(B) SEMI. {D=linkn(A,B,EQUA);}
expr(D) ::= OPENP expr(A) CLOSEP. {D=A;}
expr(D) ::= expr(A) PLUS expr(B). {D=linkn(A,B,PLUS);}
expr(D) ::= expr(A) DIVIDE expr(B). {D=linkn(A,B,DIVIDE);}
expr(D) ::= expr(A) TIMES expr(B). {D=linkn(A,B,TIMES);}
expr(D) ::= expr(A) MINUS expr(B). {D=linkn(A,B,MINUS);}
expr(D) ::= DOUBLE(A). {printf("num %s\n",A->name);D=A;}
expr(D) ::= ident(A). {D=A;}
ident(D) ::= IDENT(A). {printf("ident %s\n",A->name);D=A;}