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

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