|
1 | 1 | %{
|
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); |
7 | 9 | %}
|
8 |
| -%token FOR IDEN NUM TYPE OP |
9 |
| -%left '+' '-' |
10 |
| -%left '*' '/' |
| 10 | + |
| 11 | +%token FOR IDEN NUM OP |
| 12 | + |
11 | 13 | %%
|
12 | 14 |
|
13 |
| -// Tokens |
| 15 | +// List of statements |
| 16 | +STMTS: STMT |
| 17 | + | STMTS STMT |
| 18 | + ; |
14 | 19 |
|
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 | + ; |
20 | 27 |
|
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 | + ; |
22 | 42 |
|
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 | + ; |
34 | 48 |
|
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 | + ; |
40 | 55 |
|
41 |
| -DA:DECL|ASSGN |
42 |
| -DECL: TYPE IDEN | TYPE ASSGN; |
43 |
| -ASSGN : IDEN '=' E; |
44 |
| -COND : T OP T; |
45 |
| -T : NUM | IDEN ; |
46 | 56 |
|
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 | + ; |
48 | 65 |
|
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 ; |
52 | 66 | %%
|
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; |
59 | 75 | }
|
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); |
64 | 81 | }
|
0 commit comments