Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

A template for the LEX program is given below(lexer.l). 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, 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.

The commands to run your program are:

flex lab3lexer.l gcc lex.yy.c -lm -lfl ./a.out < test.c 

Students need to define regular expressions for ID, int, float.

Submit your flex program together with your test cases.

Note that in our project target language C--, the data types considered are int and float. We have not defined the concept of strings. Also, printf statement is not defined.

lexer.l:

%{ #include  /* need this for the call to atoi() and atof() below */ #include  %} quote \"[^"]*\" comment ({singleLineComment}|{multiLineComment}) %% {int} { printf("An integer: %s (%d) ", yytext, atoi(yytext)); } {float} { printf("A float: %s (%g) ", yytext, atof(yytext)); } void|break|continue|else|float|if|int|return|while { printf("A keyword: %s ", yytext); } {ID} { printf("An identifier: %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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions