-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccproject.y
104 lines (94 loc) · 1.66 KB
/
ccproject.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
%{
void yyerror(char *);
int yylex(void);
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
%}
%token PLUS MINUS NL MUL DIV RB LB POW EQUAL LETTER TAB SCOMMENT BCOMMENTS BCOMMENTSE DELIMIT INTEGER CHARACTER FLOATER DOUBLER
%token <val> NUM
%token <val> LETTER
%type <val> E
%left EQUAL
%left PLUS MINUS
%left MUL DIV
%right POW
%union
{
int val;
char *a;
}
%%
PGM : PGM EQUAL {writestofile("=");}
| PGM LETTER {writestofile($2);}
| PGM E {writestofile($2);}
| PGM NL {writestofile("\n");}
| PGM TAB {writestofile(" ");}
| PGM DOUBLER {writestofile("double");}
| PGM FLOATER {writestofile("float");}
| PGM CHARACTER {writestofile("char");}
| PGM INTEGER {writestofile("int");}
| PGM DELIMIT {writestofile(";");}
| PGM SCOMMENT {writestofile("//");}
| PGM BCOMMENTS {writestofile("\\*");}
| PGM BCOMMENTSE {writestofile("*\\");}
|
;
E : E PLUS E {$$ = $1 + $3;}
| NUM MINUS NUM {$$ = $1 - $3;}
| E DIV E {$$ = $1/$3;}
| E MUL E {$$ = $1 * $3;}
| NUM {$$ = $1;}
| LB E RB {$$ = $2;}
| E POW E {$$ = power($1,$3);}
;
%%
void yyerror(char *s)
{
printf("%s \n", s);
}
int main()
{
yyparse();
return 0;
}
int power(int a, int b)
{
int sum =1;
for(int i=1;i<=b;i++)
{
sum = sum*a;
}
return(sum);
}
void writectofile(char a)
{
FILE *fptr;
fptr = fopen("testproject.txt","a");
if(fptr==NULL)
{
printf("exit");
exit(1);
}
fprintf(fptr,"%c",a);
fclose(fptr);
}
void writestofile(char a[])
{
FILE *fptr;
fptr = fopen("testproject1.txt","a");
if(fptr==NULL)
{
printf("exit");
exit(1);
}
if(!(strcmp(a,"\n")))
{
fprintf(fptr,"\n");
}
else
{
fprintf(fptr,"%s",a);
}
fclose(fptr);
}