SplitPDFFile 1 To 10
SplitPDFFile 1 To 10
}
STATEMENT : DESCT ‘;’ Yyin = fp;
| ASSIGNMENT ‘;’ ; }
Return yyparse();
ASSIGNMENT: EXPR ‘=’ EXPR { checktype($1, “=”, $3, “”, $1); } ; If (!strcmp(variable[i][1], arg2)) strcpy(type2, variable[i][0]);
If (!fp) { Printf(“\nError\n”);
Return 0; Changes Made:
} Removed redundant code, such as VARLIST, which is unused.
Int r;
Float e; Ex5
Float f;
R = a + b; Threeaddrcode.l
F = r + e; C
E = f + e; Copy code
} %{
#include <string.h> %type <var> EXPR ASSIGNMENT CONDITION IFST ELSEST WHILEST
#include <stdio.h>
| WHILEST ; };
};
%%
EXPR : EXPR ‘+’ EXPR { addQuadruple(“+”, $1, $3, $$); } Int main(int argc, char *argv[]) {
}
IFST : IF ‘(‘ CONDITION ‘)’ BLOCK { Yyparse();
QUAD[Index++] = (struct quad){“GOTO”, “”, “”, “-1”}; Printf(“\n\n------------------------------\nPos Operator Arg1 Arg2 Result\n”);
Push(Index – 1); For (int I = 0; I < Index; i++)
}; Printf(“%d\t%s\t%s\t%s\t%s\n”, I, QUAD[i].op, QUAD[i].arg1, QUAD[i].arg2,
QUAD[i].result);
Printf(“------------------------------\n”);
ELSEST : ELSE BLOCK {
Return 0; 0 < a b t0
} 1 == t0 FALSE 5
2 + a b t1
Int yyerror() { 3 = t1 a
Printf(“\nError on line no: %d”, LineNo); 4 GOTO 5
Return 0; 5 < a b t2
} 6 == t2 FALSE 10
Test.c 7 + a b t3
C 8 = t3 a
While (a < b) { a = a + b; } 13 = t5 c
Commands to Execute 16 = t6 c
Bash Simplifications Made:
Copy code Consolidated repetitive code and logic.
$ yacc -d threeaddrcode.y Removed unnecessary sprintf in favor of direct string assignments.
$ lex threeaddrcode.l Combined similar actions like GOTO handling.
$ gcc lex.yy.c y.tab.c -o threeaddr Simplified stack operations by directly working with indexes.
$ ./threeaddr test.c This shortened version achieves the same output with better clarity and reduced
redundancy.
Output
Css
Copy code
DIGIT { $$ = $1; }
%% | number DIGIT { $$ = 10 * $1 + $2; }
List: ;
| list stat ‘\n’
; Cal.l
C (9*3)/3
Copy code A=9
%{ B=4
#include “y.tab.h” A+b
%} Output:
%% Plaintext
[a-z] { yylval = yytext[0] – ‘a’; return LETTER; } Copy code
[0-9] { yylval = yytext[0] – ‘0’; return DIGIT; } 17
} else {
} }
Int main() { Printf(“Keywords: %d\nIdentifiers: %d\nOperators: %d\nNumbers: %d\n”, kw, id, op,
num);
Char ch, str[25];
Fclose(f1);
FILE *f1 = fopen(“input1.c”, “r”);
Return 0;
If (!f1) {
}
Printf(“Error opening file.\n”);
Return 1;
Void keyw(char *p) {
}
If (p[0] == ‘\0’) return;
If (isdigit(p[0])) {
While ((ch = fgetc(f1)) != EOF) {
Printf(“%s is a number\n”, p); Copy code
Num++; $ gcc lexnew.c -o lexnew
} else { $ ./lexnew
For (int k = 0; k < 32; k++) { For the same input, the output remains identical:
If (strcmp(keys[k], p) == 0) {
Printf(“%s is a keyword\n”, p); Plaintext
} I is an identifier
I = 0; Const is a keyword
} = is an operator
Numbers: 3
Bash