COMPILER, Computer science

profilehu.ke2013
Project1SkeletonCode.zip

listing.cc

listing.cc

// Compiler Theory and Design
// Dr. Duane J. Jarc

// 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 Identifier: " ,
         "Semantic Error, Undeclared "   };

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

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

listing.h

// Compiler Theory and Design // Dr. Duane J. Jarc // 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

/* Compiler Theory and Design Dr. Duane J. Jarc */ /* 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] int {digit}+ punc [\(\),:;] %% {ws} { ECHO; } {comment} { ECHO; nextLine();} {line} { ECHO; nextLine();} "<" { ECHO; return(RELOP); } "+" { ECHO; return(ADDOP); } "*" { ECHO; return(MULOP); } begin { ECHO; return(BEGIN_); } boolean { ECHO; return(BOOLEAN); } end { ECHO; return(END); } endreduce { ECHO; return(ENDREDUCE); } function { ECHO; return(FUNCTION); } integer { ECHO; return(INTEGER); } is { ECHO; return(IS); } reduce { ECHO; return REDUCE; } returns { ECHO; return(RETURNS); } and { ECHO; return(ANDOP); } {id} { ECHO; return(IDENTIFIER);} {int} { ECHO; return(INT_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

// Compiler Theory and Design // Dr. Duane J. Jarc // This file contains the enumerated type definition for tokens enum Tokens {RELOP = 256, ADDOP, MULOP, ANDOP, BEGIN_, BOOLEAN, END, ENDREDUCE, FUNCTION, INTEGER, IS, REDUCE, RETURNS, IDENTIFIER, INT_LITERAL};