In lexical analysis, the input file is broken into individual words or symbols.
ID: 3542717 • Letter: I
Question
In lexical analysis, the input file is broken into individual words or symbols. These words or symbols are
then represented as tokens and passed to the parser. Your lexical analyzer needs to read ASCII characters
from standard input and do the following:
1. discard white space (space, tab, newline, carriage-return, and form-feed characters),
2. discard comments (everything from a semicolon to the end of the line),
3. recognize quotes, dots, and open and closing parentheses,
4. recognize the boolean constants #t and #f,
5. recognize integer constants (for simplicity only decimal digits without a sign),
6. recognize string constants (anything between double quotes),
7. recognize identifiers.
Explanation / Answer
%option interactive
%{
extern "C" int yylex();
#include<stdio.h>
#include<math.h>
%}
%%
"(" { printf("Got a symbol ");
return yytext[0]; }
")" { printf("Got a symbol ");
return yytext[0]; }
"{" { printf("Got a symbol ");
return yytext[0]; }
"}" { printf("Got a symbol ");
return yytext[0]; }
"[" { printf("Got a symbol ");
return yytext[0]; }
"]" { printf("Got a symbol ");
return yytext[0]; }
"." { printf("Got a symbol ");
return yytext[0]; }
"/" { printf("Got a symbol ");
return yytext[0]; }
[0-9]+ { printf("Got %s ",yytext);
yylval.ival = atoi(yytext); return NUMBER; }
[A-Za-z][A-Za-z0-9]* { yylval.id=new Idt(yytext,0);
printf(" Got an ID..!! "); return ID;}
[ ] ;
. return yytext[0];
%%
int yywrap()
{return 1;}
int main(){
yylex();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.