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

6. Generate a YACC specification to recognize a valid arithmetic expression that uses operators +, – , *,/ and parenthesis.

Below is a YACC specification for recognizing valid arithmetic expressions that involve operators (+, -, *, /) and parentheses. I'll explain each line of the code afterward: ```yacc %{ #include <stdio.h> %} %token NUMBER %left '+' '-' %left '*' '/' %% expression: expression '+' expression            | expression '-' expression            | expression '*' expression            | expression '/' expression            | '(' expression ')'            | NUMBER            ; %% int yylex() {     // Code for tokenizing input and returning tokens } void yyerror(const char *s) {     fprintf(stderr, "Error: %s\n", s); } int main() {     yyparse();     return 0; } ``` Now, let's break down the YACC specification line by line: 1. `%{` ... `%}`: This is the C code section where y...

1, Design 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

#include <stdio.h> #include <ctype.h> int main() {     char c;     while ((c = getchar()) != EOF) {         if (isspace(c)) {             // Ignore whitespace, tabs, and newlines             continue;         }         if (c == '/') {             // Check for comments             char nextChar = getchar();             if (nextChar == '/') {                 while ((nextChar = getchar()) != '\n');         ...

17. Write a program to perform constant propagation.

Sure, I can provide you with a basic example of a constant propagation program along with explanations for each line. Please note that this example is simplified and might not cover all possible cases of constant propagation. ```c #include <stdio.h> #include <stdbool.h> // Structure to represent an assignment statement struct Assignment {     char variable;     int value;     bool isConstant; }; int main() {     // Example assignments (variable, value, isConstant)     struct Assignment assignments[] = {         {'a', 10, true},         {'b', 20, true},         {'c', 0, true},         {'d', 30, false},         {'e', 0, false}     };     int numAssignments = sizeof(assignments) / sizeof(assignments[0]);     // Perform constant propagation     for (int i = 0; i < numAssignments; i++) { ...