You are on page 1of 10

COMPILER DESIGN ASSIGNMENT - 1

COMPILER DESIGN LAB-1


NAME: M. VARUN REDDY

REGN. NO.: 21BRS1507

AIM: To demonstrate the Phases of a Compiler (with suitable expression).

ALGORITHM:
SOURCE CODE:

Expression: x = y + 10z

Phase 1: Lexical Analysis:


The primary functions of this phase are:

 Identify the lexical units in a source code.


 Classify lexical units into classes like constants, reserved words, and
enter them in different tables. It will Ignore comments in the source
program.
 Identify token which is not a part of the language.

PHASE1: OUTPUT:

TOKEN TYPE
X IDENTIFIER
= ASSIGNMENT OPERATOR
Y IDENTIFIER
+ ARITHMETIC OPERATOR
10 CONSTANT
Z IDENTIFIER

Phase 2: Syntax Analysis:


Here, is a list of tasks performed in this phase:

 Obtain tokens from the lexical analyser.


 Checks if the expression is syntactically correct or not.
 Report all syntax errors.
 Construct a hierarchical structure which is known as a parse tree.

Phase 2: OUTPUT:
Phase 3: Semantic Analysis:
Functions of Semantic analyses phase are:

 Helps you to store type information gathered and save it in symbol table
or syntax tree.
 Allows you to perform type checking.
 In the case of type mismatch, where there are no exact type of
correction rules which satisfy the desired operation a semantic error is
shown
 Collects type information and checks for type compatibility.
 Checks if the source language permits the operands or not.

Phase 3: OUTPUT:
Ex-

Float y = 30.3

Float z = 30 * y

Here the semantic analyzer will typecast constant 30 into float 30.0 before
multiplication.

Float x = “stardom”
Here the semantic analyzer will throw an error since we are trying to store a
string in a float variable ‘x’.

Phase 4: Intermediate Code Generation:


Functions on Intermediate Code generation:

 It should be generated from the semantic representation of the source


program
 Holds the values computed during the process of translation
 Helps you to translate the intermediate code into target language
 Allows you to maintain precedence ordering of the source language
 It holds the correct number of operands of the instruction

Phase 4: OUTPUT:

Expression: x = y + 10z
t1 := int_to_float(10)

t2 := t1 * z

t3 := y + t2

x := t3

Phase 5: Code Optimization:


The primary functions of this phase are:

 It helps you to establish a trade-off between execution and compilation


speed
 Improves the running time of the target program
 Generates streamlined code still in intermediate representation
 Removing unreachable code and getting rid of unused variables
 Removing statements which are not altered from the loop

Phase 5: OUTPUT:
t3 := y + 10z

x := t3

Phase 6: Target Code Generator:


It is the last phase and in this, you will see how you can convert the final
expression into assembly code. so, that it will be easy to understand for the
processor.

Phase 6 OUTPUT:
// Assuming registers R0, R1, R2 are used for x, y, and z respectively

LOAD R1, y

LOAD R2, 10

MUL R2, z

ADD R1, R2

STORE R1, x
COMPILER DESIGN LAB-2
NAME: M. VARUN REDDY

REGN. NO.: 21BRS1507

AIM: Implementation of Deterministic Finite Automata (DFA) from regular


grammar using C language.

ALGORITHM:
L(M) = {a^m * (cd)^n * e ^ k / m,n,k >= 1}

1. START
2. Initialize character array of length 100 to store the string
3. Take input from user and store in the array
4. Initialize a char variable ‘state’ with the initial state ‘A’
5. Traverse the input string from idx = 0 to n-1
6. If any character accepted by the current state is str[i] then
update state variable to the next state according to the DFA
logic and continue the next iteration
7. Continue till the end of string is reached or a dead state is
reached
8. If end of the string is reached check if the string is in Final State,
if its in Final state then the string is accepted
9. If any Dead State is reached then terminate the loop and exit
program.
10. END

CODE:
#include <stdio.h>

int main()
{
printf("Hello World\n");
char str[100];
printf("Enter your input string (< 100 char length: ");
scanf("%s", str); // taking input for the string to be checked
// c program to accept strings for regular grammar L(M) = {a^m * (cd)^n * e ^
k / m,n,k >= 1}
char state = 'A';
for (int i=0; str[i] != '\0'; i++) {
if (state == 'X') {
printf("\nDead State reached, can't proceed further");
break;
}
if (state == 'A') {
if (str[i] == 'a') {
printf("[A,%c]--->", str[i]);
continue;
}
if (str[i] == 'c') {
printf("[A,%c]--->", str[i]);
state = 'B';
continue;
}
else {
printf("[A,%c]--->Dead State", str[i]);
state = 'X';
}
continue;
}
if (state == 'B') {
if (str[i] == 'd') {
printf("[B,%c]--->", str[i]);
state = 'C';
continue;
}
else {
printf("[B,%c]--->Dead State", str[i]);
state = 'X';
}
continue;
}
if (state == 'C') {
if (str[i] == 'e') {
printf("[C,%c]--->", str[i]);
state = 'D';
continue;
}
if (str[i] == 'c') {
printf("[C,%c]--->", str[i]);
state = 'B';
continue;
}
else {
printf("[C,%c]--->Dead State", str[i]);
state = 'X';
}
continue;
}
if (state == 'D') {
if (str[i] == 'e') {
printf("[D,%c]--->", str[i]);
continue;
}
else {
printf("[D,%c]--->Dead State", str[i]);
state = 'X';
continue;
}
}

}
if (state == 'D') {
printf("[D -- Final State]\nstring is accepted");
}
else {
printf("\nstring is not accepted");
}

return 0;
}

OUTPUT:
1. String: acde (accepted)

2. String: abcde (not accepted)

3. String: aaacdcdcdeee (accepted)

4. String: aacdcdcee (not accepted)

5. String: eeee (not accepted)

You might also like