CD LexProgram
CD LexProgram
Here is a short and simplified Lex program to create a lexical analyzer that recognizes identifiers,
keywords, numbers, operators, and whitespace.
%%
[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:
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.
%%
\/\/.* { 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.
Example Run:
Input:
// This is a comment
x = y + 42;
invalid$char
Output:
This program demonstrates how Lex can be used to build a simple lexical analyzer for comments,
operators, and identifiers with minimal effort.
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
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.
Example Run:
Input:
Output:
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.
%%
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.
Example Run:
Input:
Output:
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.
%%
%%
int yywrap() {
return 1;
}
%union {
int i;
char *s;
}
%%
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;
}
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:
This is a basic example of generating Three-Address Code using Lex and Yacc. The program handles
simple arithmetic expressions and generates corresponding TAC.