Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I really need help with number 13. Below is my scanner.l file and my tokens.h file, it will not compile the way it needs to.

imageimage


I really need help with number 13. Below is my scanner.l file and my tokens.h file, it will not compile the way it needs to. I need to know how to format it in my scanner.l file under char_literals.

 

scanner.l

%{
#include
#include

using namespace std;

#include "listing.h"
#include "tokens.h"

%}

%option noyywrap

ws              [ \t\r]+
comment         \-\-.*\n
secondcomment   "--[^\n]*\n"
line            [\n]
id              [A-Za-z](_?[A-Za-z0-9])*
digit           [0-9]
hex             #{digit}+|[0-9a-fA-F]+
int             {digit}+
char_literal    '\\'(b|t|n|r|f|\\\\|\\'.)*'\\'
punc            [\(\),:;]

real            {digit}+(\.{digit}+)?([eE][+-]?{digit}+)?


%%

{ws}            { ECHO; }
{comment}       { ECHO; nextLine(); }
{secondcomment} { ECHO; nextLine(); }
{line}          { ECHO; nextLine(); }

"\|"            { ECHO; return(OROP); }
"!"             { ECHO; return(NOTOP); }
"-"             { ECHO; return(ADDOP); }
"/"             { ECHO; return(MULOP); }
"=>"            { ECHO; return(ARROW); }
"="             { ECHO; return(RELOP); }
"<>|>=|<="      { ECHO; return(RELOP); }
">"             { ECHO; return(RELOP); }
">="            { ECHO; return(RELOP); }
"<="            { ECHO; return(RELOP); }
"^"             { ECHO; return(EXPOP); }
"~"             { ECHO; return(NEGOP); }
"%"             { ECHO; return(REMOP); }
"+"             { ECHO; return(ADDOP); }
"*"             { ECHO; return(MULOP); }
"&"             { ECHO; return(ANDOP); }
"<"             { ECHO; return(RELOP); }
begin           { ECHO; return(BEGIN_); }
case            { ECHO; return(CASE); }
character       { ECHO; return(CHARACTER); }
end             { ECHO; return(END); }
endswitch       { ECHO; return(ENDSWITCH); }
function        { ECHO; return(FUNCTION); }
integer         { ECHO; return(INTEGER); }
is              { ECHO; return(IS); }
list            { ECHO; return(LIST); }
of              { ECHO; return(OF); }
others          { ECHO; return(OTHERS); }
returns         { ECHO; return(RETURNS); }
switch          { ECHO; return(SWITCH); }
when            { ECHO; return(WHEN); }
else            { ECHO; return(ELSE); }
elsif           { ECHO; return(ELSIF); }
endfold         { ECHO; return(ENDFOLD); }
endif           { ECHO; return(ENDIF); }
fold            { ECHO; return(FOLD); }
if              { ECHO; return(IF); }
left            { ECHO; return(LEFT); }
real            { ECHO; return(REAL); }
right           { ECHO; return(RIGHT); }
then            { ECHO; return(THEN); }

{id}            { ECHO; return(IDENTIFIER); }
{hex}   { ECHO; return(INT_LITERAL); }
{real}          { ECHO; return(REAL_LITERAL); }
{char_literal}          { ECHO; return(CHAR_LITERAL); }
{punc}          { ECHO; return(yytext[0]); }
.               { ECHO; appendError(LEXICAL, yytext); }
%%

int main()
{
   firstLine();
   
   FILE *file = fopen("lexemes.txt", "wa");
   int token = yylex();    
   while (token)
   {
       fprintf(file, "%d %s\n", token, yytext);
       token = yylex();
   }
   lastLine();
   fclose(file);
   return 0;
}

token.h

 

#ifndef TOKENS_H

#define TOKENS_H


 

enum Tokens {

    ADDOP = 256,

    MULOP,

    ANDOP,

    RELOP,

    ARROW,

    BEGIN_,

    CASE,

    CHARACTER,

    END,

    ENDFOLD,  // Added for 'endfold'

    ENDIF,    // Added for 'endif'

    ELSE,     // Added for 'else'

    ELSIF,    // Added for 'elsif'

    FOLD,     // Added for 'fold'

    IF,       // Added for 'if'

    LEFT,

    REAL,

    RIGHT,

    THEN,     //Added for 'then'

    OROP,     //Added for '|'

    NOTOP,    //Added for '!'

    REMOP,    //Added for '%'

    EXPOP,    //Added for '^'

    NEGOP,    //Added for '~'

    IDENTIFIER,

    INT_LITERAL,

    HEX_INT,       //Added for hexadecimal integers

    REAL_LITERAL,  //Added for real literals

    CHAR_LITERAL,  //Added for char literals

    ENDSWITCH,

    FUNCTION,

    INTEGER,

    IS,

    LIST,

    OF,

    OTHERS,

    RETURNS,

    SWITCH,

    WHEN

};


 

#endif

CMSC 430 Project 1 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.1: 1. The following reserved words should be added: else, elsif, endfold, endif, fold, if, left, real, right, then Each reserved words should be a separate token. The token name should be the same as the lexeme, but in all upper case. 2. Two additional logical operators should be added. The lexeme for the first should be and its token should be CROP. The second logical operator added should be and its token should be NOTOP. 3. Five relational operators should be added. They are =, , >,>= and

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

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Algorithms questions