Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your job is to take the partially implemented emcee.l and complete it. Put all your files into a single directory (like ~/projects/emcee maybe?). From the

Your job is to take the partially implemented emcee.l and complete it. Put all your files into a single directory (like ~/projects/emcee maybe?). From the command line, change into this directory and execute the following steps to test your program:

1. flex emcee.l

This will invoke the flex tool to process your token definitions and produce a file called lex.yy.c. You can take a look at this file though it may not make much sense. It is written in C and was not meant for humans to look at.

Once you have removed all the errors, you can go on to the next step.

2. gcc lex.yy.c emcee.c -o emcee

This will compile your new Scanner and the driver program. If all goes well it will produce an executable file called emcee.

Once you have removed all the errors, you can go on to the next step.

3. ./emcee < sqrt.mc > tokens.out

This will run your compiler using the input file sqrt.mc and direct the output to the file tokens.out. The file sqrt.mc is provided on Canvas. The contents of this file should look like the sample output below. Test your program with other source files as well. You should make sure each of your tokens is picked up correctly. At this point, syntax doesnt matter.

4. cat tokens.out

This will print out the file your computer created, tokens.out

Notes

You must use flex. Dont write your own Scanner.

Turn in only your completed emcee.l file.

Required Input Lines of emcee code. 4. Required Output Your output should look something like the following example. Emcee Compiler ver. 0.1 1 9 (guess) 13 3 12 1 9 (n) 13 3 12 9 (guess) 28 8 (1.0) 12 9 (n) 28 9 (inputReal) 14 15 12 5 29 9 (n) 27 8 (0.0) 30 6 31 7 29 9 (abs) 14 9 (guess) 20 10 (2) 17 9 (n) 15 25 8 (0.01) 30 31 9 (guess) 28 14 9 (n) 16 14 9 (n) 19 9 (guess) 15 15 19 10 (2) 12 32 12 9 (print) 14 11 ("Root is ") 33 9 (guess) 15 12 32 12

Given code: emcee.l

%option noyywrap

A [a-zA-Z]

/* 1. include other pattern definitions here, if needed. Digits maybe? */

%{

#include "tokens.h" /* Leave this section untouched */

%}

%%

def return (DEF);

/* 2. Add other keywords here AND DELETE THIS COMMENT! */

{A}+ return (ID);

/* 3. Add other pattern words here AND DELETE THIS COMMENT! */

";" return (SEMI);

/* 4. Add other punctuation here AND DELETE THIS COMMENT! */

/* 5. Remember to recognize and ignore unknown punctuation, new-lines and comments AND DELETE THIS COMMENT! */

%%

void yyerror () /* Leave this section untouched */

{

printf (" error ");

exit(0);

}

Given code: emcee.c

#include

#include "tokens.h"

int yylex (); // scanner prototype

extern char* yytext;

main ()

{ int n;

printf("Emcee Compiler ver. 0.1 ");

while (n = yylex()) { // call scanner until it returns 0 for EOF

switch (n) {

case REAL_CONST:

case ID:

case INT_CONST:

case STRING_CONST: printf (" %d (%s)", n, yytext); break;

default: printf (" %d", n); break;

}

}

printf(" ");

}

Given code: sqrt.mc

# First program written in Emcee

def guess : real; def n : real;

# Set the input variables guess := 1.0; n := inputReal();

if [ n >= 0.0 ] then {

# Continue until the root has been found

while [ abs(guess^2 - n) > 0.01 ] { guess := (n + (n / guess)) / 2; };

# Print the result print("Root is ", guess); };

Given code: tokens.h

#define DEF 1

#define INTEGER 2

#define REAL 3

#define STRING 4

#define IF 5

#define THEN 6

#define WHILE 7

#define REAL_CONST 8

#define ID 9

#define INT_CONST 10

#define STRING_CONST 11

#define SEMI 12

#define COLON 13

#define LEFT_PAREN 14

#define RIGHT_PAREN 15

#define PLUS 16

#define MINUS 17

#define MULT 18

#define DIVIDE 19

#define CARAT 20

#define MOD 21

#define EQUAL 22

#define NOT_EQUAL 23

#define LESS_THAN 24

#define GREATER_THAN 25

#define LESS_EQUAL 26

#define GREATER_EQUAL 27

#define ASSIGN 28

#define LEFT_SQUARE 29

#define RIGHT_SQUARE 30

#define LEFT_BRACE 31

#define RIGHT_BRACE 32

#define COMMA 33

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions