Skip to main content

18. Implement Intermediate code generation for simple expressions.


Creating a complete implementation of intermediate code generation for simple expressions would be quite extensive, but I can provide you with a simple example along with explanations for each line of code. In this example, we'll generate three-address code for arithmetic expressions involving addition and subtraction.


```c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


int tempCount = 0;


// Function to generate a new temporary variable

char* newTemp() {

    char* temp = (char*)malloc(4);

    sprintf(temp, "t%d", tempCount);

    tempCount++;

    return temp;

}


int main() {

    char expr[50];

    printf("Enter an arithmetic expression: ");

    scanf("%[^\n]", expr);


    char* result = newTemp();

    printf("%s = %s\n", result, expr);


    return 0;

}

```


Explanation of the code:


1. `#include <stdio.h>`: Include the standard I/O library for input/output functions.

2. `#include <stdlib.h>`: Include the standard library for dynamic memory allocation (malloc function).

3. `#include <string.h>`: Include the string library for string manipulation functions.

4. `int tempCount = 0;`: Initialize a counter for temporary variable names.

5. `char* newTemp()`: Function to generate a new temporary variable name.

   - `char* temp`: Declare a character pointer named "temp".

   - `sprintf(temp, "t%d", tempCount);`: Use sprintf to format the temporary variable name using the current tempCount value.

   - `tempCount++;`: Increment tempCount for the next temporary variable.

   - `return temp;`: Return the generated temporary variable name.

6. `int main()`: The main function where the program starts execution.

7. `char expr[50];`: Declare a character array to store the input arithmetic expression.

8. `printf("Enter an arithmetic expression: ");`: Prompt the user to enter an arithmetic expression.

9. `scanf("%[^\n]", expr);`: Read the input expression until a newline character is encountered.

10. `char* result = newTemp();`: Generate a new temporary variable name and assign it to the "result" variable.

11. `printf("%s = %s\n", result, expr);`: Print the generated three-address code, assigning the input expression to the result variable.

12. `return 0;`: End the main function and the program.


In this simplified example, we are generating three-address code by assigning the input expression to a generated temporary variable. The code showcases the basic concept of generating intermediate code for expressions. In a real compiler, you would need to handle more complex expressions and operators, generate appropriate code for each operator, and manage the temporary variables and their lifetimes.


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