Skip to main content

5. Write a lex program to find out the total number of vowels and consonants from the given input string.





Sure, I can provide you with a Lex program that counts the total number of vowels and consonants in a given input string. I'll also explain each line of code for better understanding.


```lex

%{

#include <stdio.h>

int vowelCount = 0;

int consonantCount = 0;

%}


%%

[aAeEiIoOuU]   { vowelCount++; }

[^aAeEiIoOuU\n]+ { consonantCount++; }

\n             { /* Ignore newline characters */ }

.              { /* Ignore other characters */ }

%%


int main() {

    yylex();

    printf("Total Vowels: %d\nTotal Consonants: %d\n", vowelCount, consonantCount);

    return 0;

}

```


Explanation of each line of code:


1. `%{` and `%}`: These are used to enclose C code that is to be included at the beginning of the generated C code.


2. `#include <stdio.h>`: This includes the standard input/output library for using functions like `printf`.


3. `int vowelCount = 0;` and `int consonantCount = 0;`: These declare integer variables to keep track of the counts of vowels and consonants.


4. `%%`: This separates the Lex specifications from the C code.


5. `[aAeEiIoOuU] { vowelCount++; }`: This rule matches any vowel (both lowercase and uppercase) and increments the `vowelCount`.


6. `[^aAeEiIoOuU\n]+ { consonantCount++; }`: This rule matches any non-vowel character (including consonants and other characters), except newline characters, and increments the `consonantCount`.


7. `\n { /* Ignore newline characters */ }`: This rule ignores newline characters.


8. `.{ /* Ignore other characters */ }`: This rule ignores any other characters that do not match the previous rules.


9. `%%`: This separates the Lex rules from the C code that follows.


10. `int main() { yylex(); ... }`: This is the `main` function that calls `yylex()` to start the Lex scanner. After the scanning is complete, it prints the total number of vowels and consonants.


The Lex program processes the input character by character and matches against the specified rules. If a vowel is found, the `vowelCount` is incremented; if a consonant is found, the `consonantCount` is incremented. Finally, the program prints the counts of vowels and consonants in the input string.


Remember that this is a simple example. It might not handle all edge cases perfectly, and optimizations can be made.


Comments

Popular posts from this blog

Program 6: Finding Prime Numbers in a Range

  ** Program 6: Finding Prime Numbers in a Range ** ```assembly .model small .stack 100h .data     start dw 1     end dw 20 .code     main proc         mov ax, start           ; Load 'start' into AX              find_primes:         inc ax                  ; Move to the next number         cmp ax, end             ; Compare with 'end'         jg end_prog             ; Jump to end if AX > 'end'                  mov cx, ax              ; Copy AX to CX   ...

4. Write a LEX Program to convert the substring abc to ABC from the given input string.

Sure, here's a LEX program that converts the substring "abc" to "ABC" in the given input string: ```lex %{ #include <stdio.h> %} %% .*abc.*    { printf("%s", yytext); }    /* Match and print everything containing 'abc' */ .         ;                            /* Ignore other characters */ %% int main() {     yylex();     return 0; } ``` Now, let's break down the code line by line: 1. `%{` and `%}`: These are used to enclose C code that should be included in the generated C code. In this case, it includes the standard input-output library (`<stdio.h>`). 2. `.*abc.*`: This is a regular expression pattern. It matches any sequence of characters that contains the substring "abc". 3. `{ printf("%s", yytext); }`: This is the action associated with the pattern. When the pattern is matched, the `yytext` variable contains the matched te...

ktu 2019 Cse Complier programing Lab Syllabus

 1Design and implement a lexical analyzer using C language to recognize all valid tokens  in the input program. The lexical analyzer should ignore redundant spaces, tabs and  newlines. It should also ignore comments. 2. Implement a Lexical Analyzer for a given program using Lex Tool. 3. Write a lex program to display the number of lines, words and characters in an input text. 4. Write a LEX Program to convert the substring abc to ABC from the given input string. 5. Write a lex program to find out the total number of vowels and consonants from the given  input string. 6. Generate a YACC specification to recognize a valid arithmetic expression that uses  operators +, – , *,/ and parenthesis. 7. Generate a YACC specification to recognize a valid identifier which starts with a letter  followed by any number of letters or digits.   8. Implementation of Calculator using LEX and YACC  9. Convert the BNF rules into YACC form and write code to generat...