1. Program to count the number of vowels and consonants in a given string.
%{
#include <stdio.h>
int vowels = 0;
int cons = 0;
%}
%%
[aeiouAEIOU] { vowels++; }
[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z] { cons++; }
. ; /* ignore everything else */
%%
int yywrap() {
return 1;
}
int main() {
printf("Enter the string (press Ctrl+D to end input):\n");
yylex();
printf("Number of vowels = %d\nNumber of consonants = %d\n", vowels, cons);
return 0;
}
Bash:
cd "C:\Users\hp\OneDrive\Desktop\midcomplier"
flex int.l
gcc [Link].c -o int
./int
2. Program to count the number of characters, words, spaces, end of lines in a given input file.
%{
#include <stdio.h>
int c = 0, w = 0, s = 0, l = 0;
%}
%%
[ \t] { s++; c++; } /* space or tab */
\n { l++; c++; } /* new line */
[^\t\n ]+ { w++; c += yyleng; } /* word */
. { c++; } /* any other character */
%%
int yywrap() {
return 1;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <inputfile>\n", argv[0]);
return 1;
}
yyin = fopen(argv[1], "r");
if (!yyin) {
printf("Cannot open file %s\n", argv[1]);
return 1;
}
yylex();
printf("\nNumber of characters = %d\n", c);
printf("Number of words = %d\n", w);
printf("Number of spaces = %d\n", s);
printf("Number of lines = %d\n", l);
fclose(yyin);
return 0;
}
[Link]
Hello world
This is a Lex test
Bash:
flex count.l
gcc [Link].c -o count
./count [Link]
3. Program to count the number of characters in a string.
%{
#include <stdio.h>
int count = 0;
%}
%%
[A-Z] { printf("%s is a capital letter\n", yytext); count++; }
[a-z] { printf("%s is not a capital letter\n", yytext); }
\n { return 0; }
. ; /* ignore any other character */
%%
int yywrap() {
return 1;
}
int main() {
printf("Enter a string (press Ctrl+Z then Enter to end):\n");
yylex();
printf("\nNumber of capital letters in the given input = %d\n", count);
return 0;
}
Bash:
flex capital.l
gcc [Link].c -o capital
./capital
4. Program to count the no of comment line in a given C program. Also eliminate them and
copy that program into separate file.
%{
#include <stdio.h>
int com = 0;
%}
%x COMMENT
%%
"/*" { BEGIN(COMMENT); com++; } /* start of multi-line comment */
<COMMENT>"*/" { BEGIN(INITIAL); } /* end of multi-line comment */
<COMMENT>\n { com++; } /* count comment lines */
<COMMENT>. { /* ignore comment content */ }
"//".* { com++; } /* single-line comment */
.|\n { fprintf(yyout, "%s", yytext); } /* copy rest of code */
%%
int yywrap() { return 1; }
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <sourcefile> <destinationfile>\n", argv[0]);
return 1;
}
yyin = fopen(argv[1], "r");
if (!yyin) {
printf("Cannot open source file: %s\n", argv[1]);
return 1;
}
yyout = fopen(argv[2], "w");
if (!yyout) {
printf("Cannot open destination file: %s\n", argv[2]);
fclose(yyin);
return 1;
}
yylex();
printf("Number of comment lines = %d\n", com);
fclose(yyin);
fclose(yyout);
return 0;
}
Input.c
#include <stdio.h>
// This is a single-line comment
int main() {
/* This is
a multi-line comment */
printf("Hello!\n"); // End of line comment
return 0;
}
Output.c
#include <stdio.h>
int main() {
printf("Hello!\n");
return 0;
}
Bash:
cd "C:\Users\hp\OneDrive\Desktop\midcomplier"
flex count.l
gcc [Link].c -o count
./count input.c output.c
5. Program to recognize and count the number of pascal identifiers in a given input file.
%{
#include <stdio.h>
int identifier_count = 0;
%}
letter [a-zA-Z]
digit [0-9]
%%
{letter}({letter}|{digit})* { identifier_count++; } /* valid Pascal identifier */
.|\n { /* ignore everything else */ }
%%
int yywrap() { return 1; }
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <input_file>\n", argv[0]);
return 1;
}
yyin = fopen(argv[1], "r");
if (!yyin) {
perror("Error opening file");
return 1;
}
yylex();
printf("Total identifiers found = %d\n", identifier_count);
fclose(yyin);
return 0;
}
[Link]
program Test;
var
Sum, Number1, Number2: integer;
begin
Sum := Number1 + Number2;
end.
Bash:
cd "C:\Users\hp\OneDrive\Desktop\midcomplier"
flex countid.l
gcc [Link].c -o countid
./countid [Link]
6. Program to identify the unsigned numbers and identifiers are valid or not.
%{
#include <stdio.h>
%}
num [0-9]
id [A-Za-z_][A-Za-z0-9_]*
%%
{id} { printf("Valid Identifier: %s\n", yytext); }
{num}+(\.{num}+)?(E[+-]?{num}+)? { printf("Valid Unsigned Number: %s\n", yytext); }
[ \t\n]+ { /* ignore spaces, tabs, newlines */ }
. { printf("Invalid token: %s\n", yytext); }
%%
int yywrap() { return 1; }
int main() {
printf("Enter input (Ctrl+Z to end):\n");
yylex();
return 0;
}
Bash:
cd "C:\Users\hp\OneDrive\Desktop\midcomplier"
flex countvalid.l
gcc [Link].c -o countvalid
./countvalid
Input:
abc123
12.34
1.2E5
12E
@var
[Link] and Yaac Program Program to recognize a valid variable, which starts with a letter,.
followed by any number of letters or digits.
var.y
%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
int yyerror(const char *msg);
%}
%token DIGIT LETTER UND NL
%%
stmt: variable NL { printf("Valid Identifier\n"); exit(0); }
;
variable: LETTER alphanumeric
;
alphanumeric:
LETTER alphanumeric
| DIGIT alphanumeric
| UND alphanumeric
| /* empty */
;
%%
int yyerror(const char *msg) {
printf("Invalid Identifier\n");
exit(0);
}
int main() {
printf("Enter a variable name:\n");
yyparse();
return 0;
}
var.l
%{
#include "[Link].h"
%}
%%
[a-zA-Z] { return LETTER; }
[0-9] { return DIGIT; }
_ { return UND; }
\n { return NL; }
. { return yytext[0]; }
%%
int yywrap() { return 1; }
bash:
flex var.l
bison -d var.y
gcc [Link].c [Link].c -o var
./var
Program to recognize strings 'aaab', 'abbb", "ab" and 'a' using grammar (a"b", n>=0)
string.y
%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
int yyerror(const char *msg);
%}
%token A B NL
%%
stmt: s NL { printf("Valid String\n"); exit(0); }
;
s: A s B /* Recursive rule to match a...b pairs */
|AB
|B
;
%%
int yyerror(const char *msg) {
printf("Invalid String\n");
exit(0);
}
int main() {
printf("Enter the string:\n");
yyparse();
return 0;
}
string.l
%{
#include "[Link].h"
%}
%%
[aA] { return A; }
[bB] { return B; }
\n { return NL; }
. { return yytext[0]; }
%%
int yywrap() {
return 1;
}
Bash:
flex string.l
yacc -d string.y
gcc [Link].c [Link].c -o string
./string
Bash:
flex string.l
bison -d string.y
gcc [Link].c [Link].c -o string
string
Program recognize the grammar (a"b, n>=10) token ABNL
string.y
%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
int yyerror(char *s);
%}
%token A B NL
%%
stmt : S B NL { printf("Valid String\n"); exit(0); }
;
S : A A A A A A A A A A A_list
;
A_list : A A_list
| /* empty */
;
%%
int yyerror(char *s) {
printf("Invalid String\n");
exit(0);
}
int main() {
printf("Enter the string:\n");
yyparse();
return 0;
}
string.l
%{
#include "[Link].h"
%}
%%
[aA] { return A; }
[bB] { return B; }
\n { return NL; }
. { return yytext[0]; }
%%
Bash:
flex string.l
bison -d string.y # or use yacc -d string.y
gcc [Link].c [Link].c -o string
./string # or "[Link]" on Windows
Program to evaluate Arithmetic expression.
expr.y
%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
int yyerror(char *s);
%}
%token NUMBER
%left '+' '-'
%left '*' '/'
%left '(' ')'
%%
E : E '+' E { $$ = $1 + $3; }
| E '-' E { $$ = $1 - $3; }
| E '*' E { $$ = $1 * $3; }
| E '/' E {
if($3 == 0) {
printf("Division by zero error\n");
exit(1);
} else {
$$ = $1 / $3;
}
}
| '(' E ')' { $$ = $2; }
| NUMBER { $$ = $1; }
;
%%
int main() {
printf("Enter arithmetic expression:\n");
yyparse();
return 0;
}
int yyerror(char *s) {
printf("Expression is invalid.\n");
exit(0);
}
expr.l
%{
#include "[Link].h"
#include <stdlib.h>
%}
%%
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
[\t ]+ ; // ignore spaces
. { return yytext[0]; }
%%
Bash:
flex expr.l
bison -d expr.y # or yacc -d expr.y
gcc [Link].c [Link].c -o expr
./expr # or [Link] on Windows
Input:
3+5*2