-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser.y
More file actions
61 lines (53 loc) · 1.44 KB
/
parser.y
File metadata and controls
61 lines (53 loc) · 1.44 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
%{
#include <stdlib.h>
#include <stdio.h>
#include "header.h"
#include "codes.c"
int yylex(void);
#define YYSTYPE tnode*
tnode*parse_tree;
%}
%token ID NUM PLUS MINUS MUL DIV END BEG READ WRITE
%left PLUS MINUS
%left MUL DIV
%%
program : BEG NEWLINE Slist END {
$$ = $3;
parse_tree=$3;
printf("[+] Syntax Matched!\n");fflush(stdout);
return 0;
};
Slist : Slist Stmt LINE_ENDER NEWLINE {$$ = createTree(0,'c',NULL,$1,$2);} | Stmt LINE_ENDER NEWLINE {$$=$1;} | Slist NEWLINE {$$=$1;};
Stmt : InputStmt {$$=$1;} | OutputStmt {$$=$1;} | AsgStmt {$$=$1;} ;
InputStmt : READ '(' ID ')' {$$ = makeActionNode('r',$3);};
OutputStmt : WRITE '(' expr ')' {$$ = makeActionNode('w',$3);};
AsgStmt : expr ASG expr {$$ = makeOperatorNode('=',$1,$3);};
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;}
| ID {$$ = $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;
}