Professional Documents
Culture Documents
ALGORITHM:
SOURCE CODE:
Expression: x = y + 10z
PHASE1: OUTPUT:
TOKEN TYPE
X IDENTIFIER
= ASSIGNMENT OPERATOR
Y IDENTIFIER
+ ARITHMETIC OPERATOR
10 CONSTANT
Z IDENTIFIER
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: OUTPUT:
Expression: x = y + 10z
t1 := int_to_float(10)
t2 := t1 * z
t3 := y + t2
x := t3
Phase 5: OUTPUT:
t3 := y + 10z
x := t3
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
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)