-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntaxpart.y
70 lines (52 loc) · 1.43 KB
/
syntaxpart.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
%{
#include<stdio.h>
#include<stdlib.h>
extern int yylineno;
%}
%token PhpStart PhpEnd ID INT FLOAT STRING LE GE ET NE POW IF ELSE FUNC WHILE ECHO_T
%%
ProgramBlock: PhpStart StatementList PhpEnd {printf("Code Compiled Successfully"); exit(0);}
StatementList : Statement StatementList
|
;
Statement: DecStmt
| BoolExp
| RepStmt
| DecStruct
| OutStmt
| FuncStmt;
DecStmt: ID '=' RHS ';';
RHS: Value OP RHS
| Value;
Value: INT
| FLOAT
| STRING
| ID;
OP: '+'
| '-'
| '*'
| POW
| '/';
BoolExp: Exp '>' Exp { $$ = $1 > $3; }
| Exp '<' Exp { $$ = $1 < $3; }
| Exp ET Exp { $$ = $1 == $3; }
| Exp NE Exp { $$ = $1 != $3; }
| Exp GE Exp { $$ = $1 >= $3; }
| Exp LE Exp { $$ = $1 <= $3; }
;
Exp: INT;
RepStmt: WHILE '(' BoolExp ')' '{' StatementList '}';
DecStruct: IF '(' BoolExp ')' '{' StatementList '}'
| IF '(' BoolExp ')' '{' StatementList '}' ELSE '{' StatementList '}';
OutStmt: ECHO_T Output ';' ;
Output: STRING
| ID
| STRING '.' ID '.' STRING;
FuncStmt: FUNC ID '(' ')' '{' StatementList '}';
%%
void yyerror(const char *str)
{
printf("Code cannot be compiled\n");
fprintf(stderr,"Error type: %s\n",str);
fprintf(stderr,"Line number: %d\n",yylineno);
}