-
Notifications
You must be signed in to change notification settings - Fork 2
/
python_error_detector.c
133 lines (108 loc) · 4.56 KB
/
python_error_detector.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ERRORS 20
typedef struct {
int line_number;
char *message;
} Error;
Error errors[MAX_ERRORS];
int num_errors = 0;
void add_error(int line_number, char *message) {
if (num_errors < MAX_ERRORS) {
errors[num_errors].line_number = line_number;
errors[num_errors].message = message;
num_errors++;
}
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <python_program>\n", argv[0]);
exit(1);
}
char *python_program = argv[1];
// Open the Python program file.
FILE *fp = fopen(python_program, "r");
if (fp == NULL) {
printf("Error: Could not open Python program file '%s'\n", python_program);
exit(1);
}
// Read the Python program line by line.
char line[1024];
int line_number = 1;
int in_while_loop = 0; // Flag to track if we are inside a while loop
// ----------- Python Error Detector -----------
printf("Python Error Detector\n");
printf("----------------------\n\n");
// Analyzing Python Program
printf("Analyzing Python program: %s\n\n", python_program);
while (fgets(line, sizeof(line), fp) != NULL) {
// Check for common syntax errors.
if (strstr(line, "if True:") && !strstr(line, ":")) {
add_error(line_number, "Expected colon (:) after `if` statement.");
} else if (strstr(line, "while True:") && !strstr(line, ":")) {
add_error(line_number, "Expected colon (:) after `while` statement.");
} else if (strstr(line, "def f(") && !strstr(line, "):")) {
add_error(line_number, "Expected colon (:) after function definition header.");
}
// Add more error checks here:
// Example 1: Check for missing indentation
if (strstr(line, "if True:") && !strstr(line, " ")) {
add_error(line_number, "Missing indentation in the 'if' block.");
}
// Example 2: Check for invalid variable names
if (strstr(line, "var @InvalidVariableName")) {
add_error(line_number, "Invalid variable name.");
}
// Example 3: Check for incorrect function call
if (strstr(line, "foo(") && !strstr(line, ")")) {
add_error(line_number, "Expected closing parenthesis in function call.");
}
// Example 4: Check for missing 'return' statement
if (strstr(line, "def func():") && !strstr(line, "return")) {
add_error(line_number, "Expected 'return' statement in function.");
}
// Example 5: Check for missing 'import' statement
if (strstr(line, "import module") && !strstr(line, "module")) {
add_error(line_number, "Missing 'import' statement.");
}
// Example 6: Check for missing ':' in 'for' loop
if (strstr(line, "for i in range(10)") && !strstr(line, ":")) {
add_error(line_number, "Expected ':' after 'for' loop header.");
}
// Example 7: Check for invalid syntax
if (strstr(line, "invalid syntax here!")) {
add_error(line_number, "Invalid syntax detected.");
}
// Example 8: Check for missing closing parenthesis
if (strstr(line, "function(") && !strstr(line, ")")) {
add_error(line_number, "Expected closing parenthesis.");
}
// Example 9: Check for missing 'class' definition
if (strstr(line, "class MyClass:") && !strstr(line, ":")) {
add_error(line_number, "Expected colon (:) after class definition.");
}
// Example 10: Check for missing 'pass' statement
if (strstr(line, "if condition:") && !strstr(line, "pass")) {
add_error(line_number, "Expected 'pass' statement after 'if' condition.");
}
// Check if we enter or exit a while loop
if (strstr(line, "while True:")) {
in_while_loop = 1;
} else if (strstr(line, "break")) {
in_while_loop = 0;
}
// Increment the line number.
line_number++;
}
// Print any errors that were found.
if (num_errors > 0) {
printf("\nErrors found in Python program '%s':\n", python_program);
for (int i = 0; i < num_errors; i++) {
printf("Line %d: %s\n", errors[i].line_number, errors[i].message);
}
} else {
printf("\nNo errors found in Python program '%s'\n", python_program);
}
return 0;
}