Title 1: LEX Program to recognize and count vowels and consonants in a given string.
Procedure: [AEIOUaeiou] → matches any single vowel, uppercase or lowercase. vcount++
→ increases vowel count by 1 whenever a vowel is matched. [a-zA-Z] → matches any
alphabetic character (A–Z, a–z). ccount++ → increases consonant count by 1.
Code:
Output:
Title 2: LEX Program to recognize and count uppercase and lowercase letter in a given string.
Procedure: [A-Z] → Matches uppercase letters and increments upper. [a-z] → Matches
lowercase letters and increments lower. “.|\n” → Matches anything else (digits, spaces, symbols,
newline) and ignores them.
Code:
Output:
Title 3: LEX Program to recognize and count token from an input file.
Procedure: Operators → [+\-*/=<>] . Numbers → [0-9]+ . Identifiers → [a-zA-Z_][a-zA-Z0-
9_]* Checks if they are keywords (int, if, etc.), otherwise counts as identifiers. Others →
everything else (symbols, punctuation, spaces, newline).
Code:
Output:
Title 4: LEX Program to recognize string ending with aa.
Procedure: This how “aa” matches the last of a string,
.*aa$ → regex meaning:
o .* → any sequence of characters
o aa → must end with aa
o $ → end of line marker
Code:
Output:
Title 5: LEX Program to recognize string starting and ending with bb.
Procedure: This how “bb” matches the start and end of a string,
^bb → ^ anchors the start of string, so it must begin with bb.
.* → matches any characters in the middle (including none).
bb$ → $ anchors the end of string, so it must end with bb.
First rule → if string matches both start and end → print success.
Second rule → catches all other strings.
Code:
Output:
Title 6: LEX Program to recognize number which has 1 in the 3 rd position and 9 in 7 th
position.
Procedure:
[0-9][0-9]1[0-9][0-9][0-9]9[0-9]* Means
o [0-9][0-9] → first 2 digits (any)
o 1 → 3rd position must be 1
o [0-9][0-9][0-9] → next 3 digits (any)
o 9 → 7th position must be 9
o [0-9]* → remaining digits (if any)
If matched → print “Valid number”.
If it’s just digits but doesn’t match → “Invalid number”.
.|\n → ignore spaces/newlines/others.
Code:
Output:
Title 7: LEX Program to count the number of characters, words, spaces, end of lines in a given
input file.
Procedure:
[ \t] → Matches spaces or tabs → count as spaces and characters.
\n → Matches newline → count as line + char.
[^\n \t]+ → Matches sequences of non-space, non-tab, non-newline → count as words,
and add their length to character count..
. → Matches everything else (punctuation, symbols) → count as character.
Code:
Output:
Title 8: LEX Program to count the no of comment line in a given C program.
Procedure:
"//".* → matches everything after // till the end of line.
"/*"([^*]|\*+[^*/])*\*+"/" → matches /* ... */ including newlines.
Loop through yytext and count how many \n characters → each newline is a comment
line.
.|\n → ignored.
Code:
Output:
Title 9: LEX Program to count the no of comment line in a given C program.
Procedure:
checks for coordinating conjunctions (and, but, or, so, for, nor, yet).
If any is found → set isCompound = 1.
Code:
Output:
Title 10: LEX Program to recognize and count the number of identifiers in a given input file.
Procedure:
matches identifiers (letters or _ to start, letters/digits/_ afterwards).
skips spaces, tabs, and newlines.
ignores other characters (like operators, punctuation).
id_count is incremented for every identifier found.
Code:
Output: