Skip to content

Commit af3c589

Browse files
committed
Merge branch 'DeathStroke19891-main'
Fixed shift reduce conflicts in Program 3a
2 parents f871fca + 4294f5f commit af3c589

File tree

6 files changed

+107
-55
lines changed

6 files changed

+107
-55
lines changed

Prog3/a/lex.l

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
%}
44
%%
55
"for" return FOR;
6+
"int"|"float"|"double"|"bool" return TYPE;
7+
">"|"<"|">="|"<="|"=="|"!=" return OP;
68
[a-zA-Z]* return IDEN;
79
[0-9]+ return NUM;
8-
[\n ] ;
10+
[\n\t ] ;
911
. return yytext[0];
10-
%%
12+
%%

Prog3/a/yacc.y

+44-23
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,50 @@
55
int yyerror();
66
int cnt=0;
77
%}
8-
%token FOR IDEN NUM
8+
%token FOR IDEN NUM TYPE OP
9+
%left '+' '-'
10+
%left '*' '/'
911
%%
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 ;
3152
%%
3253
int main()
3354
{
@@ -40,4 +61,4 @@ int yyerror()
4061
{
4162
printf("Invalid\n");
4263
exit(0);
43-
}
64+
}

Prog3/b/lex.l

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77

88
"int"|"float"|"char"|"void" {return TYPE;}
99

10-
"return" {return RET;}
11-
1210
[a-zA-Z][a-zA-Z0-9_]* {return IDEN;}
1311

1412
[0-9]+ {return NUM;}
1513

1614
. {return yytext[0];}
17-
%%
15+
%%

Prog3/b/yacc.y

+36-25
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,42 @@
55
int yylex();
66
%}
77

8-
%token TYPE IDEN NUM RET
9-
8+
%token TYPE IDEN NUM
9+
%left '+' '-'
10+
%left '*' '/'
11+
1012
%%
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 ;
3344
%%
3445
int main()
3546
{
@@ -42,4 +53,4 @@ int yyerror()
4253
{
4354
printf("ERROR\n");
4455
exit(0);
45-
}
56+
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Input format for each of the programs
6363
```
6464
> b. Validate function defination
6565
```bash
66-
int main(){}
66+
int main(){};
6767
int foo(int a){}
6868
int bar(int a,int b){return a;}
6969
```

docs/README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Input format for each of the programs
6363
```
6464
> b. Validate function defination
6565
```bash
66-
int main(){}
66+
int main(){};
6767
int foo(int a){}
6868
int bar(int a,int b){return a;}
6969
```
@@ -80,3 +80,23 @@ Input format for each of the programs
8080
```bash
8181
./outputfilename.out < input.txt
8282
```
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.

0 commit comments

Comments
 (0)