Skip to content

Commit 742e13f

Browse files
committed
Added grammer fixes for nesting counts
1 parent d16ea8a commit 742e13f

File tree

5 files changed

+140
-92
lines changed

5 files changed

+140
-92
lines changed

Lab New Codes/nestedIf/lex.l

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
%{
2-
#include "y.tab.h"
2+
#include "y.tab.h"
33
%}
4+
45
%%
5-
"if" {return IF;}
6-
[a-zA-Z]* {return IDEN;}
7-
[0-9]+ {return NUM;}
8-
[\n ] ;
9-
. {return yytext[0];}
10-
%%
6+
"if" { return IF; }
7+
[a-zA-Z][a-zA-Z0-9]* { return IDEN; }
8+
[0-9]+ { return NUM; }
9+
[ \t\n]+ ;
10+
. { return yytext[0]; }
11+
%%
12+
13+
int yywrap() {
14+
return 1;
15+
}

Lab New Codes/nestedIf/yacc.y

+59-32
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,68 @@
11
%{
2-
#include<stdio.h>
3-
#include<stdlib.h>
4-
int cnt=0;
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
int total_if_count = 0;
5+
int current_nesting = 0;
6+
int max_nesting = 0;
7+
int yylex();
8+
int yyerror(const char *s);
59
%}
10+
611
%token IF IDEN NUM
12+
713
%%
8-
S:I
9-
;
10-
I:IF A B {cnt++;}
11-
;
12-
A:'('E')'
13-
;
14-
E:IDEN Z IDEN
15-
|IDEN Z NUM
16-
|IDEN U
17-
|IDEN
18-
;
19-
Z:'='|'<'|'>'|'<''='|'>''='|'=''+'|'=''-'
20-
;
21-
U:'+''+'|'-''-'
22-
;
23-
B:B B
24-
|'{'B'}'
25-
|I
26-
|E';'
27-
|
28-
;
14+
15+
STMTS: STMT
16+
| STMTS STMT
17+
;
18+
19+
STMT: IFSTMT
20+
| IDEN '=' EXPR ';'
21+
| IDEN ';'
22+
| '{' STMTS '}'
23+
| ';'
24+
;
25+
26+
IFSTMT: IF '(' COND ')'
27+
{
28+
total_if_count++;
29+
current_nesting++;
30+
if (current_nesting > max_nesting) {
31+
max_nesting = current_nesting;
32+
}
33+
}
34+
STMT
35+
{
36+
current_nesting--;
37+
}
38+
;
39+
40+
COND: IDEN CMP IDEN
41+
| IDEN CMP NUM
42+
| IDEN
43+
| NUM
44+
;
45+
46+
CMP: '=' | '<' | '>' | '<''=' | '>''=' | '!''='
47+
;
48+
49+
EXPR: IDEN
50+
| NUM
51+
| IDEN '+' IDEN
52+
| IDEN '-' IDEN
53+
;
54+
2955
%%
30-
int main()
31-
{
32-
printf("Enter the snippet:\n");
56+
57+
int main() {
58+
printf("Enter the code snippet (Ctrl+D to end input on Unix, Ctrl+Z on Windows):\n");
3359
yyparse();
34-
printf("Count of if is %d\n",cnt);
60+
printf("\nTotal IF STMTS: %d\n", total_if_count);
61+
printf("Maximum nesting level: %d\n", max_nesting);
3562
return 0;
3663
}
37-
int yyerror()
38-
{
39-
printf("Invalid\n");
40-
exit(0);
64+
65+
int yyerror(const char *s) {
66+
fprintf(stderr, "Parse error: %s\n", s);
67+
exit(1);
4168
}

Prog3/a/lex.l

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
%}
44
%%
55
"for" return FOR;
6-
"int"|"float"|"double"|"bool" return TYPE;
76
">"|"<"|">="|"<="|"=="|"!=" return OP;
87
[a-zA-Z]* return IDEN;
98
[0-9]+ return NUM;

Prog3/a/yacc.y

+67-50
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,81 @@
11
%{
2-
#include<stdio.h>
3-
#include<stdlib.h>
4-
int yylex();
5-
int yyerror();
6-
int cnt=0;
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
int total_for_count = 0; // Total count of for loops
5+
int current_nesting = 0; // Current level of nesting
6+
int max_nesting = 0; // Maximum level of nesting encountered
7+
int yylex();
8+
int yyerror(const char *s);
79
%}
8-
%token FOR IDEN NUM TYPE OP
9-
%left '+' '-'
10-
%left '*' '/'
10+
11+
%token FOR IDEN NUM OP
12+
1113
%%
1214

13-
// Tokens
15+
// List of statements
16+
STMTS: STMT
17+
| STMTS STMT
18+
;
1419

15-
// FOR -> for
16-
// IDEN -> identifier
17-
// NUM -> number
18-
// TYPE -> datatype
19-
// OP -> relational operator
20+
// Possible statements
21+
STMT: FORSTMT // For loop
22+
| IDEN '=' EXPR ';' // Assignment
23+
| IDEN ';' // Identifier followed by semicolon
24+
| '{' STMTS '}' // Block of statements
25+
| ';' // Empty statement
26+
;
2027

21-
// Non-terminals
28+
// For loop structure
29+
FORSTMT: FOR '(' ASSGN ';' COND ';' ASSGN ')'
30+
{
31+
total_for_count++; // Increment for loop count
32+
current_nesting++; // Increase nesting level
33+
if (current_nesting > max_nesting) {
34+
max_nesting = current_nesting; // Track maximum nesting level
35+
}
36+
}
37+
STMT
38+
{
39+
current_nesting--; // Decrease nesting level after loop ends
40+
}
41+
;
2242

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
43+
// Assignment inside for loop (initialization or update)
44+
ASSGN: IDEN '=' EXPR
45+
| IDEN
46+
|
47+
;
3448

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++; } ;
49+
// Condition inside for loop
50+
COND: IDEN OP IDEN
51+
| IDEN OP NUM
52+
| IDEN
53+
| NUM
54+
;
4055

