CD Record
CD Record
#include <stdio.h>
int word_count = 0;
%}
%%
[A-Za-z]+ { word_count++; }
%%
int main() {
yylex();
return 0;
int yywrap() {
return 1;
Output:
hello world
Number of words: 2
2. lex program to count digits and words
%{
#include <stdio.h>
%}
%%
%%
int main() {
yylex();
return 0;
int yywrap() {
return 1;
Found a word: my
Found a word: is
Found a number: 21
Found a word: N
Found a number: 71
Found a word: A
#include <stdio.h>
#include <stdlib.h>
int op = 0,i;
float a, b;
%}
dig [0-9]+|([0-9]*)"."([0-9]+)
add "+"
sub "-"
mul "*"
div "/"
pow "^"
ln \n
%%
{dig} {digi();}
{add} {op=1;}
{sub} {op=2;}
{mul} {op=3;}
{div} {op=4;}
{pow} {op=5;}
%%
void digi()
{
if(op == 0)
a=atof(yytext);
else
b=atof(yytext);
switch(op)
case 1:a=a+b;
break;
case 2:a=a-b;
break;
case 3:a=a*b;
break;
case 4:a=a/b;
break;
case 5:
for(i=a;b>1;b--)
a=a*i;
}
break;
op=0;
yylex();
return 0;
int yywrap()
return 1;
Output:
30+21
#include <stdio.h>
#include <unistd.h>
#include <string.h>
};
if (strcmp(word, keywords[i]) == 0) {
return 1;
return 0;
%}
%%
[a-zA-Z_][a-zA-Z0-9_]* {
if (is_keyword(yytext)) {
} else {
%%
int main() {
yylex();
return 0;
int yywrap() {
return 1;
input:
int main() {
int a = 10;
float b = 20.5;
if (a > b) {
return 1;
} else {
return 0;
Output:
int main() {
int a = 10;
Found identifier: a
float b = 20.5;
Found identifier: b
if (a > b) {
Found keyword: if
Found identifier: a
Found identifier: b
return 1;
} else {
return 0;
}
WEEK 2:
#include <stdio.h>
#include <string.h>
char table[5][6][10];
int numr(char c)
switch (c)
case 'S':
return 0;
case 'A':
return 1;
case 'B':
return 2;
case 'C':
return 3;
case 'a':
return 0;
case 'b':
return 1;
case 'c':
return 2;
case 'd':
return 3;
case '$':
return 4;
return (2);
int main(){
int i, j, k;
printf("%s\n", prod[i]);
fflush(stdin);
k = strlen(first[i]);
if (first[i][j] != '@')
}
for (i = 0; i < 7; i++)
if (strlen(pror[i]) == 1)
if (pror[i][0] == '@')
k = strlen(follow[i]);
strcpy(table[0][1], "a");
strcpy(table[0][2], "b");
strcpy(table[0][3], "c");
strcpy(table[0][4], "d");
strcpy(table[0][5], "$");
strcpy(table[1][0], "S");
strcpy(table[2][0], "A");
strcpy(table[3][0], "B");
strcpy(table[4][0], "C");
printf("\n--------------------------------------------------------\n");
{
printf("%-10s", table[i][j]);
if (j == 5)
printf("\n--------------------------------------------------------\n");
}}
Output:
The following grammar is used for Parsing Table:
S->A
A->Bb
A->Cd
B->aB
B->@
C->Cc
C->@
--------------------------------------------------------
a b c d $
--------------------------------------------------------
--------------------------------------------------------
--------------------------------------------------------
--------------------------------------------------------
--------------------------------------------------------
WEEK 3:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int temp_count = 0;
int i = 0;
char temp[2];
char op;
if (isalnum(expression[i])) {
expression[i+1] == '/') {
left_operand = expression[i];
op = expression[++i];
right_operand = expression[++i];
i++;
}
int main () {
gets(expression);
generate_three_address_code(expression);
return 0;
Input:
a+b*c
output:
Enter an arithmetic expression: a+b*c
Three-address code:
t0 = a +
WEEK 4:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SHIFT 1
#define REDUCE 2
#define ACCEPT 3
#define ERROR 4
// Token definitions
#define TOK_IDENTIFIER 1
#define TOK_PLUS 2
#define TOK_STAR 3
#define TOK_LPAREN 4
#define TOK_RPAREN 5
#define TOK_END 6
typedef struct {
int lhs;
int rhs_len;
} Production;
typedef struct {
int action;
int value;
} Action;
Action ACTION[10][7];
int GOTO[10][5];
typedef struct {
int state;
int symbol;
} StackItem;
StackItem stack[100];
int input_pos = 0;
int get_next_token() {
return input[input_pos++];
Production get_production(int p) {
Production productions[] = {
{1, 3},
};
return productions[p];
}
stack[++top].state = state;
stack[top].symbol = symbol;
StackItem pop() {
return stack[top--];
StackItem peek() {
return stack[top];
// Parsing function
void parse() {
while (1) {
if (action.action == SHIFT) {
push(action.value, lookahead);
pop();
push(goto_state, p.lhs);
printf("Input accepted\n");
return;
} else {
printf("Error in input\n");
return;
int main() {
// Initialize the parsing table with some dummy values for the example
parse();
return 0;
}
Output:
Input accepted
Week 5:
<program> ::= <block>
| { <slist> }
| <ifstatement>
| <whilestatement>
| <block>
| <printstatement>
| <empty>
| <term>
<addingop> ::= + | -
| <factor>
<multop> ::= * | /
| <identifier>
| <identifier> [ <expression> ]
| ( <expression> )
| <digit> <constant>
| <letter>
| <digit>
<empty> ::= ε
Program :
#include <stdio.h>
#include <stdlib.h>
enum {
};
enum {
};
// Data structures
typedef struct {
int type;
} Token;
typedef struct {
int action;
int state;
} Action;
typedef struct {
int state;
int symbol;
} Goto;
Action action_table[10][24] = {
{ {SHIFT, 1, 0}, {REDUCE, 0, 3}, {SHIFT, 2, 0}, {SHIFT, 3, 0}, {REDUCE, 1, 3},
{REDUCE, 2, 3}, {SHIFT, 4, 0}, {REDUCE, 3, 3}, {REDUCE, 4, 3}, {REDUCE, 5, 3},
{REDUCE, 6, 3}, {REDUCE, 7, 3}, {SHIFT, 5, 0}, {SHIFT, 6, 0}, {SHIFT, 7, 0},
{SHIFT, 8, 0}, {SHIFT, 9, 0}, {SHIFT, 10, 0}, {SHIFT, 11, 0}, {SHIFT, 12, 0},
{SHIFT, 13, 0}, {SHIFT, 14, 0}, {SHIFT, 15, 0}, {SHIFT, 16, 0}
},
};
Goto goto_table[10][9] = {
{ {1, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0} },
};
typedef struct {
int items[100];
int top;
} Stack;
void push(Stack *stack, int item) {
stack->items[++stack->top] = item;
return stack->items[stack->top--];
return stack->items[stack->top];
// Parsing function
Stack stack;
stack.top = -1;
int idx = 1;
while (1) {
if (action.action == SHIFT) {
push(&stack, lookahead);
push(&stack, action.state);
lookahead = tokens[idx++];
// Perform reduction
int reduce_length = action.production_len;
int lhs = action.state; // Adjust based on the grammar and production rules
pop(&stack);
push(&stack, lhs);
push(&stack, goto_state);
printf("Parsing successful!\n");
break;
} else {
printf("Syntax error!\n");
break;
int main() {
TOK_RBRACKET,
TOK_PRINT,
TOK_MULTIPLY,
TOK_RBRACE,
TOK_EOF};
parse(tokens, num_tokens);
return 0;
Output: