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

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++) { ...