Question
A cryptographic calculator performs simple operations on strings of a kind that are used in classical cryptosystems. It is also able to combine these operations
A cryptographic calculator performs simple operations on strings of a kind that are used in
classical cryptosystems. It is also able to combine these operations in a natural way.
Suppose x = x 1 x 2 x n and k = k 1 k 2 k t are two strings, where x i and k i denote the i-th
letters of x and k respectively.
QUESTION: Using the following code as a starting point construct a lex file, called crypt.l and use it
to build a lexical analyser for CRYPT.
/S E
S
E E + E
E E E
E SIMPLESUB(E, STRING)
E VIGENERE(E, STRING)
E LOCTRAN(E, DIGITS)
E STRING
/*** Definition section ***/
%{
/* C code to be copied verbatim */
#include
#include
void yyerror(char *);
#include "y.tab.h";
%}
%%
/*** Rules section ***/
/* yytext is a string containing the matched text. */
/* Strings are all lower case*/
[a-z]+{
printf("Token: STRING;Lexeme: %s ", yytext);
yylval.str = strdup(yytext);
returnSTRING;
}
Reverse{
printf("Token: REVERSE;Lexeme: %s ", yytext);
yylval.str = strdup(yytext);
returnREVERSE;
}
[#()]{
printf("Token and Lexeme: %s ", yytext);
return*yytext;
}
{
printf("Token and Lexeme:
return*yytext;
}
[ \t]{}/*skip whitespace*/
.{
printf("Invalid character. ");
/*
yyerror("invalid character");
*/
}
/*will match any single character that does not match
any of the above patterns
*/
%%
/*** C Code section ***/
void yyerror(char *s) {
fprintf(stderr, "line %d: yytext = %s.Error msg: %s. ", yylineno, yytext, s);
}
int yywrap(void) {
return 1;
}
/*comment out the functionmain()if usinglexwithyacc
int main(void) {
yylex();
return 0;
}
*/
some sample ouput is:
$ ./a.out
Loc(Sim(Vig(therewasmovementatthestation,banjo),thequickbrownfxjmpsvlazydg),10)
Token: LOCTRAN; Lexeme: Loc
Token and Lexeme: (
Token: SIMPLESUB; Lexeme: Sim
Token and Lexeme: (
Token: VIGENERE; Lexeme: Vig
Token and Lexeme: (
Token: STRING; Lexeme: therewasmovementatthestation
Token and Lexeme: ,
Token: STRING; Lexeme: banjo
7Token and Lexeme: )
Token and Lexeme: ,
Token: STRING; Lexeme: thequickbrownfxjmpsvlazydg
Token and Lexeme: )
Token and Lexeme: ,
Token: DIGITS; Lexeme: 10
Token and Lexeme: )
Token and Lexeme:
Control-D
$ ./a.out
V(twentysix - eleven, eleven)
Token: VIGENERE; Lexeme: V
Token and Lexeme: (
Token: STRING; Lexeme: twentysix
Token and Lexeme: -
Token: STRING; Lexeme: eleven
Token and Lexeme: ,
Token: STRING; Lexeme: eleven
Token and Lexeme: )
Token and Lexeme:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started