We want to write a LEX program for a scanner for the project target language C--
ID: 3747300 • Letter: W
Question
We want to write a LEX program for a scanner for the project target language C--, which specification can be found in the project resources page.
Your job is to complete the definitions section of the LEX program. The rest of the program has been completed for you.
NOTE: for the specification of a comment token, we follow the definition given in part (c) of Exercise 3.3.5 in page 125 of the textbook. That is, a comment is a string surrounded by /* and */, without an intervening */, unless it is inside double-quotes ("). This definition of comment is more general than the one given in the C-- language specification. In this homework, we call this definition of a comment the multi-line comment. Actually, we have already written the regular expression for the multi-line comment token for you.
We also allow single-line comment, which according to https://en.wikibooks.org/wiki/C%2B%2B_Programming/Code/Style_Conventions/Comments is defined as follows:
Single-line comments (informally, C++ style), start with // and continue until [and include] the end of the line. If the last [non-space non-tab] character in a comment line is a the comment will continue in the next line.
Note that we adopt the convention that a single-line comment includes the end of line symbol as well.
To summarize, a comment token is either a single-line comment or a multi-line comment.
you need to define regular expressions for singleLineComment and multiLineComment.
%% void|break|continue|else|float|if|int|return|while { printf("A keyword: %s ", yytext); } "+"|"-"|"*"|"/"|"%"|"!"|"?"|":"|"="|","|"<"|">"|";"|"("|")"|"{"|"}" { printf("operator/bracket/other char: %s ", yytext); } "||" printf("Disjunction operator: %s ", yytext); "&&" printf("Conjunction operator: %s ", yytext); "==" printf("Equality operator: %s ", yytext); {comment} { printf("Comment: %s ", yytext); } [ ]+ /* eat up white space */ ; . printf("Unrecognized character: %s ", yytext); %% int main() { yylex(); }
Explanation / Answer
Comment lines are of the form // or /**/. In order to identigy the following regular expressions can be used:
Here is an example to check and remove the comment lines:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.