forked from newseduser/basic_oop_compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
55 lines (47 loc) · 984 Bytes
/
parser.y
File metadata and controls
55 lines (47 loc) · 984 Bytes
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
%{
#include <stdlib.h>
#include <stdio.h>
#include "header.h"
#include "codes.c"
int yylex(void);
#define YYSTYPE tnode*
tnode*parse_tree;
%}
%token NUM PLUS MINUS MUL DIV END
%left PLUS MINUS
%left MUL DIV
%%
program : expr END {
$$ = $2;
parse_tree=$1;
return 0;
}
;
expr : expr PLUS expr {$$ = makeOperatorNode('+',$1,$3);}
| expr MINUS expr {$$ = makeOperatorNode('-',$1,$3);}
| expr MUL expr {$$ = makeOperatorNode('*',$1,$3);}
| expr DIV expr {$$ = makeOperatorNode('/',$1,$3);}
| '(' expr ')' {$$ = $2;}
| NUM {$$ = $1;}
;
%%
void yyerror(char const *s)
{
printf("yyerror %s",s);
}
int lex_input_file(FILE*);
int main(int argc, char**argv)
{
if(argc<2)
{
printf("No args.");
return 0;
}
FILE*src_file = fopen(argv[1],"r");
lex_input_file(src_file);
yyparse();
FILE*target_file;
target_file = fopen("fin.xsm","w");
codeWrite(parse_tree,target_file);
return 0;
}