0% found this document useful (0 votes)
9 views

Compiler Design Programs All

The document contains various programs related to compiler design, including a lexical analyzer for identifiers and constants, a symbol table with basic operations, and parsers using LEX and YACC for arithmetic expressions and variable recognition. It also covers three-address code generation, type checking, code optimization, machine code generation, and pattern recognition. Each section includes example code demonstrating the functionality of the respective compiler design concepts.

Uploaded by

thakkuduwhatsapp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Compiler Design Programs All

The document contains various programs related to compiler design, including a lexical analyzer for identifiers and constants, a symbol table with basic operations, and parsers using LEX and YACC for arithmetic expressions and variable recognition. It also covers three-address code generation, type checking, code optimization, machine code generation, and pattern recognition. Each section includes example code demonstrating the functionality of the respective compiler design concepts.

Uploaded by

thakkuduwhatsapp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Compiler Design Programs

1. Lexical Analyzer for Identifiers and Constants

#include <stdio.h>

#include <ctype.h>

#include <string.h>

void lexicalAnalyzer(char str[]) {

int i = 0;

printf("Tokens:\n");

while (str[i] != '\0') {

if (isalpha(str[i])) {

printf("Identifier: ");

while (isalnum(str[i])) {

printf("%c", str[i++]);

printf("\n");

} else if (isdigit(str[i])) {

printf("Constant: ");

while (isdigit(str[i])) {

printf("%c", str[i++]);

printf("\n");

} else {

i++; // Skip other characters

}
}

int main() {

char input[] = "x1 = 100 + y2;";

lexicalAnalyzer(input);

return 0;

2. Symbol Table with Basic Operations

#include <stdio.h>

#include <string.h>

#define MAX 100

typedef struct {

char name[50];

int value;

} Symbol;

Symbol table[MAX];

int count = 0;

void insert(char name[], int value) {

strcpy(table[count].name, name);

table[count].value = value;
count++;

printf("Inserted: %s = %d\n", name, value);

void delete (char name[]) {

for (int i = 0; i < count; i++) {

if (strcmp(table[i].name, name) == 0) {

for (int j = i; j < count - 1; j++) {

table[j] = table[j + 1];

count--;

printf("Deleted: %s\n", name);

return;

printf("Not Found: %s\n", name);

void search(char name[]) {

for (int i = 0; i < count; i++) {

if (strcmp(table[i].name, name) == 0) {

printf("Found: %s = %d\n", name, table[i].value);

return;

printf("Not Found: %s\n", name);

}
void modify(char name[], int value) {

for (int i = 0; i < count; i++) {

if (strcmp(table[i].name, name) == 0) {

table[i].value = value;

printf("Modified: %s = %d\n", name, value);

return;

printf("Not Found: %s\n", name);

int main() {

insert("x", 10);

search("x");

modify("x", 20);

delete("x");

search("x");

return 0;

3. Lexical Analyzer Using LEX Tool

LEX File (lexer.l):

%{
#include <stdio.h>

%}

%%

[a-zA-Z_][a-zA-Z0-9_]* { printf("Identifier: %s\n", yytext); }

[0-9]+ { printf("Constant: %s\n", yytext); }

. ;

%%

int main() {

yylex();

return 0;

4. Arithmetic Expression Parsing Using YACC

YACC File (parser.y):

%{

#include <stdio.h>

%}

%token NUMBER

%left '+' '-'

%left '*' '/'


%%

expr: expr '+' expr { printf("Add\n"); }

| expr '-' expr { printf("Subtract\n"); }

| expr '*' expr { printf("Multiply\n"); }

| expr '/' expr { printf("Divide\n"); }

| NUMBER { printf("Number: %d\n", $1); }

%%

int main() {

yyparse();

return 0;

5. Valid Variable Recognition Using YACC

YACC File (var_parser.y):

%{

#include <stdio.h>

%}

%token VARIABLE

%%

stmt: VARIABLE { printf("Valid Variable: %s\n", yytext); }


| error { printf("Invalid Variable!\n"); }

%%

int main() {

yyparse();

return 0;

int yyerror(char *s) {

fprintf(stderr, "%s\n", s);

return 0;

6. Native Calculator Using LEX and YACC

LEX File (calc_lexer.l):

%{

#include "y.tab.h"

%}

%%

[0-9]+ { yylval = atoi(yytext); return NUMBER; }

[+\-*/] { return yytext[0]; }

\n { return 0; }
. ;

%%

YACC File (calc_parser.y):

%{

#include <stdio.h>

#include <stdlib.h>

%}

%token NUMBER

%left '+' '-'

%left '*' '/'

%%

stmt: expr { printf("Result = %d\n", $1); }

expr: expr '+' expr { $$ = $1 + $3; }

| expr '-' expr { $$ = $1 - $3; }

| expr '*' expr { $$ = $1 * $3; }

| expr '/' expr { $$ = $1 / $3; }

| NUMBER { $$ = $1; }

%%

int main() {
yyparse();

return 0;

int yyerror(char *s) {

fprintf(stderr, "%s\n", s);

return 0;

7. Three-Address Code Generation

#include <stdio.h>

#include <string.h>

void generateTAC(char *expr) {

char op;

char t[10], temp[10];

int i = 0, k = 1;

while (expr[i]) {

if (expr[i] == '+' || expr[i] == '-' || expr[i] == '*' || expr[i] == '/') {

op = expr[i];

sprintf(t, "t%d", k++);

sprintf(temp, "%c = %c %c %c", t[0], expr[i - 1], op, expr[i + 1]);

printf("%s\n", temp);

expr[i - 1] = t[0];
memmove(expr + i, expr + i + 2, strlen(expr) - i - 1);

i -= 2;

i++;

int main() {

char expr[100] = "a+b*c";

printf("Three-Address Code:\n");

generateTAC(expr);

return 0;

8. Simple Type Checker

#include <stdio.h>

#include <string.h>

void checkTypes(char *stmt) {

char var[10], type[10], value[10];

sscanf(stmt, "%s %s = %s;", type, var, value);

if (strcmp(type, "int") == 0 && !isdigit(value[0])) {

printf("Error: Expected integer for variable '%s'\n", var);

} else if (strcmp(type, "float") == 0 && !strchr(value, '.')) {


printf("Error: Expected float for variable '%s'\n", var);

} else {

printf("Type-check passed for '%s'\n", var);

int main() {

checkTypes("int x = 42;");

checkTypes("float y = 3.14;");

checkTypes("int z = 'a';");

return 0;

9. Code Optimization Example

#include <stdio.h>

#include <string.h>

void optimizeCode(char *code) {

if (strstr(code, "x = x + 0;")) {

printf("Optimization: Remove '%s'\n", code);

return;

printf("No optimization needed.\n");

}
int main() {

optimizeCode("x = x + 0;");

return 0;

10. Machine Code Generation

#include <stdio.h>

void generateMachineCode(char *expr) {

printf("Input Expression: %s\n", expr);

printf("Machine Code:\n");

printf("LOAD %c\n", expr[0]);

printf("ADD %c\n", expr[2]);

printf("STORE %c\n", expr[4]);

int main() {

generateMachineCode("a+b=c");

return 0;

11. Pattern Recognition with YACC

YACC File (pattern_parser.y):


%{

#include <stdio.h>

%}

%token PATTERN

%%

stmt: PATTERN { printf("Valid Statement: %s\n", yytext); }

| error { printf("Invalid Pattern\n"); }

%%

int main() {

yyparse();

return 0;

LEX File (pattern_lexer.l):

%{

#include "y.tab.h"

%}

%%

[A-Z][a-zA-Z0-9]{5} { return PATTERN; }

. { return yytext[0]; }

%%

You might also like