Pcc
Pcc
CBCPC14
PRACTICAL FILE
2024-25
Branch: CSDA
Section: 1
int main() {
// Predefined assembly instructions
const char *instructions[] = {
"LOAD A",
"STORE B",
"ADD C",
"SUB D",
"MULT E",
"DIV F",
"ADD", // Example of missing operand
"LOAD A B", // Example of incorrect format (too many operands)
"UNKNOWN X" // Example of an unrecognized command
};
Output:
Practical-2
Aim: Design a small high-level language.
Code:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum {
TOKEN_EOF,
TOKEN_VAR,
TOKEN_ID,
TOKEN_EQUALS,
TOKEN_NUMBER,
TOKEN_PLUS,
TOKEN_SEMICOLON
} TokenType;
// Define a token.
typedef struct {
TokenType type;
const char *start;
int length;
} Token;
int main() {
char input[1024];
printf("Enter code: ");
// Use fgets to read a full line with spaces.
if (!fgets(input, sizeof(input), stdin)) {
fprintf(stderr, "Error reading input.\n");
return 1;
}
Output:
Practical-3
Aim: Develop a lexical analyzer and a syntax analyzer for a simple high-level language using the
LEX and YACC tools. Also implement the bookkeeper module.
Code:
1) Create and Compile Lex file (lex simple.l)
%{
#include <stdio.h>
#include <stdlib.h>
#include "y.tab.h" // This header file is generated by YACC
%}
%%
%%
int yywrap(void) {
return 1;
}
%union {
int num;
char* str;
}
%%
program:
program statement
| /* empty */
;
statement:
IDENTIFIER ASSIGN expression {
add_variable($1, $3); // Add the variable to bookkeeper
printf("Assigned %d to %s\n", $3, $1);
}
| expression {
printf("Result: %d\n", $1);
}
;
expression:
term {
$$ = $1;
}
| expression ADD term {
$$ = $1 + $3;
}
| expression SUB term {
$$ = $1 - $3;
}
;
term:
factor {
$$ = $1;
}
| term MUL factor {
$$ = $1 * $3;
}
| term DIV factor {
$$ = $1 / $3;
}
;
factor:
NUMBER {
$$ = $1;
}
| IDENTIFIER {
$$ = get_variable($1); // Get the value from bookkeeper
}
| LPAREN expression RPAREN {
$$ = $2;
}
;
%%
int main(void) {
yyparse();
return 0;
}
typedef struct {
char* name;
int value;
} Variable;
Variable variables[MAX_VARS];
int var_count = 0;
#endif
Input:
Output:
Practical-4
Aim: Design a small high-level language and implement a compiler for the same. If the target
machine of the compiler is a hypothetical machine, then implement a simulator for it.
Code:
%{
#include "MiniLang.tab.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
%}
%%
let { return LET; }
if { return IF; }
then { return THEN; }
else { return ELSE; }
print { return PRINT; }
%%
int yywrap() {
return 1;
}
Symbol symbolTable[100];
int symbolCount = 0;
%}
%%
// Define the grammar rules
program:
statements
;
statements:
statement SEMICOLON statements
| statement SEMICOLON
;
statement:
LET IDENTIFIER ASSIGN expression { setSymbolValue($2, $4);
printf("Assign %s = %d\n", $2, $4); }
| IF condition THEN statement ELSE statement { printf("Conditional
statement\n"); }
| PRINT expression { printf("Print %d\n",
$2); }
| PRINT STRING { printf("Print %s\n",
$2); }
;
condition:
expression GT expression { $$ = ($1 > $3); }
;
expression:
expression PLUS term { $$ = $1 + $3; }
| term { $$ = $1; }
;
term:
term MULT factor { $$ = $1 * $3; }
| factor { $$ = $1; }
;
factor:
NUMBER { $$ = $1; }
| IDENTIFIER { $$ =
getSymbolValue($1); }
;
%%
Input:
Output:
Practical-5
Aim: Develop a simple calculator using LEX and YACC tools.
Code:
1) Create and Compile Lex file (lex calc.l)
%{
#include<stdlib.h>
#include "y.tab.h"
extern int yylval;
%}
%%
. return yytext[0];
%%
%{
#include<stdio.h>
%}
int yyerror(){}
int yywrap(){
return 1;
}
Output:
Practical-6
Aim: Implement a program for symbol table using hashing.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
SymbolTable symbolTable;
initSymbolTable(&symbolTable);
int choice, value;
char key[100];
do {
printf("\n------ Menu ------\n");
printf("1. Insert Symbol\n");
printf("2. Search Symbol\n");
printf("3. Delete Symbol\n");
printf("4. Display Symbol Table\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter symbol name: ");
scanf("%s", key);
printf("Enter symbol value: ");
scanf("%d", &value);
insert(&symbolTable, key, value);
printf("Symbol inserted.\n");
break;
case 2:
printf("Enter symbol name to search: ");
scanf("%s", key);
value = search(&symbolTable, key);
if (value == -1)
printf("Symbol not found.\n");
else
printf("Symbol '%s' found with value: %d\n", key,
value);
break;
case 3:
printf("Enter symbol name to delete: ");
scanf("%s", key);
delete(&symbolTable, key);
printf("Symbol deleted (if it existed).\n");
break;
case 4:
display(&symbolTable);
break;
case 5:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while(choice != 5);
return 0;
}
Output:
Practical-7
Aim: Implement a two-pass assembler.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char label[MAX_LINE_LENGTH];
int address;
} Symbol;
Symbol symbolTable[MAX_LABELS];
int symbolCount = 0;
int currentAddress = 0;
char code[MAX_CODE_LINES][MAX_LINE_LENGTH];
int codeLineCount = 0;
strcpy(code[codeLineCount++], line);
printf("Symbol Table:\n");
for (int i = 0; i < symbolCount; i++) {
printf("Label: %s, Address: %d\n", symbolTable[i].label,
symbolTable[i].address);
}
}
void secondPass() {
for (int i = 0; i < codeLineCount; i++) {
char label[MAX_LINE_LENGTH], instruction[MAX_LINE_LENGTH],
operand[MAX_LINE_LENGTH];
int main() {
FILE *input = fopen("assembly.txt", "r");
if (!input) {
perror("File opening failed");
return EXIT_FAILURE;
}
firstPass(input);
rewind(input);
secondPass();
fclose(input);
return 0;
}
Input:
Output:
Practical-8
Aim: Implement a bottom-up parser using YACC tool.
Code:
1) Create and Compile Lex file (lex lexer.l)
%{
#include <stdio.h>
#include "y.tab.h" // Include the header generated by YACC for token
definitions
%}
%%
[0-9]+ { yylval.dval = atof(yytext); return DIGIT; } // Recognize numbers
and convert to double
\n|. { return yytext[0]; } // Pass newline or any other character
%%
%{
#include <stdio.h>
%}
%union {
double dval; // Use a double to store numeric values
}
%%
line: expr '\n' { printf("%g\n", $1); } // Print result of expression on
a new line
;
Output:
Practical-9
Aim: Represent ‘C’ language using Context Free Grammar.
Representation:
Practical-10
Aim: Write a code to generate the three address code for the given expression.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct three {
char data[10], temp[7];
} s[30];
void main() {
char d1[7], d2[7] = "t";
int i = 0, j = 1, len = 0;
FILE *f1;
strcpy(d1, "");
strcpy(d2, "t");
j++;
}
Input:
Output: