This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
compiler.y
112 lines (101 loc) · 2.01 KB
/
compiler.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
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
%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
int yyerror();
extern FILE *yyin;
%}
%token TRUE FALSE STRING INTEGER OB CB NOT EQ LET LTE LT COMP DIV OCB CCB SS COMMA COL ASSIGN AT DOT IF THEN ELSE FI WHILE LOOP POOL IN CASE OF TYPEOF ESAC NEW ISVOID SUB ADD MUL CLASS INHERITS TYPE ID
%right ASSIGN
%precedence NOT
%nonassoc LT EQ LTE
%left ADD SUB
%left MUL DIV
%precedence ISVOID
%precedence COMP
%precedence AT
%precedence DoT
%%
stmt : program;
features: feature SS features
|
;
classes: class SS classes1
;
classes1: class SS classes1
|
;
formals: COMMA formal formals
|
;
exprs: COMMA expr exprs
|
;
program: classes
;
class: CLASS TYPE INHERITS TYPE OCB features CCB
|
CLASS TYPE OCB features CCB
;
feature: ID OB formal formals CB COL TYPE OCB expr CCB
| ID OB CB COL TYPE OCB expr CCB
| ID COL TYPE ASSIGN expr
| ID COL TYPE
;
formal: ID COL TYPE
;
expressplus: expr SS expressplus | ;
express: COMMA ID COL TYPE ASSIGN expr express|
;
cases: ID COL TYPE TYPEOF expr SS cases1;
cases1: ID COL TYPE TYPEOF expr SS cases1 | ;
expr : TRUE
| FALSE
| STRING
| INTEGER
| ID
| OB expr CB
|NOT expr
| expr EQ expr
| expr LTE expr
| expr LT expr
| COMP expr
| expr DIV expr
| expr MUL expr
| expr SUB expr
| expr ADD expr
| ISVOID expr
| NEW TYPE
| CASE expr OF cases ESAC
| LET ID COL TYPE ASSIGN expr express IN expr
| LET ID COL TYPE express IN expr
| OCB expressplus CCB
| WHILE expr LOOP expr POOL
| IF expr THEN expr ELSE expr FI
| ID ASSIGN expr
| expr AT TYPE DOT ID OB expr exprs CB
| expr DOT ID OB expr exprs CB
| expr DOT ID OB CB
| expr AT TYPE DOT ID OB CB
| ID OB expr exprs CB
| ID OB CB
;
%%
int yyerror(char *msg)
{
printf("Invalid Expression: The issue is this ---> %s", msg);
return 1;
}
void main()
{
yyin=fopen("SampleInput.txt", "r");
do
{
if(yyparse())
{
printf("\n Failure");
exit(0);
}
} while (!feof(yyin));
printf("Success");
}