cd lab programs only
cd lab programs only
i++;
ch = exp[i];
if (isdigit(ch))
{
while (isdigit(ch))
{
ptr[n][++k] = ch;
i++;
ch = exp[i];
}
}
i--;
ch = exp[i];
ptr[n][++k] = ' ';
strcat(con, ptr[n]);
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' || ch == '>' || ch == '<' || ch == '=' || ch == '!')
{
opr[++p] = ch;
i++;
ch = exp[i];
if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' || ch == '>' || ch == '<' || ch == '=' || ch == '!')
{
opr[++p] = ch;
}
else
{
i--;
ch = exp[i];
opr[++p] = ' ';
}
}
else
{
sym[++s] = ch;
sym[++s] = ' ';
}
i++;
} while (exp[i] != '\0');
puts("\nKeywords..\n");
puts(kwd);
puts("\nIdentifiers..\n");
puts(id);
puts("\nConstants..\n");
puts(con);
puts("\nOperators..\n");
puts(opr);
puts("\nSymbols..\n");
puts(sym);
//getch();
return 0;
}
3
Output-1:
4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct node {
char symb[40];
char type[10];
char strg[10];
uint addr;
Node *next;
};
void Create() {
Node *Last = NULL;
char ch;
do {
printf("Enter Symbol, type, storage class & address [0 & 65535] ...\n");
p = (Node*) malloc(sizeof(Node));
if (!p) {
printf("Memory allocation failed!\n");
return;
}
scanf("%s %s %s %u", p->symb, p->type, p->strg, &(p->addr));
p->next = NULL;
if (First == NULL)
First = p;
else
Last->next = p;
Last = p;
// FIX: Properly read user input with a space before %c to consume newline
printf("One more Symbol [y/n] : ");
scanf(" %c", &ch); // Ensure the space before %c to remove the newline issue
void Display() {
if (First == NULL)
printf("Symbol Table is Empty ...\n");
else {
printf("Symbols in the Table:\n");
printf("--------------------------------------------------\n");
printf(" Symbol Type StorageClass Address\n");
printf("--------------------------------------------------\n");
for (p = First; p != NULL; p = p->next)
printf("%10s %10s %10s\t%u\n", p->symb, p->type, p->strg, p->addr);
printf("--------------------------------------------------\n");
}
}
5
void Destroy() {
while (First != NULL) {
p = First;
First = First->next;
free(p);
}
}
void Search() {
char sym[10];
printf("Enter Symbol ...\n");
scanf("%s", sym);
if (p == NULL)
printf("Symbol does not exist in the table.\n");
else {
printf(" Symbol = %s\n", p->symb);
printf(" Type = %s\n", p->type);
printf(" Storage Class = %s\n", p->strg);
printf(" Address = %u\n", p->addr);
}
}
void Insert() {
Node *t;
char sym[40];
printf("Enter Symbol ...\n");
scanf("%s", sym);
if (p != NULL)
printf("Multiple Declaration.\n");
else {
p = (Node*) malloc(sizeof(Node));
strcpy(p->symb, sym);
printf("Enter type, storage class & address [0 & 65535] ...\n");
scanf("%s %s %u", p->type, p->strg, &(p->addr));
p->next = NULL;
void Delete() {
Node *t;
char sym[40];
printf("Enter Symbol ...\n");
scanf("%s", sym);
p = NULL;
t = First;
do {
if (strcmp(t->symb, sym) == 0)
6
break;
p = t;
t = t->next;
} while (t != NULL);
if (t == NULL)
printf("Symbol does not exist.\n");
else {
if (t == First)
First = First->next;
else
p->next = t->next;
free(t);
printf("Symbol Deleted Successfully.\n");
}
}
int main() {
int ch;
do {
printf("Symbol Table Operations Menu\n");
printf("-----------------------------\n");
printf("1. Create the Symbol Table\n");
printf("2. Display the Symbol Table\n");
printf("3. Insert a Symbol\n");
printf("4. Delete a Symbol\n");
printf("5. Search for a Symbol\n");
printf("6. Destroy the Table\n");
printf("7. Exit\n\n");
printf("Your Choice [1..7] ? : ");
scanf("%d", &ch);
switch (ch) {
case 1: Create(); break;
case 2: Display(); break;
case 3: Insert(); break;
case 4: Delete(); break;
case 5: Search(); break;
case 6: Destroy(); break;
case 7: break;
default: printf("Invalid Choice ..."); getch();
}
} while (ch != 7);
}
7
Output-2:
8
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
int main()
{
FILE *fi, *fo, *fop, *fk;
int flag = 0, i = 1;
char c, t, a[15], ch[15], file[20];
fi = fopen("input.c", "r");
fo = fopen("inter.c", "w");
fop = fopen("oper.c", "r");
fk = fopen("key.c", "r");
c = getc(fi);
while (!feof(fi))
{
if (isalpha(c) || isdigit(c) || (c == '[' || c == ']' || c == '.' == 1))
fputc(c, fo);
else
{
if (c == '\n')
fprintf(fo, "\t$\t");
else
fprintf(fo, "\t%c\t", c);
}
c = getc(fi);
}
fclose(fi);
fclose(fo);
fi = fopen("inter.c", "r");
printf("\n Lexical Analysis");
fscanf(fi, "%s", a);
printf("\n Line: %d\n", i++);
while (!feof(fi))
{
if (strcmp(a, "$") == 0)
{
printf("\n Line: %d \n", i++);
fscanf(fi, "%s", a);
}
}
fscanf(fop, "%s", ch);
}
rewind(fop);
if (flag == 0)
{
if (isdigit(a[0]))
printf("\t\t%s\t:\tConstant\n", a);
else
printf("\t\t%s\t:\tIdentifier\n", a);
}
flag = 0;
fscanf(fi, "%s", a);
}
getch();
}
Input.c:
#include <stdio.h>
int main()
{
int a=10, b=20, c;
c = a + b;
printf ("Sum is %d", c);
return 0;
}
Inter.c:
#include <stdio.h>
int main()
{
int a = 10,
b = 20,
c;
c = a + b;
printf("Sum is %d", c);
return 0;
}
10
Oper.c: Key.c:
+
Addition
- auto
Subtraction break
* case
Multiplication char
/
const
Division
= continue
Assignment default
== do
Equality double
!= else
Not Equal
enum
<
Less Than extern
> float
Greater Than for
<= goto
Less Than or Equal
if
>=
Greater Than or Equal int
++ long
Increment register
-- return
Decrement
short
&&
Logical AND signed
|| sizeof
Logical OR static
! struct
Logical NOT switch
&
typedef
Bitwise AND
| union
Bitwise OR unsigned
^ void
Bitwise XOR volatile
~
while
Bitwise Complement
<<
Left Shift
>>
Right Shift
?
Ternary Conditional
:
Colon
;
Semicolon
,
Comma
.
Dot
()
Parentheses
[]
Brackets
{}
Braces
11
Output-3:
12
letter [a-zA-Z]
digit [0-9]
%%
{digit}+("E"("+"|"-")?{digit}+)?
printf("\n%s\tis real number", yytext);
{digit}+"."{digit}+("E"("+"|"-")?{digit}+)?
printf("\n%s\t is floating pt no ", yytext);
"if"|"else"|"int"|"char"|"scanf"|"printf"|"switch"|"return"|"struct"|"do"|"while"|"void"|"for"|"float"
printf("\n%s\t is keywords", yytext);
"\a"|"\\n"|"\\b"|"\t"|"\\t"|"\b"|"\\a"
printf("\n%s\tis Escape sequences", yytext);
{letter}({letter}|{digit})*
printf("\n%s\tis identifier", yytext);
"&&"|"<"|"">"|"<="|">="|"="|"+"|"-"|"?"|"*"|"/"|"%"|"&"|"||"
printf("\n%s\toperator ", yytext);
"{"|"}"|"["|"]"|"("|")"|"#"|"'"|"."|"\""|"\\"|";"|","
printf("\n%s\t is a special character", yytext);
%%
int yywrap()
{
return 1;
}
int main()
{
yyin = fopen("test.c", "r");
yylex();
getch();
fclose(yyin);
return 0;
}
Output-4 :
13
Lex File :
%{
#include "y.tab.h"
%}
%%
[0-9]+(\.[0-9]+)? { return NUM; }
[\t] ;
\n return 0;
. return yytext[0];
%%
yywrap() { }
Yacc File :
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token NUM ID
%left '+'
%left '*' '/'
%%
e: e '+' e
| e '-' e
| e '*' e
| e '/' e
| '(' e ')'
| NUM
;
%%
main()
{
printf("enter the expression...\n");
yyparse();
getch();
}
yyerror()
{
printf("invalid expression\n");
getch();
exit(0);
}
14
Output-5
15
Lex File :
%{
#include"y.tab.h"
%}
%%
[a-zA-Z] { return LETTER; }
[0-9] { return DIGIT; }
[_] { return UND; }
[\n] { return NL; }
. { return yytext[0]; }
%%
Yacc File :
%{
#include<stdio.h>
#include<stdlib.h>
%}
%%
%%
int yywrap(void)
{
return 0;
}
int main()
{
16
Output-6 :
17
Lex File :
%{
#include"y.tab.h"
#include<math.h>
extern int yylval;
%}
%%
[0-9]+ { yylval = atoi(yytext); return NUM; }
[+] { return '+'; }
[-] { return '-'; }
[*] { return '*'; }
[/] { return '/'; }
[\t]+ ;
[\n] { return 0; }
. { return yytext[0]; }
%%
Yacc File :
%{
#include <stdio.h>
%}
%token NUM
%left '-''+'
%right '*' '/'
%%
%%
main()
{
printf("Enter the Expr. in terms of integers\n");
if (yyparse() == 0)
{
printf("Success\n");
18
getch();
}
}
yywrap() {}
yyerror()
{
printf("Error\n");
}
Output-7:
19
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 128
#define NONE -1
#define EOS '\0'
#define NUM 257
#define KEYWORD 258
#define ID 259
#define DONE 260
#define MAX 999
char lexemes[MAX];
char buffer[SIZE];
int lastchar=-1;
int lastentry=0;
int tokenval=DONE;
int lineno=1;
int lookahead;
struct entry
{
char *lexptr;
int token;
}
symtable[100];
struct entry
keywords[]= {"if",KEYWORD,"else",KEYWORD,"for",KEYWORD,"int",KEYWORD,"float",KEYWORD,
"double",KEYWORD,"char",KEYWORD,"struct",KEYWORD,"return",KEYWORD,0,0
};
void Error_Message(char *m)
{
fprintf(stderr,"line %d, %s \n",lineno,m);
exit(1);
}
int look_up(char s[ ])
{
int k;
for(k=lastentry; k>0; k--)
if(strcmp(symtable[k].lexptr,s)==0)
return k;
return 0;
}
int insert(char s[ ],int tok)
{
int len;
len=strlen(s);
if(lastentry+1>=MAX)
Error_Message("Symbpl table is full");
if(lastchar+len+1>=MAX)
Error_Message("Lexemes array is full");
lastentry=lastentry+1;
symtable[lastentry].token=tok;
symtable[lastentry].lexptr=&lexemes[lastchar+1];
lastchar=lastchar+len+1;
strcpy(symtable[lastentry].lexptr,s);
return lastentry;
}
/*void Initialize()
{
20
display(t,NONE);
continue;
case '-' :
t=lookahead;
Match(lookahead);
T();
display(t,NONE);
continue;
default :
return;
}
}
}
void parser()
{
lookahead=lexer();
while(lookahead!=DONE)
{
E();
Match(';');
}
}
main()
{
char ans[10];
printf("Enter the expression,at last;and press ctrl-z to terminate\n");
parser();
return 0;
}
Output-8:
23
Lex File:
%{
#include"y.tab.h"
#include<stdio.h>
#include<string.h>
int LineNo = 1;
%}
identifier [a-zA-Z][_a-zA-Z0-9]*
number [0-9]+|([0-9]*\.[0-9]+)
%%
int |
char |
float return TYPE;
{identifier} {
strcpy(yylval.var, yytext);
return VAR;
}
{number} {
strcpy(yylval.var, yytext);
return NUM;
}
\< |
\> |
\>= |
\<= |
== {
strcpy(yylval.var, yytext);
return RELOP;
}
[ \t] ;
\n LineNo++;
. return yytext[0];
%%
Yacc File:
%{
#include<string.h>
#include<stdio.h>
24
struct quad {
char op[5];
char arg1[10];
char arg2[10];
char result[10];
} QUAD[30];
struct stack {
int items[100];
int top;
} stk;
%%
CODE : BLOCK
| STATEMENT CODE
| STATEMENT
;
strcpy($$, QUAD[Index++].result);
}
;
CONDST : IFST {
Ind = pop();
sprintf(QUAD[Ind].result, "%d", Index);
Ind = pop();
sprintf(QUAD[Ind].result, "%d", Index);
}
| IFST ELSEST
;
ELSEST : ELSE {
tInd = pop();
Ind = pop();
push(tInd);
sprintf(QUAD[Ind].result, "%d", Index);
}
BLOCK {
Ind = pop();
sprintf(QUAD[Ind].result, "%d", Index);
};
CONDITION : VAR RELOP VAR { AddQuadruple($2, $1, $3, $$); StNo = Index - 1; }
| VAR
| NUM
;
WHILEST : WHILELOOP {
26
Ind = pop();
sprintf(QUAD[Ind].result, "%d", StNo);
Ind = pop();
sprintf(QUAD[Ind].result, "%d", Index);
}
;
%%
int pop() {
int data;
if (stk.top == -1) {
printf("\n Stack underflow\n");
exit(1);
}
data = stk.items[stk.top--];
return data;
}
yyerror() {
printf("\n Error on line no: %d", LineNo);
}
27
if (argc > 1) {
fp = fopen(argv[1], "r");
if (!fp) {
printf("\n File not found");
return 1;
}
yyin = fp;
}
yyparse();
printf("\n\n\t\t ----------------------------"
"\n\t\t Pos Operator Arg1 Arg2 Result"
"\n\t\t----------------------------");
printf("\n\t\t ----------------------------");
printf("\n\n");
return 0;
}
Input.txt:
Output-9:
28
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct stack {
int no;
struct stack *next;
} *start = NULL;
void push();
int pop();
void display();
void main() {
char ch;
int choice, item;
do {
printf("\n 1: push");
printf("\n 2: pop");
printf("\n 3: display");
printf("\n Enter your choice:");
scanf("%d", &choice);
switch (choice) {
case 1:
push();
break;
case 2:
item = pop();
printf("The delete element is %d", item);
break;
case 3:
display();
break;
default:
printf("\n Wrong choice");
};
void push() {
st *node;
node = (st *)malloc(sizeof(st));
printf("\n Enter the number to be insert");
scanf("%d", &node->no);
node->next = start;
start = node;
}
int pop() {
st *temp;
temp = start;
if (start == NULL) {
29
void display() {
st *temp;
temp = start;
while (temp->next != NULL) {
printf("\nno = %d", temp->no);
temp = temp->next;
}
printf("\nno = %d", temp->no);
}
Output-10:
30
int S[MaxSize];
int Top = -1;
Bool IsEmpty() {
return Top == -1;
}
Bool IsFull() {
return Top == MaxSize - 1;
}
int Pop() {
if (IsEmpty()) {
printf("Stack is Empty.");
return -1;
} else {
return S[Top--];
}
}
int TopEle() {
if (IsEmpty()) {
printf("Stack is Empty.");
return -1;
}
return S[Top];
}
void Display() {
int i;
if (IsEmpty())
printf("Stack is Empty.");
else {
printf("Stack is ...\n");
for (i = Top; i >= 0; i--)
printf("%d\n", S[i]);
}
}
int main() {
int ch, ele;
do {
//clrscr();
printf("Stack Operation Menu\n");
printf("--------------------\n\n");
31
printf("1.Push\n2.Pop\n3.Display\n4.Exit\n\n");
printf("Enter Your Choice [1..4] : ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the Element : ");
scanf("%d", &ele);
Push(ele);
break;
case 2:
ele = Pop();
if (ele != -1)
printf("%d is deleted.\n", ele);
break;
case 3:
Display();
break;
case 4:
break;
default:
printf("Invalid Choice, Try Again.\n");
getch();
}
getch();
} while (ch != 4);
}
Output-11:
32
bool IsEmpty()
{
return Top == NULL;
}
bool IsFull()
{
return False;
}
int Pop()
{
Node *p;
int ele;
if (IsEmpty())
{
printf("Stack is Empty.");
return -1;
}
else
{
ele = Top->data;
p = Top;
Top = Top->next;
free(p);
return ele;
}
}
33
int TopEle()
{
return Top->data;
}
void Display()
{
Node *p;
if (IsEmpty())
printf("Stack is Empty.");
else
{
printf("Stack is ...\n");
for (p = Top; p != NULL; p = p->next)
printf("--->%d\n", p->data);
}
}
int main()
{
int ch, ele;
do
{
//clrscr();
printf("Stack Operation Menu\n");
printf("--------------------\n\n");
printf("1.Push\n2.Pop\n3.Display\n4.Exit\n\n");
printf("Enter Your Choice [1..4] : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter the Element : ");
scanf("%d", &ele);
Push(ele);
break;
case 2:
ele = Pop();
if (ele != -1)
printf("%d is deleted.\n", ele);
break;
case 3:
Display();
break;
case 4:
break;
default:
printf("Invalid Choice, Try Again.\n");
getch();
}
getch();
} while (ch != 4);
}
34
Output-12:
35
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define size 20
btree *stack[size];
int top;
int main()
{
btree *root;
char exp[80];
btree *create(char exp[80]);
void dag(btree *root);
// clrscr();
printf("\nEnter the postfix expression:\n");
scanf("%s", exp);
top = -1;
root = create(exp);
printf("\nThe tree is created.....\n");
printf("\nInorder DAG is : \n\n");
dag(root);
getchar();
}
if (isalpha(ch))
push(temp);
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
temp->right = pop();
temp->left = pop();
push(temp);
}
else
printf("\n Invalid char Expression\n");
pos++;
ch = exp[pos];
36
}
temp = pop();
return(temp);
}
btree* pop()
{
btree *Node;
if (top == -1)
printf("\nError: stack is empty..\n");
Node = stack[top];
top--;
return(Node);
}
Output-13:
37
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char icode[10][30], str[20], opr[10];
int i = 0;
//clrscr();
printf("Enter the set of intermediate code (terminated by exit):\n");
do
{
scanf("%s", icode[i]);
}
while (strcmp(icode[i++], "exit") != 0);
getch();
}
Output-14:
38
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i = 0, j = 0, k, f1 = 1, f = 1, k1 = 0;
void part();
//clrscr();
printf("\n Enter the input string\n");
scanf("%s", o);
strlen(o);
if (i > 3)
{
while (s[j] != '\0')
{
if ((s[j] == '*') == 1 || (s[j] == '/') == 1)
{
k = j;
if (f1 != 0)
{
printf("t1=%c\n", s[k + 1]);
printf("t2=%c%ct1", s[k - 1], s[k]);
}
else
{
if (k > 3)
{
printf("t2=t1%c%c\n", s[k], s[k + 1]);
}
else
{
printf("\t2=t1%c%c\n", s[k], s[k - 1]);
}
}
f = 0;
break;
}
39
j++;
}
j = 0;
while (s[j] != '\0')
{
if ((s[j] == '+') == 1 || (s[j] == '-') == 1)
{
k = j;
if (f == 0)
{
if (k < 3)
{
printf("\nt3=t2%c%c\n", s[k], s[k - 1]);
}
else
{
printf("\nt3=t2%c%c\n", s[k], s[k + 1]);
}
}
else
{
printf("t1=%c%c%c\n", s[k - 1], s[k], s[k + 1]);
}
f1 = 0;
}
j++;
}
printf("%c=t3", o[0]);
}
else
{
printf("t1=%s\n", s);
printf("%c=t1", o[0]);
}
part();
getch();
}
void part()
{
int i = 0, j = 0, k, f1 = 1, f = 1, k1 = 0;
while (o[k1] != '\0')
{
if ((o[k1] == '=') == 1)
{
break;
}
k1++;
}
if (i > 3)
{
40
j = 0;
while (s[j] != '\0')
{
if ((s[j] == '+') == 1 || (s[j] == '-') == 1)
{
k = j;
if (f == 0)
{
if (k < 3)
{
printf("t2=t1%c%c\n", s[k], s[k - 1]);
}
else
{
printf("t2=t1%c%c\n", s[k], s[k + 1]);
}
}
else
{
printf("t1=%c%c%c\n", s[k - 1], s[k], s[k + 1]);
}
f1 = 0;
}
j++;
}
printf("%c=t2", o[0]);
}
else
{
printf("t1=%s\n", s);
printf("%c=t1", o[0]);
}
}
41
Output-15:
42
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
int i, n;
int fact = 1;
cout << "The factorial value is: " << fact << endl;
return 0;
}
#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
int n, f;
f = 1;
printf("Enter the number:\n");
scanf("%d", &n);
do {
f = f * n;
n--;
} while(n > 0);
printf("The factorial value is: %d", f);
getch();
}
Output: