-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
60 lines (47 loc) · 2.47 KB
/
README
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
maayanshani,rotem.garti
319111886,207869520
## Regular Expressions Used in the Code
---
### 1. VARIABLE_NAME_REGEX
Regular Expression:
(?!true$)(?!false$)([a-zA-Z]|_[a-zA-Z0-9])[a-zA-Z0-9_]*
Explanation:
This regex defines the structure of a valid variable name.
- (?!true$)(?!false$): Negative lookahead assertions ensure the variable name is not "true" or "false".
This prevents the use of boolean literals as variable names.
- ([a-zA-Z]|_[a-zA-Z0-9]): Matches a valid starting character for a variable name:
- [a-zA-Z]: Allows variable names to start with a letter.
- |: OR operator.
- _[a-zA-Z0-9]: Allows variable names to start with an underscore followed by an alphanumeric character.
- [a-zA-Z0-9_]*: Matches the rest of the variable name, allowing any combination of alphanumeric characters
and underscores.
Purpose:
This ensures variable names:
- Follow valid naming conventions.
- Avoid conflicts with reserved boolean literals (true, false).
- Allow underscores and alphanumeric characters.
---
### 2. singleDeclaration
Regular Expression:
(" + VARIABLE_NAME_REGEX + ")\\s*(=\\s*(" + typeValueRegex + "|" + VARIABLE_NAME_REGEX + "))?
Explanation:
This regex defines the structure of a valid single variable declaration:
- (" + VARIABLE_NAME_REGEX + "): Matches the variable name using the rules defined in VARIABLE_NAME_REGEX.
- Group 1 captures the variable name.
- \\s*: Matches optional whitespace after the variable name.
- (=\\s*(" + typeValueRegex + "|" + VARIABLE_NAME_REGEX + "))?: Matches an optional assignment:
- =: The assignment operator.
- \\s*: Matches optional whitespace around the assignment operator.
- (" + typeValueRegex + "|" + VARIABLE_NAME_REGEX + "): Matches the assigned value:
- typeValueRegex: Valid literal values for the type (int, double, Sring, char, boolean).
- VARIABLE_NAME_REGEX: Another valid variable name being assigned.
- Group 3 captures the value being assigned.
- ?: Makes the assignment optional, allowing for declarations without initialization (e.g., int x;).
Purpose:
This regex validates and parses single variable declarations, ensuring:
- Variable names are valid.
- Assignments, if present, use either a valid literal value or a valid variable name.
- Formatting is flexible with optional whitespace.
---
These regular expressions form the backbone of variable validation and declaration parsing in the program,
ensuring adherence to strict syntax rules while maintaining flexibility in formatting.