0% found this document useful (0 votes)
18 views

CD LexProgram

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CD LexProgram

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

2a:Lexical Analyzer using Lex Tool

Here is a short and simplified Lex program to create a lexical analyzer that recognizes identifiers,
keywords, numbers, operators, and whitespace.

Lex Program: lexical.l


%{
#include <stdio.h>
%}

%%
[a-zA-Z_][a-zA-Z0-9_]* {
if (strcmp(yytext, "if") == 0 || strcmp(yytext, "else") == 0 ||
strcmp(yytext, "while") == 0 || strcmp(yytext, "return") == 0)
printf("Keyword: %s\n", yytext);
else
printf("Identifier: %s\n", yytext);
}
[0-9]+ { printf("Number: %s\n", yytext); }
[+\-*/=<>!] { printf("Operator: %s\n", yytext); }
[ \t\n]+ { /* Skip whitespace */ }
. { printf("Unknown token: %s\n", yytext); }
%%

int main() {
yylex();
return 0;
}

int yywrap() {
return 1;
}

Explanation:

1. Header Section (%{...%}):


o Includes stdio.h for output functions.
2. Rules Section (%% ... %%):
o Identifiers and Keywords:
 Matches alphanumeric strings starting with a letter or underscore.
 Checks against a list of keywords (if, else, etc.) to differentiate from identifiers.
o Numbers:
 Matches sequences of digits.
o Operators:
 Matches basic operators like +, -, *, /, = and comparison operators.
o Whitespace:
 Matches spaces, tabs, and newlines but does not produce any output.
o Unknown Tokens:
Matches any character not covered by the above rules.
3. Auxiliary Functions:
o main(): Starts the lexical analysis.
o yywrap(): Returns 1 to indicate EOF (end of input).

How to Compile and Run:

1. Save the code in a file named lexical.l.


2. Use the following commands:
3. lex lexical.l
4. gcc lex.yy.c -o lexical -ll
5. ./lexical
6. Enter the input text directly.

Example Run:

Input:

int x = 42;
if (x > 10) x = x + 1;

Output:

Keyword: int
Identifier: x
Operator: =
Number: 42
Operator: ;
Keyword: if
Operator: (
Identifier: x
Operator: >
Number: 10
Operator: )
Identifier: x
Operator: =
Identifier: x
Operator: +
Number: 1
Operator: ;

This program demonstrates how to use Lex for a basic lexical analyzer in a simplified and clear manner.

2b:Identifying Comments,Validating Operators, and Checking Identifiers with Lex


Here’s a short and simplified Lex program to identify comments, validate operators, and check
identifiers.

Lex Program: analyzer.l


%{
#include <stdio.h>
%}

%%
\/\/.* { printf("Comment: %s\n", yytext); }
[a-zA-Z_][a-zA-Z0-9_]* { printf("Identifier: %s\n", yytext); }
[+\-*/=<>!] { printf("Operator: %s\n", yytext); }
[ \t\n]+ { /* Ignore whitespace */ }
. { printf("Invalid Token: %s\n", yytext); }
%%

int main() {
printf("Enter code (Ctrl+D to end input):\n");
yylex();
return 0;
}

int yywrap() {
return 1;
}

Explanation:

1. Rules Section:
o Comments:
 Matches single-line comments starting with //.
o Identifiers:
 Matches valid identifiers (alphanumeric strings starting with a letter or
underscore).
o Operators:
 Matches common operators like +, -, *, /, =, <, > and !.
o Whitespace:
 Matches spaces, tabs, and newlines but doesn’t produce output.
o Invalid Tokens:
 Matches any character not covered by the above rules and flags it as invalid.
2. Auxiliary Functions:
o main(): Starts the lexical analysis and reads input until EOF.
o yywrap(): Signals the end of input.

How to Compile and Run:


1. Save the program in a file called analyzer.l.
2. Use the following commands:
3. lex analyzer.l
4. gcc lex.yy.c -o analyzer -ll
5. ./analyzer
6. Enter the code you want to analyze and press Ctrl+D (EOF) to finish.

Example Run:

Input:

// This is a comment
x = y + 42;
invalid$char

