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
7+2*(5+4);
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 INT_LITERAL CHAR_LITERAL REAL_LITERAL
%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:
function_header optional_variable body ;
function_header:
FUNCTION IDENTIFIER RETURNS type ';' ;
type:
INTEGER |
CHARACTER ;
optional_variable:
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 INT_LITERAL ARROW statement ';' ;
condition:
condition ANDOP relation |
relation ;
relation:
'(' condition ')'|
expression RELOP expression ;
expression:
expression ADDOP term |
term ;
term:
term MULOP primary |
primary ;
primary:
'(' expression ')'|
INT_LITERAL |
CHAR_LITERAL |
IDENTIFIER '(' expression ')'|
IDENTIFIER ;
%%
void yyerror(const char* message){
appendError(SYNTAX, message);
}
int main(int argc, char *argv[]){
firstLine();
yyparse();
lastLine();
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!