-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavat.y
87 lines (74 loc) · 2.36 KB
/
javat.y
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
%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
int yyparse();
void yyerror(const char* s);
%}
%union {
int number;
float floating;
char* string;
}
%token<number> INT
%token<floating> FLOAT
%token<string> WORD LETTER DISALLOWED_INDENTIFIER
%token ADD SUBT MULT DIV LPAR RPAR SEMICOLON DONE EQUALS PUBLIC PRIVATE CLASS
%token LBRACE RBRACE QUOTE NEW
%type<number> expression
%type<floating> mixed_expression
%start prob
%%
prob:
| var_def
| var_def prob
| class_definition
| NEW { printf("New line\n"); }
;
var_def:
| WORD EQUALS quoted_string SEMICOLON NEW { printf("Str = Str\n"); }
| WORD EQUALS expression SEMICOLON NEW { printf("Str = Exp\n"); }
| WORD EQUALS mixed_expression SEMICOLON NEW { printf("Str = Mixed_Exp\n"); }
| LETTER EQUALS quoted_string SEMICOLON NEW { printf("Str = Str\n"); }
| LETTER EQUALS expression SEMICOLON NEW { printf("Str = Exp\n"); }
| LETTER EQUALS mixed_expression SEMICOLON NEW { printf("Str = Mixed_Exp\n"); }
class_definition:
| PUBLIC CLASS WORD LBRACE { printf("Public class\n"); }
| PRIVATE CLASS WORD LBRACE { printf("Private class\n"); }
| CLASS WORD LBRACE
mixed_expression: FLOAT { $$ = $1; }
| mixed_expression ADD mixed_expression { $$ = $1 + $3; }
| mixed_expression SUBT mixed_expression { $$ = $1 - $3; }
| mixed_expression MULT mixed_expression { $$ = $1 * $3; }
| mixed_expression DIV mixed_expression { $$ = $1 / $3; }
| LPAR mixed_expression RPAR { $$ = $2; }
| expression ADD mixed_expression { $$ = $1 + $3; }
| expression SUBT mixed_expression { $$ = $1 - $3; }
| expression MULT mixed_expression { $$ = $1 * $3; }
| expression DIV mixed_expression { $$ = $1 / $3; }
| mixed_expression ADD expression { $$ = $1 + $3; }
| mixed_expression SUBT expression { $$ = $1 - $3; }
| mixed_expression MULT expression { $$ = $1 * $3; }
| mixed_expression DIV expression { $$ = $1 / $3; }
| expression DIV expression { $$ = $1 / (float)$3; }
| FLOAT { $$ = $1; }
;
expression: INT { $$ = $1; }
| expression ADD expression { $$ = $1 + $3; }
| expression SUBT expression { $$ = $1 - $3; }
| expression MULT expression { $$ = $1 * $3; }
| LPAR expression RPAR { $$ = $2; }
| INT { $$ = $1; }
;
quoted_string:
| QUOTE WORD QUOTE
%%
int main() {
while(!feof(stdin))
yyparse();
return 0;
}
void yyerror(const char* s) {
fprintf(stderr, "Parse error: %s\n", s);
exit(1);
}