41-
DA:DECL|ASSGN
42-
DECL: TYPE IDEN | TYPE ASSGN;
43-
ASSGN : IDEN '=' E;
44-
COND : T OP T;
45-
T : NUM | IDEN ;
4656

47-
BODY: S1';' | '{'SS'}' | F |';';
57+
// Expressions (simple arithmetic or identifier/number)
58+
EXPR: IDEN
59+
| NUM
60+
| IDEN '+' IDEN
61+
| IDEN '-' IDEN
62+
| IDEN '*' IDEN
63+
| IDEN '/' IDEN
64+
;
4865

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 ;
5266
%%
53-
int main()
54-
{
55-
printf("Enter the snippet:\n");
56-
yyparse();
57-
printf("Count of for : %d\n",cnt);
58-
return 0;
67+
68+
// Main function to handle user input and parse
69+
int main() {
70+
printf("Enter the code snippet (Ctrl+D to end input on Unix, Ctrl+Z on Windows):\n");
71+
yyparse();
72+
printf("\nTotal FOR loops: %d\n", total_for_count);
73+
printf("Maximum nesting level: %d\n", max_nesting);
74+
return 0;
5975
}
60-
int yyerror()
61-
{
62-
printf("Invalid\n");
63-
exit(0);
76+
77+
// Error handling
78+
int yyerror(const char *s) {
79+
fprintf(stderr, "Parse error: %s\n", s);
80+
exit(1);
6481
}

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ Input format for each of the programs
7575

7676
3. > a. Count the number of valid nested FOR loops
7777
```bash
78-
for(a;b;c){}
79-
for(a;b;c){for(a;b;c){d;}}
78+
for(a;b<c;c);
79+
for(i=0;i<10;i){for(j=0;j<10;j);}
8080
```
8181
> b. Validate function defination
8282
```bash

0 commit comments

Comments
 (0)