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
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.
%{ #include /* need this for the call to atoi() and atof() below */ #include %} quote \"[^"]*\" comment ({singleLineComment}|{multiLineComment}) singleLineComment ?????
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); } [ \t ]+ /* eat up white space */ ; . printf("Unrecognized character: %s ", yytext); %% int main() { yylex(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
