-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop-parser.y
More file actions
74 lines (56 loc) · 1.36 KB
/
loop-parser.y
File metadata and controls
74 lines (56 loc) · 1.36 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
// cases covered in here
// for i in var
// for i in range(func())
// for i in range(1,50,10)
// for i in xrange(1,10)
// for i in xrange(1,10,2)
%{
#include <stdio.h>
#define border printf("\n---------------------------------------------------------------------------------\n")
int yydebug=1;
%}
%token ID NUMBER
%token LBRACKET RBRACKET COLON FOR IN RANGE XRANGE COMMA
%token PRINT RARE
/// set precedence
%right EQ
%left PLUS MINUS
%left MUL DIVIDE
%%
start : stmt COLON { printf("\nThis is a valid python expression\n"); border; YYACCEPT;}
stmt: simple changes
simple: FOR identifier IN
changes: identifier
|range
| xrange
range: RANGE LBRACKET myrange RBRACKET
| RANGE LBRACKET myfunc RBRACKET
myfunc: identifier LBRACKET RBRACKET
myrange : NUMBER
| NUMBER COMMA NUMBER
| NUMBER COMMA NUMBER COMMA NUMBER
xrange: XRANGE LBRACKET myrange RBRACKET
identifier: ID
| keyword { yyerror("\nkeyword can't be used as a identifier\n"); YYABORT; }
keyword: PRINT
| FOR
| IN
| RANGE
| XRANGE
| RARE
%%
main() {
printf("\n--------------------------- PYTHON FOR LOOP PARSER ---------------------------\n");
printf("\nEnter FOR loop: \n");
return yyparse();
}
yyerror(s)
char *s;
{
printf("\n%s \n", s);
border;
}
yywrap()
{
return(1);
}