Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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:
functionheader variable body
functionheader:
FUNCTION IDENTIFIER parameters RETURNS type ;
variable:
IDENTIFIER : type IS statement ;
IDENTIFIER : LIST OF type IS list ;
list:
expression expression
parameters:
parameter parameter
parameter:
IDENTIFIER : type
type:
INTEGER REAL CHARACTER
body:
BEGIN statement END ;
statement:
expression ;
WHEN condition expression : expression ;
SWITCH expression IS case OTHERS ARROW statement ENDSWITCH ;
IF condition THEN statement ELSIF condition THEN statement
ELSE statement ENDIF ;
FOLD direction operator listchoice ENDFOLD ;
case:
CASE INTLITERAL ARROW statement
direction:
LEFT RIGHT
operator:
ADDOP MULOP
listchoice:
list
IDENTIFIER
condition:
expression RELOP expression condition logicaloperator condition
condition
NOTOP condition
logicaloperator:
ANDOP OROP
expression:
expression
expression arithmeticoperator expression
NEGOP expression
INTLITERAL REALLITERAL CHARLITERAL
IDENTIFIER expression
IDENTIFIER
arithmeticoperator: ADDOP MULOP MODOP EXPOP
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 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. The precedence and associativity rules are as follows:
Among binary arithmetic operators the exponentiation operator has highest precedence
followed by the multiplying operators and then the adding operators. But the unary
negation operator ~ has higher precedence that all of the binary operators.
All relational operators have the same precedence.
Among the binary logical operators, & has higher precedence than But the unary logical
operator has higher precedence than either of the binary logical operators.
All binary 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 body, to the variable declaration and to the when
clause.
Your bison input file should not produce any shiftreduce or reducereduce 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: Multiple errors
function main a integer returns real;
Syntax Error, Unexpected INTEGER, expecting :
b: integer is ;
Syntax Error, Unexpected MULOP
c: real is ;
begin
if a c then
b ;
Syntax Error, Unexpected MULOP
else
case b is
when ;
Syntax Error, Unexpected ARROW, expecting INTLITERAL
when c;
endcase;
Syntax Error, Unexpected ENDCASE, expecting OTHERS or WHEN
endif;
end;
Lexical Errors
Syntax Errors
Semantic Errors
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started