Output:

Comment: // This is a comment


Identifier: x
Operator: =
Identifier: y
Operator: +
Invalid Token: 4
Invalid Token: 2
Operator: ;
Invalid Token: i
Invalid Token: n
Invalid Token: v
Invalid Token: a
Invalid Token: l
Invalid Token: i
Invalid Token: d
Invalid Token: $
Invalid Token: c
Invalid Token: h
Invalid Token: a
Invalid Token: r

This program demonstrates how Lex can be used to build a simple lexical analyzer for comments,
operators, and identifiers with minimal effort.

3E:Count Vowels,Constants,Words,Characters,Blanks,Lines using


Lex
Here’s a short and simplified Lex program that counts vowels, constants (numbers), words,
characters, blanks (spaces), and lines:
Lex Program: count_stats.l
%{
#include <stdio.h>
#include <ctype.h>

int vowels = 0;
int constants = 0;
int words = 0;
int characters = 0;
int blanks = 0;
int lines = 0;
%}

%%
[aeiouAEIOU] { vowels++; characters++; }
[0-9]+ { constants++; characters++; }
[a-zA-Z_][a-zA-Z0-9_]* { words++; characters++; }
[ \t] { blanks++; characters++; }
\n { lines++; }
. { characters++; }
%%

int main() {
printf("Enter text (Ctrl+D to end input):\n");
yylex(); // Start lexical analysis

// Output the counts


printf("\nStatistics:\n");
printf("Vowels: %d\n", vowels);
printf("Constants: %d\n", constants);
printf("Words: %d\n", words);
printf("Characters: %d\n", characters);
printf("Blanks: %d\n", blanks);
printf("Lines: %d\n", lines);

return 0;
}

int yywrap() {
return 1;
}

Explanation:

1. Rules Section:
o Vowels:
 Matches characters that are vowels (both uppercase and lowercase). Each vowel
increments the vowels count and characters count.
o Constants (Numbers):
 Matches any sequence of digits and increments the constants and characters
counts.
o Words:
 Matches identifiers (alphanumeric strings starting with a letter or underscore) and
increments the words and characters counts.
o Blanks (Spaces and Tabs):
 Matches spaces or tabs and increments the blanks and characters counts.
o Newlines:
 Matches line breaks and increments the lines count.
o Other Characters:
 Matches any other characters and increments the characters count.
2. Auxiliary Functions:
o main(): Starts the lexical analysis and displays the counts after processing the input.
o yywrap(): Signals the end of input.

How to Compile and Run:

1. Save the program in a file called count_stats.l.


2. Use the following commands to compile and run:
3. lex count_stats.l
4. gcc lex.yy.c -o count_stats -ll
5. ./count_stats
6. Enter the text you want to analyze and press Ctrl+D (EOF) to finish.

Example Run:

Input:

Hello World 123


This is a test 456

Output:

Enter text (Ctrl+D to end input):

Statistics:
Vowels: 9
Constants: 2
Words: 6
Characters: 29
Blanks: 7
Lines: 2

This Lex program effectively counts the specified categories, providing a simple example of lexical
analysis and counting various elements in a text.
3f:Convert the substring abc to ABC From the given input string
Here is a short and simplified Lex program that converts the substring "abc" to "ABC" from a given
input string.

Lex Program: convert_abc.l


%{
#include <stdio.h>
%}

%%
abc { printf("ABC"); } // Replace 'abc' with 'ABC'
. { putchar(yytext[0]); } // Print all other characters as they are
%%

int main() {
printf("Enter a string (Ctrl+D to end input):\n");
yylex(); // Start lexical analysis
return 0;
}

int yywrap() {
return 1;
}

Explanation:

1. Rules Section:
o Substitution for "abc":
 The rule abc { printf("ABC"); } matches the substring "abc" and replaces it
with "ABC".
o Printing Other Characters:
 The rule . matches any character not covered by the previous rule and prints it as
is using putchar(yytext[0]).
2. Auxiliary Functions:
o main(): Starts the lexical analysis and processes the input string.
o yywrap(): Returns 1 to indicate the end of input.

How to Compile and Run:

