CMSC 430

profileebote
Project1SkeletonCode.zip

listing.cc

listing.cc

// CMSC 430 Compiler Theory and Design
// Project 1 Skeleton
// UMGC CITE
// Summer 2023

// This file contains the bodies of the functions that produces the 
// compilation listing

#include   < cstdio >
#include   < string >

using   namespace  std ;

#include   "listing.h"

static   int  lineNumber ;
static  string error  =   "" ;
static   int  totalErrors  =   0 ;

static   void  displayErrors ();

void  firstLine ()
{
    lineNumber  =   1 ;
    printf ( "\n%4d  " , lineNumber );
}

void  nextLine ()
{
    displayErrors ();
    lineNumber ++ ;
    printf ( "%4d  " , lineNumber );
}

int  lastLine ()
{
    printf ( "\r" );
    displayErrors ();
    printf ( "     \n" );
     return  totalErrors ;
}
    
void  appendError ( ErrorCategories  errorCategory ,  string message )
{
    string messages []   =   {   "Lexical Error, Invalid Character " ,   "" ,
         "Semantic Error, " ,   "Semantic Error, Duplicate " ,
         "Semantic Error, Undeclared "   };

    error  =  messages [ errorCategory ]   +  message ;
    totalErrors ++ ;
}

void  displayErrors ()
{
     if   ( error  !=   "" )
        printf ( "%s\n" ,  error . c_str ());
    error  =   "" ;
}

listing.h

// CMSC 430 Compiler Theory and Design // Project 1 Skeleton // UMGC CITE // Summer 2023 // This file contains the function prototypes for the functions that produce // the compilation listing enum ErrorCategories {LEXICAL, SYNTAX, GENERAL_SEMANTIC, DUPLICATE_IDENTIFIER, UNDECLARED}; void firstLine(); void nextLine(); int lastLine(); void appendError(ErrorCategories errorCategory, string message);

makefile

compile: scanner.o listing.o g++ -o compile scanner.o listing.o scanner.o: scanner.c listing.h tokens.h g++ -c scanner.c scanner.c: scanner.l flex scanner.l mv lex.yy.c scanner.c listing.o: listing.cc listing.h g++ -c listing.cc

scanner.l

/* CMSC 430 Compiler Theory and Design Project 1 Skeleton UMGC CITE Summer 2023 */ /* This file contains flex input file */ %{ #include <cstdio> #include <string> using namespace std; #include "listing.h" #include "tokens.h" %} %option noyywrap ws [ \t\r]+ comment "//".*\n line [\n] id [A-Za-z]([A-Za-z0-9])* digit [0-9] dec {digit}+ char '.' punc [\(\),:;] %% {ws} { ECHO; } {comment} { ECHO; nextLine(); } {line} { ECHO; nextLine(); } "+" { ECHO; return(ADDOP); } "*" { ECHO; return(MULOP); } "&" { ECHO; return(ANDOP); } "<" { ECHO; return(RELOP); } "=>" { ECHO; return(ARROW); } 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); } {id} { ECHO; return(IDENTIFIER);} {dec} { ECHO; return(INT_LITERAL); } {char} { 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; }

tokens.h

// CMSC 430 Compiler Theory and Design // Project 1 Skeleton // UMGC CITE // Summer 2023 // This file contains the enumerated type definition for tokens enum Tokens {ADDOP = 256, MULOP, ANDOP, RELOP, ARROW, BEGIN_, CASE, CHARACTER, END, ENDSWITCH, FUNCTION, INTEGER, IS, LIST, OF, OTHERS, RETURNS, SWITCH, WHEN, IDENTIFIER, INT_LITERAL, CHAR_LITERAL};