• Home
  • Blog
  • CMSC430 compiler theory and design, project2

CMSC430 compiler theory and design, project2

0 comments

The second project involves 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 ) |

expression binary_operator expression |

NOT expression |

INT_LITERAL | REAL_LITERAL | BOOL_LITERAL |

IDENTIFIER

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

3 function main a integer returns real;

Syntax Error, Unexpected INTEGER, expecting ‘:’

4 b: integer is * 2;

Syntax Error, Unexpected MULOP

5 c: real is 6.0;

6 begin

7 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

You are to submit two files.

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}