Question

Use Lex to produce a lexical analyzer. You have to include all tests, make files, etc.

Use Lex to produce a lexical analyzer. You have to include all tests, make files, etc.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* Tokens. */( better make a file and include that file in program)

#define IDENTIFIER 258

#define NUMBER 259

#define SIZEOF 260

#define PTR_OP 261

#define GT_OP 262

#define LT_OP 263

#define GE_OP 264

#define LE_OP 265

#define NE_OP 266

#define EQ_OP 267

#define AND_OP 268

#define OR_OP 269

#define DOT_OP 270

#define TYPEDEF 271

#define INT 272

#define CHAR 273

#define VOID 274

#define STRUCT 275

#define IF 276

#define ELSE 277

#define WHILE 278

#define RETURN 279

Normally, in a compiler, the parser frequently signals and calls the lexer, to returns the next token to the parser.

int main(){

int tok;

int n;

n = 0;

while(1) {

tok = yylex();

if(tok == 0)

break;

printf("line:%2d token type:%3d token text:(%s)\n", yylineno, tok, yytext);

n++;

}

printf("Number of tokens matched is %d\n",n);

return 0;

}

Generate the lexer using flex:

flex lex1.l

The result is in a file with name lex.yy.c. Compile and link:

gcc -c lex.yy.c

gcc lex.yy.o -ll

Sample Input and Output File: Below is a sample input file

/* comment */

void main(){

int i;

i=0;

while(i<10)

i=i+1;

}

and the corresponding output file

line: 2 token type:275 token text:(void)

line: 2 token type:258 token text:(main)

line: 2 token type: 40 token text:(()

line: 2 token type: 41 token text:())

line: 2 token type:123 token text:({)

line: 3 token type:273 token text:(int)

line: 3 token type:258 token text:(i)

line: 3 token type: 59 token text:(;)

line: 4 token type:258 token text:(i)

line: 4 token type: 61 token text:(=)

line: 4 token type:259 token text:(0)

line: 4 token type: 59 token text:(;)

line: 5 token type:279 token text:(while)

line: 5 token type: 40 token text:(()

line: 5 token type:258 token text:(i)

line: 5 token type:264 token text:(<)

line: 5 token type:259 token text:(10)

line: 5 token type: 41 token text:())

line: 6 token type:258 token text:(i)

line: 6 token type: 61 token text:(=)

line: 6 token type:258 token text:(i)

line: 6 token type: 43 token text:(+)

line: 6 token type:259 token text:(1)

line: 6 token type: 59 token text:(;)

line: 7 token type:125 token text:(})

Number of tokens matched is 25

Add a comment
Know the answer?
Add Answer to:
Use Lex to produce a lexical analyzer. You have to include all tests, make files, etc.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The first project involves modifying the attached lexical analyzer and the compilation listing ge...

    The first project involves modifying the attached lexical analyzer and the compilation listing generator code. You need to make the following modifications to the lexical analyzer, scanner.l: 1. A second type of comment should be added that begins with // and ends with the end of line. As with the existing comment, no token should be returned. 2. The definition for the identifiers should be modified so that underscores can be included, however, consecutive underscores, leading and trailing underscores should...

  • 7. Lexical analyzer, is the first phase in compiler, Of what use is this to the...

    7. Lexical analyzer, is the first phase in compiler, Of what use is this to the whole compilation and 14 marks) what are the formal concepts involved ? 8. Describe this language using a Regular Expression: all strings beginning with a digit, followed 13 marks] by either a or b, and ending with an non-vowel uppercase letter 9. Write a context free granimar for the strings that consist of equal number of a's followed by equal number of b's. Attempt...

  • For this homework, you will use multiple source files. The file descriptions are below. Make sure...

    For this homework, you will use multiple source files. The file descriptions are below. Make sure to use the correct include statements in all files. Submit only the .c and .h files. multiple.h - This file will include the function prototypes for two functions. Include the prototypes within an include guard. void printUpTriangle(int x); void printDownTriangle(int x); main.c - This file will include the main function. In main, prompt the user for a positive integer and then call both functions...

  • C++ HTML files use tags enclosed in angle brackets to denote formatting instructions. For ex- ample,...

    C++ HTML files use tags enclosed in angle brackets to denote formatting instructions. For ex- ample, indicates bold, indicates italics, etc. If a web browser is displaying an HTML document that contains ‘<’ or ‘>’ then it may mistake these symbols for tags. This is a common problem with C++ files, which contain many <’s and >’s. For example, the line “#include ” may result in the browser interpreting as a tag. To avoid this problem, HTML uses special symbols...

  • Make File you should create a Makefile containing targets for all the exercises; by default, the...

    Make File you should create a Makefile containing targets for all the exercises; by default, the Makefile should compile all the files, and the output of each of them should be an executable file named i. Further, when the make is called with a number, your Makefile should compile only that exercise. Examples: •make 1 compiles only 1.c into the executable file 1 •make compiles all C files •make clean removes all generated executable files OBS( I Have different programs...

  • Question 2 In this question, you will read two data files that include integers into two...

    Question 2 In this question, you will read two data files that include integers into two different arrays – the same way we did in class (but we are doing to arrays here). Duplicates are ok. 1- After you read the data into the array (use one function that takes an int array and a dsize by reference just like we did in class, and call that from main to fill both arrays). 2- Include a printArray function so that...

  • a) How would you use the ls command to display all of the files/directories in the...

    a) How would you use the ls command to display all of the files/directories in the directory ‘/sbin’ that start with ‘bl’? b) How would you use the ls command to display all of the files/directories in your current directory that contain the word ‘grade’ somewhere in the name and end with ‘.pdf’? c) How would you use the ls command to display all of the files/directories in your current directory that are any two characters followed by the file...

  • I have a the data from hundreds of fMRI patients. All of the files have the...

    I have a the data from hundreds of fMRI patients. All of the files have the same ending (...abc.nii.gz for example). I need to make a .txt file that contains the pathways to every one of these files, but I don't want to copy hundreds of pathways by hand. Is there a way to search for each of these files ending in ...abc.nii.gz and copy their pathways into a new txt file in linux?

  • Which signature peaks make up the compound meso-hydrobenzoin? Please include all signature peaks including aromatics, etc....

    Which signature peaks make up the compound meso-hydrobenzoin? Please include all signature peaks including aromatics, etc. meso-Hydrobenzoin INFRARED SPECTRUM کی 0.96 Relative Transmittance 0.92 0.88 4000 3000 1000 2000 Wavenumber (cm-1)

  • do numbers 4-8 4. Given any directory, use the Is command to display: • all files...

    do numbers 4-8 4. Given any directory, use the Is command to display: • all files and sub-directories starting with the letter "D" (note do not list anything in any sub-directory) • its immediate sub-directories (sub-directories only, and no other ordinary files) its immediate hidden sub-directories only - take a screenshot (#3-3) that clearly shows the command and the result. 5. Assume that the following files are in the working directory: $ ls intro notesb ref2 section 1 section3 section4b...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT