Skip to main content

3. Write a lex program to display the number of lines, words and characters in an input text.



There's a Lex program that displays the number of lines, words, and characters in an input text, along with explanations for each line of code:


```lex

%{

#include <stdio.h>

int lineCount = 0;

int wordCount = 0;

int charCount = 0;

%}


%%

\n         { lineCount++; }          // Match newline characters

[^\n\t ]+  { wordCount++; }          // Match non-space, non-tab, non-newline characters

[^\n]+     { charCount += yyleng; }  // Match any characters

.         ;                          // Match any other characters

%%


int main() {

    yylex();  // Start Lex analysis

    printf("Lines: %d\nWords: %d\nCharacters: %d\n", lineCount, wordCount, charCount);

    return 0;

}

```


Explanation of each line:


1. `%{ ... %}`: This section allows you to include C code that will be copied directly into the generated Lex code.


2. `#include <stdio.h>`: This includes the standard I/O library for printing.


3. `int lineCount = 0;`: Declare a variable to count lines and initialize it to 0.


4. `int wordCount = 0;`: Declare a variable to count words and initialize it to 0.


5. `int charCount = 0;`: Declare a variable to count characters and initialize it to 0.


6. `%%`: This is the start of the Lex rules section.


7. `\n         { lineCount++; }`: This rule matches newline characters and increments the line count.


8. `[^\n\t ]+  { wordCount++; }`: This rule matches sequences of characters that are not spaces, tabs, or newlines, and increments the word count.


9. `[^\n]+     { charCount += yyleng; }`: This rule matches any sequence of characters that is not a newline and increments the character count by the length of the matched sequence.


10. `.`: This rule matches any character that didn't match any of the previous rules.


11. `;`: This rule is a no-op, it simply matches a single character and does nothing.


12. `%%`: This marks the end of the Lex rules section.


13. `int main() {`: The main function of the program starts here.


14. `yylex();`: Initiates Lex analysis.


15. `printf("Lines: %d\nWords: %d\nCharacters: %d\n", lineCount, wordCount, charCount);`: Prints the counts of lines, words, and characters.


16. `return 0;`: Exits the program with a success status.


This Lex program matches and counts lines, words, and characters in the input text, and then prints the counts using the `printf` statement in the `main` function.


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...

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...

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');         ...