Question: Scanner : % { #include #include int line _ num = 1 ; / / Track the current line number int char _ pos =

Scanner :
%{
#include
#include
int line_num =1; // Track the current line number
int char_pos =1; // Track the character position on the line
%}
%option noyywrap
DIGIT [0-9]
LETTER [A-Za-z]
SYMBOL [_@\.]
ID {LETTER}({LETTER}|{DIGIT})*({SYMBOL})*({LETTER}|{DIGIT})*
NUM {DIGIT}+|{DIGIT}+"."{DIGIT}+((E|e)(\+|-)?{DIGIT}+)?
%%
{DIGIT}+\.{DIGIT}*[Ee][^0-9]{
fprintf(stderr, "Error: Incomplete token at line %d, position %d
", line_num, char_pos);
char_pos += yyleng;
}
{DIGIT}+\.{DIGIT}*[Ee][\+\-]?[^0-9]{
fprintf(stderr, "Error: Incomplete token at line %d, position %d
", line_num, char_pos);
char_pos += yyleng;
}
{ID}+[!#$%^&*\(\)\-\+\=\[\]\{\}\'\";:>,~]{
fprintf(stderr, "Error: Invalid character in identifier at line %d, position %d
", line_num, char_pos);
char_pos += yyleng;
}
[!#$%^&*\(\)\-\+\=\[\]\{\}\'\";:\.>,~_@]{ID}{
fprintf(stderr, "Error: Symbol at beginning of identifier at line %d, position %d
", line_num, char_pos);
char_pos += yyleng;
}
{ID}{printf("Token: ID, Lexeme: %s at line %d, position %d
", yytext, line_num, char_pos);}
{NUM}{printf("Token: NUM, Lexeme: %s at line %d, position %d
", yytext, line_num, char_pos);}
"/*"([^*]|(\*+[^*/]))*"*/"{char_pos += yyleng;}
"/*"([^*]|(\*+[^*/]))*{fprintf(stderr, "Error: Unclosed comment at line %d, position %d
", line_num, char_pos);exit(1);}
{line_num++;char_pos =1;}
[\t]+{char_pos += yyleng;}
.{fprintf(stderr, "Error: Unknown character '%s' at line %d, position %d
", yytext, line_num, char_pos);}
%%
int main(void){
yylex();
return 0;
}
Scanner : % { #include #include int line _ num =

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!