Skip to main content

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 text. The action here is to print the matched text using `printf`.


4. `.`: This is a catch-all pattern that matches any character except a newline.


5. `;`: This semicolon terminates the action for a pattern.


6. `%%`: This double percentage sign separates the rules section from the C code section.


7. `int main() {`: This is the start of the `main` function where the Lex-generated code begins.


8. `yylex();`: This function call initiates the lexical analysis process.


9. `return 0;`: This returns a success status code from the program.


Now, when you run the program and input text that contains "abc", it will print out those parts of the text. For example, if you input "xyzabc123", the program will output "xyzabc". Any other characters that are not part of "abc" are ignored.


Keep in mind that this program doesn't directly replace "abc" with "ABC". Instead, it matches and prints out the portions of the input string that contain "abc". If you want to perform actual replacement, you would need to modify the action to replace "abc" with "ABC" and print the modified result.


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