1. Save the program in a file called convert_abc.l.


2. Use the following commands to compile and run:
3. lex convert_abc.l
4. gcc lex.yy.c -o convert_abc -ll
5. ./convert_abc
6. Enter the input string and press Ctrl+D (EOF) to finish.

Example Run:

Input:

abc is the first letter of the alphabet.

Output:

ABC is the first letter of the alphabet.

This Lex program demonstrates how to replace a specific substring ("abc") with a new one ("ABC")
while leaving other characters in the input string unchanged.

4a:Three address code using lex and yacc


Here’s a short and simplified example of using Lex and Yacc to generate Three-Address Code (TAC)
for basic arithmetic expressions. This program will take input expressions like a = b + c and generate
a corresponding TAC.

Step 1: Lex Program (lexer.l)


%{
#include "y.tab.h" // Include the Yacc header for token definitions
%}

%%

[a-zA-Z_][a-zA-Z0-9_]* { yylval.s = strdup(yytext); return ID; }


[0-9]+ { yylval.i = atoi(yytext); return NUM; }
[+\-*/=] { return yytext[0]; }
[ \t\n]+ { /* Skip spaces and newlines */ }
. { return yytext[0]; }

%%

int yywrap() {
return 1;
}

Step 2: Yacc Program (parser.y)


%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int tempCount = 1; // Temporary variable counter for TAC

// Structure for the three-address code (TAC) generation


void generateTAC(char *op, char *arg1, char *arg2, char *result) {
printf("%s = %s %s %s\n", result, arg1, op, arg2);
}
%}

%union {
int i;
char *s;
}

%token <i> NUM


%token <s> ID
%token PLUS MINUS TIMES DIVIDE ASSIGN

%%

program:
statement
;

statement:
ID ASSIGN expression {
printf("%s = ", $1);
generateTAC("=", $3, "", $1);
}
;

expression:
NUM {
$$ = strdup(yytext);
}
|
ID {
$$ = strdup(yytext);
}
|
expression PLUS expression {
char temp[10];
sprintf(temp, "t%d", tempCount++);
generateTAC("+", $1, $3, temp);
$$ = strdup(temp);
}
|
expression MINUS expression {
char temp[10];
sprintf(temp, "t%d", tempCount++);
generateTAC("-", $1, $3, temp);
$$ = strdup(temp);
}
|
expression TIMES expression {
char temp[10];
sprintf(temp, "t%d", tempCount++);
generateTAC("*", $1, $3, temp);
$$ = strdup(temp);
}
|
expression DIVIDE expression {
char temp[10];
sprintf(temp, "t%d", tempCount++);
generateTAC("/", $1, $3, temp);
$$ = strdup(temp);
}
;

%%

int main() {
printf("Enter expressions (Ctrl+D to end input):\n");
yyparse(); // Start parsing
return 0;
}

int yyerror(char *s) {


printf("Error: %s\n", s);
return 0;
}

Step 3: How to Compile and Run

1. Save the Lex program to lexer.l and the Yacc program to parser.y.
2. Compile and generate the code:
3. lex lexer.l
4. yacc -d parser.y
5. gcc lex.yy.c y.tab.c -o tac -ll -ly
6. Run the program:
7. ./tac

Example Input:
a = b + c
x = 5 * a

Example Output:
Enter expressions (Ctrl+D to end input):
a = b + c
t1 = b + c
x = 5 * t1

Explanation:

1. Lex Program (lexer.l):


o Recognizes identifiers (e.g., a, b, c), numbers (e.g., 5), and operators (+, -, *, /, =).
o Each recognized token is returned to the Yacc parser.
2. Yacc Program (parser.y):
o Handles the grammar for assignment and expressions with operators.
o Three-Address Code is generated for each operation.
o Temporary variables are used to store intermediate results (e.g., t1, t2).
3. TAC Generation:
o When an expression is parsed, a temporary variable (e.g., t1, t2) is created, and a TAC
statement is printed (e.g., t1 = b + c).

This is a basic example of generating Three-Address Code using Lex and Yacc. The program handles
simple arithmetic expressions and generates corresponding TAC.

You might also like