File tree 6 files changed +107
-55
lines changed
6 files changed +107
-55
lines changed Original file line number Diff line number Diff line change 3
3
%}
4
4
%%
5
5
" for" return FOR;
6
+ " int" | " float" | " double" | " bool" return TYPE;
7
+ " >" | " <" | " >=" | " <=" | " ==" | " !=" return OP;
6
8
[a -zA -Z ]* return IDEN;
7
9
[0 -9 ]+ return NUM;
8
- [\n ] ;
10
+ [\n\t ] ;
9
11
. return yytext[0 ];
10
- %%
12
+ %%
Original file line number Diff line number Diff line change 5
5
int yyerror ();
6
6
int cnt=0 ;
7
7
%}
8
- %token FOR IDEN NUM
8
+ %token FOR IDEN NUM TYPE OP
9
+ %left ' +' ' -'
10
+ %left ' *' ' /'
9
11
%%
10
- S :I
11
- ;
12
- I :FOR A B {cnt++;}
13
- ;
14
- A :' (' E ' ;' E ' ;' E ' )'
15
- ;
16
- E :IDEN Z IDEN
17
- | IDEN Z NUM
18
- | IDEN U
19
- | IDEN
20
- ;
21
- Z :' =' | ' >' | ' <' | ' <' ' =' | ' >' ' =' | ' =' ' +' | ' =' ' -'
22
- ;
23
- U :' +' ' +' | ' -' ' -'
24
- ;
25
- B :B B
26
- | ' {' B ' }'
27
- | I
28
- | E ' ;'
29
- |
30
- ;
12
+
13
+ // Tokens
14
+
15
+ // FOR -> for
16
+ // IDEN -> identifier
17
+ // NUM -> number
18
+ // TYPE -> datatype
19
+ // OP -> relational operator
20
+
21
+ // Non-terminals
22
+
23
+ // S -> Start symbol
24
+ // BODY -> Body of For loop
25
+ // COND -> Condition
26
+ // S1 -> Single Statement
27
+ // SS -> Set of statements
28
+ // T -> Term
29
+ // E -> Expression
30
+ // F -> For loop block
31
+ // DA -> Declaration or assignment
32
+ // DECL -> Declaration
33
+ // ASSGN -> Assignment
34
+
35
+ S :F ;
36
+ F :FOR ' (' DA ' ;' COND ' ;' S1 ' )' BODY { cnt++; } |
37
+ FOR ' (' ' ;' COND ' ;' S1 ' )' BODY { cnt++; } |
38
+ FOR ' (' DA ' ;' ' ;' S1 ' )' BODY { cnt++; } |
39
+ FOR ' (' ' ;' ' ;' S1 ' )' BODY { cnt++; } ;
40
+
41
+ DA :DECL | ASSGN
42
+ DECL : TYPE IDEN | TYPE ASSGN ;
43
+ ASSGN : IDEN ' =' E ;
44
+ COND : T OP T ;
45
+ T : NUM | IDEN ;
46
+
47
+ BODY : S1 ' ;' | ' {' SS ' }' | F | ' ;' ;
48
+
49
+ SS : S1 ' ;' SS | F SS | ;
50
+ S1 : ASSGN | E | DECL ;
51
+ E : E ' +' E | E ' -' E | E ' *' E | E ' /' E | ' -' ' -' E | ' +' ' +' E | E ' +' ' +' | E ' -' ' -' | T ;
31
52
%%
32
53
int main ()
33
54
{
@@ -40,4 +61,4 @@ int yyerror()
40
61
{
41
62
printf (" Invalid\n " );
42
63
exit (0 );
43
- }
64
+ }
Original file line number Diff line number Diff line change 7
7
8
8
" int" | " float" | " char" | " void" {return TYPE;}
9
9
10
- " return" {return RET;}
11
-
12
10
[a -zA -Z ][a -zA -Z0 -9_ ]* {return IDEN;}
13
11
14
12
[0 -9 ]+ {return NUM;}
15
13
16
14
. {return yytext[0 ];}
17
- %%
15
+ %%
Original file line number Diff line number Diff line change 5
5
int yylex ();
6
6
%}
7
7
8
- %token TYPE IDEN NUM RET
9
-
8
+ %token TYPE IDEN NUM
9
+ %left ' +' ' -'
10
+ %left ' *' ' /'
11
+
10
12
%%
11
- S : FUN {printf(" Accepted\n " );exit(0 );}
12
- ;
13
- FUN : TYPE IDEN ' (' PARAM ' )' ' {' BODY ' }'
14
- ;
15
- PARAM : PARAM ' ,' PARAM
16
- | TYPE IDEN
17
- |
18
- ;
19
- BODY : BODY BODY
20
- | PARAM ' ;'
21
- | E ' ;'
22
- | RET E ' ;'
23
- |
24
- ;
25
- E : IDEN ' =' E
26
- | E ' +' E
27
- | E ' -' E
28
- | E ' *' E
29
- | E ' /' E
30
- | IDEN
31
- | NUM
32
- ;
13
+ // Tokens
14
+
15
+ // IDEN -> identifier
16
+ // NUM -> number
17
+ // TYPE -> datatype
18
+
19
+ // Non-terminals
20
+
21
+ // S -> Start symbol
22
+ // FUN -> function block
23
+ // PARAMS -> parameters
24
+ // PARAM -> parameter
25
+ // BODY -> Function body
26
+ // S1 -> Single Statement
27
+ // SS -> Set of statements
28
+ // T -> Term
29
+ // E -> Expression
30
+ // DECL -> Declaration
31
+ // ASSGN -> Assignment
32
+
33
+ S : FUN { printf(" Accepted\n " ); exit(0 ); } ;
34
+ FUN : TYPE IDEN ' (' PARAMS ' )' BODY ;
35
+ BODY : S1 ' ;' | ' {' SS ' }'
36
+ PARAMS : PARAM ' ,' PARAMS | PARAM | ;
37
+ PARAM : TYPE IDEN ;
38
+ SS : S1 ' ;' SS | ;
39
+ S1 : ASSGN | E | DECL ;
40
+ DECL : TYPE IDEN | TYPE ASSGN ;
41
+ ASSGN : IDEN ' =' E ;
42
+ E : E ' +' E | E ' -' E | E ' *' E | E ' /' E | ' -' ' -' E | ' +' ' +' E | E ' +' ' +' | E ' -' ' -' | T ;
43
+ T : NUM | IDEN ;
33
44
%%
34
45
int main ()
35
46
{
@@ -42,4 +53,4 @@ int yyerror()
42
53
{
43
54
printf (" ERROR\n " );
44
55
exit (0 );
45
- }
56
+ }
Original file line number Diff line number Diff line change @@ -63,7 +63,7 @@ Input format for each of the programs
63
63
` ` `
64
64
> b. Validate function defination
65
65
` ` ` bash
66
- int main (){}
66
+ int main (){};
67
67
int foo(int a){}
68
68
int bar(int a,int b){return a; }
69
69
` ` `
Original file line number Diff line number Diff line change @@ -63,7 +63,7 @@ Input format for each of the programs
63
63
` ` `
64
64
> b. Validate function defination
65
65
` ` ` bash
66
- int main (){}
66
+ int main (){};
67
67
int foo(int a){}
68
68
int bar(int a,int b){return a; }
69
69
` ` `
@@ -80,3 +80,23 @@ Input format for each of the programs
80
80
` ` ` bash
81
81
./outputfilename.out < input.txt
82
82
` ` `
83
+
84
+ ---
85
+
86
+ # Change Log
87
+
88
+ # # Version 1
89
+
90
+ - Added all 5 programs
91
+ - Had some conflicts in Shift/Reduce and Reduce/ Reduce
92
+
93
+ # # Version 2
94
+
95
+ - @DeathStroke1991(https://github.com/DeathStroke19891) Fixed the issues in 3a and 3b
96
+ - Added comments on grammer.
97
+
98
+ ---
99
+
100
+ # Contribution
101
+
102
+ This repo is open for contributions. Please open an Issue or open a PR with appropriate edits.
You can’t perform that action at this time.
0 commit comments