-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyClean_DSL_Language_Guide.txt
More file actions
523 lines (398 loc) · 13.2 KB
/
Copy pathPyClean_DSL_Language_Guide.txt
File metadata and controls
523 lines (398 loc) · 13.2 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
================================================================================
PYCLEAN DSL - LANGUAGE GUIDE
Domain-Specific Language for Data Cleaning Operations
================================================================================
TABLE OF CONTENTS
-----------------
1. Introduction
2. Language Syntax & Keywords
3. Statement Types
4. Data Types
5. Complete Examples
6. Execution Flow
7. Error Handling
8. Best Practices
================================================================================
1. INTRODUCTION
================================================================================
PyClean DSL is a domain-specific language designed for data cleaning operations
on CSV files. It provides a simple, intuitive syntax for common data cleaning
tasks without requiring programming knowledge.
Purpose:
--------
- Clean and transform CSV data
- Fill missing values (nulls)
- Remove duplicates
- Validate data ranges
- Transform text (trim, case conversion)
Target Users:
-------------
- Data analysts
- Data scientists
- Business analysts
- Anyone working with messy CSV data
================================================================================
2. LANGUAGE SYNTAX & KEYWORDS
================================================================================
RESERVED KEYWORDS:
------------------
CLEAN - Start a cleaning process
FILL_NULL - Fill null/empty values
WITH - Specify fill value
REMOVE_DUPLICATES - Remove duplicate rows
COLUMN - Target a specific column
TRIM - Remove leading/trailing whitespace
UPPERCASE - Convert text to uppercase
LOWERCASE - Convert text to lowercase
VALIDATE - Validate data against rules
range - Range validation function
OPERATORS:
----------
; - Statement terminator (required)
, - Parameter separator
( ) - Function parameters
" " - String literals
COMMENTS:
---------
// - Single-line comment
/* */ - Multi-line comment (if supported)
================================================================================
3. STATEMENT TYPES
================================================================================
3.1 CLEAN STATEMENT
-------------------
Purpose: Initialize the cleaning process for a dataset
Syntax:
CLEAN dataset_name;
Example:
CLEAN dataset;
CLEAN mydata;
Notes:
- Must be the first statement in most scripts
- dataset_name is an identifier (usually "dataset")
- Does not perform any actual cleaning, just declares intent
3.2 FILL_NULL STATEMENT
------------------------
Purpose: Fill empty/null values in a specific column with a default value
Syntax:
FILL_NULL column_name WITH value;
Examples:
FILL_NULL age WITH 0;
FILL_NULL name WITH "Unknown";
FILL_NULL salary WITH 50000;
FILL_NULL email WITH "no-email@example.com";
Supported Value Types:
- Numbers: FILL_NULL age WITH 25;
- Strings: FILL_NULL name WITH "Default Name";
- Decimals: FILL_NULL price WITH 99.99;
Notes:
- Column name must exist in the CSV
- String values must be in double quotes
- Numbers don't need quotes
- Only fills cells that are empty or null
3.3 REMOVE_DUPLICATES STATEMENT
--------------------------------
Purpose: Remove duplicate rows from the dataset
Syntax:
REMOVE_DUPLICATES;
Example:
REMOVE_DUPLICATES;
Notes:
- Removes rows that are completely identical
- Keeps the first occurrence
- No parameters needed
- Works on entire rows, not specific columns
3.4 COLUMN TRANSFORM STATEMENTS
--------------------------------
Purpose: Apply transformations to a specific column
Syntax:
COLUMN column_name operation;
Operations Available:
---------------------
a) TRIM - Remove leading and trailing whitespace
COLUMN name TRIM;
COLUMN email TRIM;
b) UPPERCASE - Convert all text to uppercase
COLUMN name UPPERCASE;
COLUMN department UPPERCASE;
c) LOWERCASE - Convert all text to lowercase
COLUMN email LOWERCASE;
COLUMN username LOWERCASE;
Examples:
COLUMN name TRIM;
COLUMN name UPPERCASE;
COLUMN email LOWERCASE;
COLUMN address TRIM;
Notes:
- Operations are applied to all rows in the column
- Column must exist in the dataset
- Converts non-string values to string first
- Multiple operations can be chained on same column
3.5 VALIDATE STATEMENT
-----------------------
Purpose: Validate data and remove invalid rows
Syntax:
COLUMN column_name VALIDATE validation_type(parameters);
Validation Types:
-----------------
a) range - Validate numeric values are within a range
Syntax: range(min, max)
Examples:
COLUMN age VALIDATE range(0, 120);
COLUMN temperature VALIDATE range(-50, 50);
COLUMN score VALIDATE range(0, 100);
Parameters:
- min: Minimum valid value (inclusive)
- max: Maximum valid value (inclusive)
Behavior:
- Removes rows where value < min OR value > max
- Converts column to numeric type
- Non-numeric values are treated as invalid
Notes:
- Currently only range validation is supported
- Future versions may support: regex, email, phone, etc.
- Invalid rows are completely removed from dataset
================================================================================
4. DATA TYPES
================================================================================
4.1 IDENTIFIERS
---------------
- Column names: age, name, email, salary
- Dataset names: dataset, mydata
- Must start with letter or underscore
- Can contain letters, numbers, underscores
4.2 STRING LITERALS
-------------------
- Enclosed in double quotes: "Hello"
- Examples: "Unknown", "no-email@example.com", "0"
- Cannot contain unescaped quotes
4.3 NUMERIC LITERALS
--------------------
- Integers: 0, 25, 100, -5
- Decimals: 99.99, 3.14, -2.5
- Used in FILL_NULL and VALIDATE statements
4.4 SPECIAL VALUES
------------------
- Empty/Null: Represented as empty cells in CSV
- NaN: Result of failed numeric conversions
================================================================================
5. COMPLETE EXAMPLES
================================================================================
EXAMPLE 1: Basic Data Cleaning
-------------------------------
// Clean employee data
CLEAN dataset;
// Fill missing values
FILL_NULL name WITH "Unknown Employee";
FILL_NULL age WITH 25;
FILL_NULL department WITH "Unassigned";
// Remove duplicates
REMOVE_DUPLICATES;
// Clean up text
COLUMN name TRIM;
COLUMN name UPPERCASE;
COLUMN email LOWERCASE;
EXAMPLE 2: Sales Data Processing
---------------------------------
CLEAN sales_data;
// Handle nulls
FILL_NULL product WITH "Unknown Product";
FILL_NULL quantity WITH 0;
FILL_NULL price WITH 0.0;
// Standardize text
COLUMN product TRIM;
COLUMN product UPPERCASE;
COLUMN customer_email LOWERCASE;
COLUMN customer_email TRIM;
// Remove invalid data
COLUMN quantity VALIDATE range(0, 10000);
COLUMN price VALIDATE range(0, 999999);
EXAMPLE 3: Student Records
---------------------------
CLEAN student_records;
// Fill missing grades
FILL_NULL grade WITH 0;
FILL_NULL attendance WITH 0;
// Clean names
COLUMN first_name TRIM;
COLUMN last_name TRIM;
COLUMN first_name UPPERCASE;
COLUMN last_name UPPERCASE;
// Email standardization
COLUMN email LOWERCASE;
COLUMN email TRIM;
// Validate ranges
COLUMN grade VALIDATE range(0, 100);
COLUMN attendance VALIDATE range(0, 100);
COLUMN age VALIDATE range(16, 25);
// Remove duplicates
REMOVE_DUPLICATES;
EXAMPLE 4: E-commerce Products
-------------------------------
CLEAN products;
// Default values
FILL_NULL product_name WITH "Untitled Product";
FILL_NULL price WITH 0.00;
FILL_NULL stock WITH 0;
FILL_NULL category WITH "Uncategorized";
// Text cleanup
COLUMN product_name TRIM;
COLUMN category TRIM;
COLUMN category UPPERCASE;
COLUMN description TRIM;
// Price validation
COLUMN price VALIDATE range(0, 100000);
COLUMN stock VALIDATE range(0, 999999);
REMOVE_DUPLICATES;
================================================================================
6. EXECUTION FLOW
================================================================================
Compilation Process:
--------------------
1. LEXICAL ANALYSIS (Tokenization)
- Source code → Tokens
- Identifies keywords, operators, literals
- Removes whitespace and comments
2. SYNTAX ANALYSIS (Parsing)
- Tokens → Abstract Syntax Tree (AST)
- Validates grammar rules
- Checks statement structure
3. CODE GENERATION
- AST → Pandas Python code
- Converts DSL to executable operations
4. EXECUTION
- Runs Pandas operations on CSV data
- Applies transformations in order
- Returns cleaned CSV
Execution Order:
----------------
Statements are executed in the order written:
1. CLEAN statement (initialization)
2. FILL_NULL statements (fill missing data)
3. REMOVE_DUPLICATES (deduplicate)
4. COLUMN transforms (TRIM, UPPERCASE, LOWERCASE)
5. VALIDATE statements (remove invalid rows)
IMPORTANT: Order matters!
- Fill nulls BEFORE validation
- TRIM BEFORE case conversion
- Remove duplicates AFTER filling nulls
================================================================================
7. ERROR HANDLING
================================================================================
7.1 LEXICAL ERRORS
------------------
- Invalid characters
- Unterminated strings
- Unknown tokens
Example:
FILL_NULL name WITH 'invalid'; // Error: Use double quotes
COLUMN age @TRIM; // Error: Invalid character @
7.2 SYNTAX ERRORS
-----------------
- Missing semicolons
- Wrong keyword order
- Invalid statement structure
Examples:
FILL_NULL age WITH 0 // Error: Missing semicolon
WITH 0 FILL_NULL age; // Error: Wrong order
COLUMN TRIM name; // Error: Column name before operation
7.3 RUNTIME ERRORS
------------------
- Column not found
- Invalid data type
- Validation failures
Examples:
FILL_NULL nonexistent WITH 0; // Error: Column doesn't exist
COLUMN age VALIDATE range(0); // Error: Range needs 2 parameters
7.4 ERROR MESSAGES
------------------
Format: Line X:Y - Error message
Example:
Line 5:10 - Column 'agee' not found. Available columns: [age, name, email]
Line 8:15 - Expected SEMICOLON, got IDENTIFIER
================================================================================
8. BEST PRACTICES
================================================================================
8.1 ORDERING
------------
✓ DO: Start with CLEAN statement
✓ DO: Fill nulls before validation
✓ DO: TRIM before case conversion
✓ DO: Remove duplicates after filling nulls
✗ DON'T: Validate before filling nulls
✗ DON'T: Skip the CLEAN statement
8.2 NAMING
----------
✓ DO: Use descriptive column names
✓ DO: Match exact column names from CSV
✗ DON'T: Use spaces in column names
✗ DON'T: Use special characters
8.3 VALUES
----------
✓ DO: Use appropriate data types
✓ DO: Use quotes for strings
✓ DO: Use meaningful defaults
✗ DON'T: Fill numbers with strings
✗ DON'T: Use empty strings as defaults
8.4 VALIDATION
--------------
✓ DO: Set realistic ranges
✓ DO: Validate after filling nulls
✓ DO: Document why ranges are chosen
✗ DON'T: Use overly restrictive ranges
✗ DON'T: Forget to handle edge cases
8.5 COMMENTS
------------
✓ DO: Comment complex logic
✓ DO: Explain business rules
✓ DO: Document data assumptions
Example:
// Age range based on company policy (18-65)
COLUMN age VALIDATE range(18, 65);
================================================================================
9. GRAMMAR REFERENCE (BNF)
================================================================================
program ::= statement+
statement ::= clean_stmt
| fill_null_stmt
| remove_dup_stmt
| column_stmt
| validate_stmt
clean_stmt ::= "CLEAN" IDENTIFIER ";"
fill_null_stmt ::= "FILL_NULL" IDENTIFIER "WITH" value ";"
remove_dup_stmt ::= "REMOVE_DUPLICATES" ";"
column_stmt ::= "COLUMN" IDENTIFIER operation ";"
operation ::= "TRIM" | "UPPERCASE" | "LOWERCASE"
validate_stmt ::= "COLUMN" IDENTIFIER "VALIDATE"
IDENTIFIER "(" parameter_list ")" ";"
parameter_list ::= value ( "," value )*
value ::= STRING | NUMBER
IDENTIFIER ::= [a-zA-Z_][a-zA-Z0-9_]*
STRING ::= '"' [^"]* '"'
NUMBER ::= [0-9]+ ( "." [0-9]+ )?
================================================================================
10. QUICK REFERENCE CARD
================================================================================
CLEAN dataset;
FILL_NULL column WITH value;
REMOVE_DUPLICATES;
COLUMN column TRIM;
COLUMN column UPPERCASE;
COLUMN column LOWERCASE;
COLUMN column VALIDATE range(min, max);
// Comment
"string value"
123
45.67
; - Required at end of statement
, - Separates parameters
================================================================================
END OF GUIDE
================================================================================
For more information, examples, or support:
- Check the project README.md
- Review example scripts in the project
- Open the web interface at http://localhost:5173
Version: 1.0.0
Last Updated: October 2025
================================================================================