Compiler Design labs
Compiler Design labs
com/tW1vSGRVPd
#include <ctype.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main() {
char input[100];
printf("Enter number: ");
scanf("%s", input);
if(isDigit(input)) {
printf("Input is a digit\n");
} else {
printf("Input is a string\n");
}
}
2. count_operator.c : https://www.online-cpp.com/tUNIf473Rx
#include <stdio.h>
int main() {
char expression[100];
printf("Enter an expression: ");
fgets(expression, 100, stdin);
return 0;
}
3. valid_comment.c : https://www.online-cpp.com/o81IryF4kG
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
if (length < 2) {
return false; // Comment must be at least 2 characters (// or /*)
}
return true;
}
int main() {
char inputString[100]; // Adjust the array size as needed
if (isValidComment(inputString)) {
printf("'%s' is a valid C comment.\n", inputString);
} else {
printf("'%s' is not a valid C comment.\n", inputString);
}
return 0;
}
4. dfa_accept_ aba_as_substring.c :
https://www.online-cpp.com/B2zTwxWlRy
#include <cstring>
#include <iostream>
using namespace std;
bool isValidIdentifierChar(char c) {
return (isalnum(c) || c == '_');
}
return true;
}
int main() {
char inputString[100];
if (isValidIdentifier(inputString)) {
printf("'%s' is a valid identifier.\n", inputString);
} else {
printf("'%s' is not a valid identifier.\n", inputString);
}
return 0;
}
6. valid_keyword.c : https://www.online-cpp.com/eMIHfpbPla
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
// Array of C keywords
const char *keywords[] = {
"break", "case", "char", "const", "continue", "default", "do",
"double", "else", "enum", "extern", "float", "for", "goto", "if",
"int", "long", "return", "sizeof", "struct", "switch", "typedef", "void", "while"
};
return false;
}
int main() {
char inputString[50];
if (isKeyword(inputString)) {
printf("'%s' is a C keyword.\n", inputString);
} else {
printf("'%s' is not a C keyword.\n", inputString);
}
return 0;
}
7. recognize_token.c : https://www.online-cpp.com/dAPrgpM1qQ
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
int main() {
char input[100];
const char delimiters[] = " \t\n";
printf("Enter input: ");
fgets(input, sizeof(input), stdin);
return 0;
}
Lex
Lex is a computer program that generates lexical analyzers. Lex reads an
input stream specifying the lexical analyzer and outputs source code
implementing the lexer in the C programming language.
Flex
FLEX (fast lexical analyzer generator) is a tool/computer program for
generating lexical analyzers (scanners or lexers) written by Vern Paxson in C
around 1987. It is used together with Berkeley Yacc parser generator or GNU
Bison parser generator. Flex and Bison both are more flexible than Lex and
Yacc and produces faster code. Bison produces parser from the input file
provided by the user. The function yylex() is automatically generated by the
flex when it is provided with a .l file and this yylex() function is expected by
parser to call to retrieve tokens from current/this token stream.
Note: The function yylex() is the main flex function that runs the Rule Section
and extension (.l) is the extension used to save the programs.
Installing flex
On ubuntu
sudo apt-get update
sudo apt-get install flex
On linux
sudo xbps-install flex
On windows
https://www.geeksforgeeks.org/how-to-install-flex-on-windows/
How its work: The Lex complier transforms the input patterns into a transition
diagram and generates code in a file called lex.yy.c, that simulates this transition
diagram.
Use of Lex:
==>An input file lex.l is written in the Lex language and describe the lexical analyzer.
The Lex complier transforms lex.l to C program , in a file that is always named
lex.yy.c.
==>The C output is a.out that can take a stream of input characters and produce a
stream of tokens.
To understand the complete concept , just go through this below example.
Here is the source code of How to find out the lowercase and uppercase of a string??
%{
#include
int Upper=0;
int Lower=0;
%}
%%
[A-Z] {printf("Uppercase\t");Upper++;}
[a-z] {printf("Lowercase\t");Lower++;}
%%
int
yywrap()
return 1;
main()
{
printf("Enter a string\n");
yylex();
%%
"int"|"float"|"char"|"void" { printf("Keyword: %s\n", yytext); }
[a-zA-Z][a-zA-Z0-9]* { printf("Identifier: %s\n", yytext); }
[0-9]+ { printf("Numeric Literal: %s\n", yytext); }
"=" { printf("Assignment Operator\n"); }
";" { printf("Semicolon\n"); }
[ \t] { /* Ignore space and tab characters */ }
\n { /* Ignore newline characters */ }
. { printf("Invalid input\n"); }
%%
int main() {
printf("Enter an input string:\n");
yylex();
return 0;
}
/*
EXAMPLE:
Enter an input string:
int a = 69;
Keyword: int
Identifier: a
Assignment Operator
Numeric Literal: 69
Semicolon
*/
9. check_digit.l
/* Lex program to check whether input is digit or not. */
%{
#include<stdio.h>
#include<stdlib.h>
%}
/* Rule Section */
%%
^[0-9]* printf("DIGIT");
^[^0-9]|[0-9]*[a-zA-Z] printf("NOT A DIGIT");
.;
%%
int main()
{
yylex();
return 0;
}
/*
EXAMPLE:
dfa
NOT A DIGIT
69
DIGIT
^C
*/
10. check_digit_repeatedly.l
/* Lex program to check whether input is digit or not. */
%{
#include<stdio.h>
#include<stdlib.h>
%}
/* Rule Section */
%%
^[0-9]* { printf("DIGIT\n"); }
^[^0-9]|[0-9]*[a-zA-Z] { printf("NOT A DIGIT\n"); }
. { /* Ignore other characters */ }
%%
int main()
{
char input[100];
int choice;
do {
printf("Enter a string:\n");
fgets(input, sizeof(input), stdin);
yy_scan_string(input);
yylex();
return 0;
}
/*
EXAMPLE:
Enter a string:
69
DIGIT
%%
"int"|"float"|"char"|"void"|"if"|"else"|"while" { printf("%s is a keyword\n",
yytext); }
. { printf("Not a keyword\n"); }
%%
int main() {
printf("Enter an input string:");
yylex();
return 0;
}
/*
EXAMPLE:
Enter an input string:int
int is a keyword
*/
12. count_operator.l
/* count the number of operator used in given input */
%{
#include <stdio.h>
#include <string.h>
int operatorCount = 0;
%}
%%
[+\-*/=] { operatorCount++; }
. { /* Ignore other characters */ }
%%
int main() {
char input[100];
printf("Enter an input string:\n");
fgets(input, sizeof(input), stdin);
yy_scan_string(input);
yylex();
return 0;
}
/*
EXAMPLE:
Enter an input string:
int a = 5 + 6 * 9 - 5;
Number of operators: 4
*/
13. three_address_code.l
/* Write a flex program to generate three address code */
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
%}
%%
[0-9]+ { printf("t%d = %s\n", yylineno, yytext); }
[-+*/] { printf("t%d = t%d %s t%d\n", yylineno, yylineno-2, yytext,
yylineno-1); }
\n { /* Ignore newline character */ }
. { printf("Invalid input\n"); exit(1); }
%%
int main() {
printf("Enter an arithmetic expression:\n");
yylex();
return 0;
}
/*
EXAMPLE
Enter an arithmetic expression:
3*4*5
t1 = 3
t1 = t-1 * t0
t1 = 4
t1 = t-1 * t0
t1 = 5
^C
*/
14. intermediate_code_generation.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct exp {
int pos;
char op;
} k[15];
void findopr();
void explore();
void fleft(int);
void fright(int);
int main() {
printf("INTERMEDIATE CODE GENERATION\n");
printf("Enter the Expression: ");
scanf("%s", str);
printf("The intermediate code:\t\tExpression\n");
findopr();
explore();
return 0;
}
void findopr() {
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ':') {
k[j].pos = i;
k[j++].op = ':';
}
}
void explore() {
i = 0;
while (k[i].op != '\0') {
fleft(k[i].pos);
fright(k[i].pos);
str[k[i].pos] = tmpch--;
printf("\t%c := %s%c%s\t\t", str[k[i].pos], left, k[i].op, right);
for (j = 0; j < strlen(str); j++) {
if (str[j] != '$')
printf("%c", str[j]);
}
printf("\n");
i++;
}
fright(-1);
if (no == 0) {
fleft(strlen(str));
printf("\t%s := %s\n", right, left);
exit(0);
}
void fleft(int x) {
int w = 0, flag = 0;
x--;
while (x >= 0 && str[x] != '+' && str[x] != '*' && str[x] != '=' && str[x] !=
'\0' && str[x] != '-' && str[x] != '/' && str[x] != ':') {
if (str[x] != '$' && flag == 0) {
left[w++] = str[x];
left[w] = '\0';
str[x] = '$';
flag = 1;
}
x--;
}
}
void fright(int x) {
int w = 0, flag = 0;
x++;
while (x < strlen(str) && str[x] != '+' && str[x] != '*' && str[x] != '\0' &&
str[x] != '=' && str[x] != ':' && str[x] != '-' && str[x] != '/') {
if (str[x] != '$' && flag == 0) {
right[w++] = str[x];
right[w] = '\0';
str[x] = '$';
flag = 1;
}
x++;
}
}