-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArithmetic_Counter.l
More file actions
74 lines (65 loc) · 2.43 KB
/
Arithmetic_Counter.l
File metadata and controls
74 lines (65 loc) · 2.43 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
%{
/*
* =========================================================================================
* Experiment 10.2: Arithmetic Counter using LEX
* Course: System Programming and Compiler Construction (SPCC)
*
* Author: Amey Thakur
* GitHub: https://github.com/Amey-Thakur
* Repository: https://github.com/Amey-Thakur/SYSTEM-PROGRAMMING-AND-COMPILER-CONSTRUCTION-AND-SYSTEM-SOFTWARE-LAB
* =========================================================================================
*
* TECHNICAL LOGIC EXPLANATION:
* This program implements a state-ful pattern analyzer that maintains a real-time
* count of specific lexical tokens (integers) within the input stream.
*
* Operational Logic:
* 1. Accumulation: Whenever the numeric pattern [0-9]+ is matched, an internal
* counter is incremented.
* 2. Visual Feedback: Every match triggers a status update showing both the
* detected value and the updated running total.
* 3. Reporting Trigger: The program monitors for lowercase alphabetic characters
* ([a-z]). Encountering such a character acts as a "Request Report" command,
* prompting the system to display the final cumulative count.
*/
#include <stdio.h>
int integer_count = 0; // Global accumulator for numeric tokens
%}
/*
* RULE DEFINITIONS
*/
%%
[0-9]+ {
/* Action: Update state and provide immediate detection feedback */
integer_count++;
printf(" [Status] Numeric Token Found: %s | Current Total: %d\n", yytext, integer_count);
}
[a-z] {
/* Action: Trigger result summary and report generation */
printf("\n--- CUMULATIVE RESULT SUMMARY ---\n");
printf("Final Integer Count: %d\n", integer_count);
printf("Reporting Triggered By: '%s'\n", yytext);
printf("----------------------------------\n");
}
.|\n {
/* Action: Ignore non-triggering characters */
}
%%
/*
* USER INTERFACE LOGIC
*/
/* Required function to handle single-input source completion */
int yywrap() {
return 1;
}
int main(void) {
printf("==============================================\n");
printf(" ARITHMETIC COUNTER (LEX) \n");
printf("==============================================\n");
printf("Function: Continuous counting of numericlexemes.\n");
printf("Control: Use lowercase letters [a-z] to fetch the current total.\n");
printf("----------------------------------------------\n");
/* Activating the LEX analysis engine */
yylex();
return 0;
}