Compiler | Computer Science homework help

Home work for modifying the syntactic analyzer for the attached compiler by adding to the existing grammar. The full grammar of the language is shown below. The highlighted portions of the grammar show what you must either modify or add to the existing grammar.

function:

function_header {variable} body

function_header:

FUNCTION IDENTIFIER [parameters] RETURNS type ;

variable:

IDENTIFIER : type IS statement

parameters:

parameter {, parameter}

parameter:

IDENTIFIER : type

type:

INTEGER | REAL | BOOLEAN

body:

BEGIN statement END ;

statement: expression ; |

REDUCE operator {statement} ENDREDUCE ; |

IF expression THEN statement ELSE statement ENDIF ; |

CASE expression IS {case} OTHERS ARROW statement ; ENDCASE ;

operator:

ADDOP | MULOP

case:

WHEN INT_LITERAL ARROW statement

expression:

( expression ) |

REAL_LITERAL

NOT expression

expression binary_operator expression |

|

INT_LITERAL | IDENTIFIER

| BOOL_LITERAL |

binary_operator: ADDOP | MULOP | REMOP | EXPOP | RELOP | ANDOP | OROP

In the above grammar, the red symbols are nonterminals, the blue symbols are terminals and the black punctuation are EBNF metasymbols. The braces denote repetition 0 or more times and the brackets denote optional.

You must rewrite the grammar to eliminate the EBNF brace and bracket metasymbols and to incorporate the significance of parentheses, operator precedence and associativity for all operators. Among arithmetic operators the exponentiation operator has highest precedence following by the multiplying operators and then the adding operators. All relational operators have the same precedence. Among the binary logical operators, and has higher precedence than or. Of the categories of operators, the unary logical operator has highest precedence, the arithmetic operators have next highest precedence, followed by the relational operators and finally the binary logical operators. All operators except the exponentiation operator are left associative. The directives to specify precedence and associativity, such as %prec and %left, may not be used

Your parser should be able to correctly parse any syntactically correct program without any problem.

You must modify the syntactic analyzer to detect and recover from additional syntax errors using the semicolon as the synchronization token. To accomplish detecting additional errors an error production must be added to the function header and another to the variable declaration.

Your bison input file should not produce any shift/reduce or reduce/reduce errors. Eliminating them can be difficult so the best strategy is not introduce any. That is best achieved by making small incremental additions to the grammar and ensuring that no addition introduces any such errors.

An example of compilation listing output containing syntax errors is shown below:

1 — Multiple errors 2

  1. function main a integer returns real; Syntax Error, Unexpected INTEGER, expecting ‘:’
  2. b: integer is * 2; Syntax Error, Unexpected MULOP
  3. c: real is 6.0;
  4. begin
  5. if a > c then 8      b   3.0;

Syntax Error, Unexpected REAL_LITERAL, expecting ‘;’

9      else

10          b = 4.;

11      endif;

12 ;

Syntax Error, Unexpected ‘;’, expecting END

Lexical Errors 0

Syntax Errors 4

Semantic Errors 0

————————————————————

listing.h

// 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 parser.o listing.o
  g++ -o compile scanner.o parser.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

parser.o: parser.c listing.h
  g++ -c parser.c

parser.c tokens.h: parser.y
  bison -d -v parser.y
  mv parser.tab.c parser.c
  mv parser.tab.h tokens.h

listing.o: listing.cc listing.h
  g++ -c listing.cc

———————————————————-

parser.y

%{

#include 

using namespace std;

#include “listing.h”

int yylex();
void yyerror(const char* message);

%}

%error-verbose

%token IDENTIFIER
%token INT_LITERAL

%token ADDOP MULOP RELOP ANDOP

%token BEGIN_ BOOLEAN END ENDREDUCE FUNCTION INTEGER IS REDUCE RETURNS

%%

function:  
  function_header optional_variable body ;
 

function_header:  
  FUNCTION IDENTIFIER RETURNS type ‘;’ ;

optional_variable:
  variable |
  ;

variable:
  IDENTIFIER ‘:’ type IS statement_ ;

type:
  INTEGER |
  BOOLEAN ;

body:
  BEGIN_ statement_ END ‘;’ ;
 

statement_:
  statement ‘;’ |
  error ‘;’ ;
 

statement:
  expression |
  REDUCE operator reductions ENDREDUCE ;

operator:
  ADDOP |
  MULOP ;

reductions:
  reductions statement_ |
  ;
 

expression:
  expression ANDOP relation |
  relation ;

relation:
  relation RELOP term |
  term;

term:
  term ADDOP factor |
  factor ;
 

factor:
  factor MULOP primary |
  primary ;

primary:
  ‘(‘ expression ‘)’ |
  INT_LITERAL |
  IDENTIFIER ;
 

%%

void yyerror(const char* message)
{
  appendError(SYNTAX, message);
}

int main(int argc, char *argv[])
{
  firstLine();
  yyparse();
  lastLine();
  return 0;
}
————————————————————————

scanner

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

/* This file contains flex input file */

%{
#include
#include 

using namespace std;

#include “listing.h”
#include “tokens.h”

%}

%option noyywrap

ws       [ tr]+
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); }

%%
—————————————————————-

listing.cc

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

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

#include
#include 

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(“%sn”, error.c_str());
  error = “”;
}
———————————————————-

Calculate your paper price
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.