-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyacc.y
More file actions
118 lines (94 loc) · 3 KB
/
yacc.y
File metadata and controls
118 lines (94 loc) · 3 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
%{
#include <stdio.h>
#include <stdlib.h>
extern int yylex();
%}
%union{
int intValue; /* integer value */
float floatValue; /* float value */
char charValue; /* char value */
char* stringValue; /* string value*/
char* identifier; /* identifier name */
char* comment;
};
%token <intValue> INTVALUE;
%token <floatValue> FLOATVALUE;
%token <charValue> CHARVALUE;
%token <stringValue> STRINGVALUE;
%token <comment> COMMENT;
%token <identifier> IDENTIFIER;
%token CONSTANT INT FLOAT STRING CHAR BOOL IF THEN ELSE WHILE DO SWITCH CASE DEFAULT FOR AND OR EQUALEQUAL GREATERTHAN SMALLERTHAN GREATERTHANOREQUAL SMALLERTHANOREQUAL NOT NOTEQUAL VOID MAIN RETURN COMMA
%token SEMI_COLON OPENED_BRACKET CLOSED_BRACKET OPENED_BRACE CLOSED_BRACE OPENED_SQ_BRACKET CLOSED_SQ_BRACKET COMMA TWO_DOTS PLUS MINUS MULTIPLY DIVIDE REMAINDER PLUS_EQUAL MINUS_EQUAL MULTIPLY_EQUAL DIVIDE_EQUAL PLUS_PLUS MINUS_MINUS EQUAL
%left MINUS PLUS
%left DIVIDE MULTIPLY
%left REMAINDER
%left OR AND
%left EQUALEQUAL NOTEQUAL
%left SMALLERTHAN SMALLERTHANOREQUAL GREATERTHAN GREATERTHANOREQUAL
%right NOT
%%
/* Language BODY */
Root: comments funcDefs comments main comments
| comments main comments
;
main: VOID MAIN OPENED_BRACKET CLOSED_BRACKET OPENED_BRACE body CLOSED_BRACE
;
body : whilestmt body
| ifstmt body
| dowhilestmt body
| forstmt body
| switchstmt body
| declaration body
| constdeclaration body
| assignment SEMI_COLON body
| mathassignment SEMI_COLON body
/*| function_call SEMI_COLON body */
| COMMENT body {$$ = $2 ;}
|{$$ = NULL ;}
;
fndecs : fndecs comments function_declaration
| comments function_declaration
;
comments : comments COMMENT
|
;
/* //////////////////////////////////////// */
/* Function declarations */
function_declaration : VOID IDENTIFIER OPENED_BRACKET args CLOSED_BRACKET OPENED_BRACE body CLOSED_BRACE
| VOID IDENTIFIER OPENED_BRACKET args CLOSED_BRACKET OPENED_BRACE body RETURN SEMI_COLON CLOSED_BRACE
| datatype IDENTIFIER OPENED_BRACKET args CLOSED_BRACKET OPENED_BRACE body RETURN IDENTIFIER SEMI_COLON CLOSED_BRACE
| datatype IDENTIFIER OPENED_BRACKET args CLOSED_BRACKET OPENED_BRACE body RETURN val SEMI_COLON CLOSED_BRACE
;
args : datatype IDENTIFIER COMMA args
| datatype IDENTIFIER
|
;
/* /////////////////////////////////////// */
/* Utils */
datatype : INT {$$ = NULL;}
| FLOAT {$$ = NULL;}
| STRING {$$ = NULL;}
| CHAR {$$ = NULL;}
| BOOLEAN {$$ = NULL;}
;
val : number { $$ = $1 ;}
| STRINGVALUE { $$ = con($1,3);}
| CHARVALUE { $$ = con($1,2);}
| boolean { $$ = $1 ;}
;
number : INTVALUE
| FLOATVALUE
;
boolean: TRUE
| FALSE
;
/* ///////////////////////////////////// */
%%
int yyerror(char *msg){
fprintf(stderr,"%s\n",msg);
exit(1);
}
int main(){
yyparse();
return 0;
}