Question: / / Function with Arithmetic Expression function main returns integer; begin 7 + 2 * ( 5 + 4 ) ; end; How to I
Function with Arithmetic Expression
function main returns integer;
begin
;
end;
How to I modify the grammar below to parse the arithmetic expression above?
It current throws a syntax error, unexpected IDENTIFIER, expecting RETURNS.
define parse.error verbose
token IDENTIFIER INTLITERAL CHARLITERAL REALLITERAL
token ADDOP MULOP ANDOP RELOP ARROW OROP NOTOP REMOP EXPOP NEGOP
token BEGIN CASE CHARACTER ELSE END ENDSWITCH FUNCTION INTEGER IS LIST OF OTHERS RETURNS SWITCH WHEN ELSIF ENDFOLD ENDIF FOLD IF LEFT REAL RIGHT THEN
function:
functionheader optionalvariable body ;
functionheader:
FUNCTION IDENTIFIER RETURNS type ; ;
type:
INTEGER
CHARACTER ;
optionalvariable:
variable
empty ;
variable:
IDENTIFIER : type IS statement ;
IDENTIFIER : LIST OF type IS list ; ;
list:
expressions ;
expressions:
expressions expression
expression ;
body:
BEGIN statement END ; ;
statement:
statement ;
error ; ;
statement:
expression
WHEN condition expression : expression
SWITCH expression IS cases OTHERS ARROW statement ; ENDSWITCH ;
cases:
cases case
empty ;
case:
CASE INTLITERAL ARROW statement ; ;
condition:
condition ANDOP relation
relation ;
relation:
condition
expression RELOP expression ;
expression:
expression ADDOP term
term ;
term:
term MULOP primary
primary ;
primary:
expression
INTLITERAL
CHARLITERAL
IDENTIFIER expression
IDENTIFIER ;
void yyerrorconst char message
appendErrorSYNTAX message;
int mainint argc, char argv
firstLine;
yyparse;
lastLine;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
