You should be able to use the same function to check a narrowing function return by adding a
similar semantic action on the top level production for the whole function. Be sure that you have
declared that the function_header and body productions carry a type attribute if your receive an
error indicating that they have no declared type. In addition both require semantic actions that
pass the type information up the parse tree.
My issue is that my output says "Type Mismatch on Function Body" when it should say "Illegal
Narrowing Function Return"
heres the test case:
-- Narrowing Function Return
function main returns integer;
b: integer is 6 * 2;
begin
if 8 < 0 then
b + 3.0;
else
b * 4.6;
endif;
end;
heres the expected output:
--Narrowing Function Return
function main returns integer;
b: integer is 6 * 2;
begin
if 8 < 0 then
b + 3.0;
else
b * 4.6;
endif;
end;
Semantic Error, Illegal Narrowing Function Return
Lexical Errors 0
Syntax Errors 0
Semantic Errors 1
Heres the output I'm getting:
-- Narrowing Function Return
function main returns integer;
b: integer is 6 * 2;
begin
if 8 < 0 then
b + 3.0;
else
b * 4.6;
endif;
end;
Semantic Error, Type Mismatch on Function Body
Lexical Errors: 0
Syntax Errors: 0
Semantic Errors 1
heres my parser.y code:
/* Compiler Theory and Design
Duane J. Jarc */
%{
#include
#include
#include
#include
#include
using namespace std;
#include "types.h"
#include "listing.h"
#include "symbols.h"
int yylex();
void yyerror(const char* message);
Symbols symbols;
Types case_return = INT_TYPE;
Types current_function_return = INT_TYPE;
%}
%define parse.error verbose
%union
{
CharPtr iden;
Types type;
}
%token IDENTIFIER
%token INT_LITERAL REAL_LITERAL BOOL_LITERAL CASE ELSE IF ENDIF
%token ADDOP MULOP RELOP ANDOP EXPOP REMOP OROP NOTOP
%token BEGIN_ BOOLEAN END ENDREDUCE FUNCTION INTEGER IS REDUCE
RETURNS
%token ENDCASE OTHERS REAL ARROW THEN WHEN
%type type statement_ statement variable parameter reductions expression binary
relation term factor exponent unary primary case cases
%type function
%type function_header
%type body
%%
function:
function_header optional_variable body ;
function_header:
FUNCTION IDENTIFIER parameters RETURNS type {
current_function_return = $5;
checkAssignment($5, $1, "Function Return"); // Check for illegal narrowing
} ';' |
FUNCTION IDENTIFIER RETURNS type {
current_function_return = $3;
checkAssignment($3, $1, "Function Return"); // Check for illegal narrowing
} ';' |
error ';' {
$$ = MISMATCH;
};
optional_variable:
optional_variable variable | %empty
;
variable:
IDENTIFIER ':' type IS statement_
{checkAssignment($3, $5, "Variable Initialization");
if (symbols.find($1, $3))
appendError(DUPLICATE_IDENTIFIER, $1);
else
symbols.insert($1, $3);} |
error ';' {$$ = MISMATCH;};
parameters:
parameter optional_parameter;
optional_parameter:
optional_parameter ',' parameter | %empty
;
parameter:
IDENTIFIER ':' type {
if(symbols.find($1, $$))
appendError(DUPLICATE_IDENTIFIER, $1);
else
symbols.insert($1, $3);} |.
You should be able to use the same function to check a narrowing fun.pdf
1. You should be able to use the same function to check a narrowing function return by adding a
similar semantic action on the top level production for the whole function. Be sure that you have
declared that the function_header and body productions carry a type attribute if your receive an
error indicating that they have no declared type. In addition both require semantic actions that
pass the type information up the parse tree.
My issue is that my output says "Type Mismatch on Function Body" when it should say "Illegal
Narrowing Function Return"
heres the test case:
-- Narrowing Function Return
function main returns integer;
b: integer is 6 * 2;
begin
if 8 < 0 then
b + 3.0;
else
b * 4.6;
endif;
end;
heres the expected output:
--Narrowing Function Return
function main returns integer;
b: integer is 6 * 2;
begin
if 8 < 0 then
b + 3.0;
else
b * 4.6;
endif;
end;
Semantic Error, Illegal Narrowing Function Return
Lexical Errors 0
Syntax Errors 0
Semantic Errors 1
Heres the output I'm getting:
-- Narrowing Function Return
2. function main returns integer;
b: integer is 6 * 2;
begin
if 8 < 0 then
b + 3.0;
else
b * 4.6;
endif;
end;
Semantic Error, Type Mismatch on Function Body
Lexical Errors: 0
Syntax Errors: 0
Semantic Errors 1
heres my parser.y code:
/* Compiler Theory and Design
Duane J. Jarc */
%{
#include
#include
#include
#include
#include
using namespace std;
#include "types.h"
#include "listing.h"
#include "symbols.h"
int yylex();
void yyerror(const char* message);
Symbols symbols;
Types case_return = INT_TYPE;
Types current_function_return = INT_TYPE;
%}
%define parse.error verbose
3. %union
{
CharPtr iden;
Types type;
}
%token IDENTIFIER
%token INT_LITERAL REAL_LITERAL BOOL_LITERAL CASE ELSE IF ENDIF
%token ADDOP MULOP RELOP ANDOP EXPOP REMOP OROP NOTOP
%token BEGIN_ BOOLEAN END ENDREDUCE FUNCTION INTEGER IS REDUCE
RETURNS
%token ENDCASE OTHERS REAL ARROW THEN WHEN
%type type statement_ statement variable parameter reductions expression binary
relation term factor exponent unary primary case cases
%type function
%type function_header
%type body
%%
function:
function_header optional_variable body ;
function_header:
FUNCTION IDENTIFIER parameters RETURNS type {
current_function_return = $5;
checkAssignment($5, $1, "Function Return"); // Check for illegal narrowing
} ';' |
FUNCTION IDENTIFIER RETURNS type {
current_function_return = $3;
checkAssignment($3, $1, "Function Return"); // Check for illegal narrowing
} ';' |
error ';' {
$$ = MISMATCH;
};
optional_variable:
optional_variable variable | %empty
;
variable:
4. IDENTIFIER ':' type IS statement_
{checkAssignment($3, $5, "Variable Initialization");
if (symbols.find($1, $3))
appendError(DUPLICATE_IDENTIFIER, $1);
else
symbols.insert($1, $3);} |
error ';' {$$ = MISMATCH;};
parameters:
parameter optional_parameter;
optional_parameter:
optional_parameter ',' parameter | %empty
;
parameter:
IDENTIFIER ':' type {
if(symbols.find($1, $$))
appendError(DUPLICATE_IDENTIFIER, $1);
else
symbols.insert($1, $3);} |
error ';' {$$ = MISMATCH;};
type:
INTEGER {$$ = INT_TYPE;} |
BOOLEAN {$$ = BOOL_TYPE;} |
REAL {$$ = REAL_TYPE;} ;
body:
BEGIN_ statement_ END ';' {
// Check for illegal narrowing in the function body
checkAssignment(current_function_return, $2, "Function Return");
};
statement_:
statement ';' |
error ';' {$$ = MISMATCH;} ;
statement:
expression |
IF expression THEN statement_ ELSE statement_ ENDIF {$$ = checkIfThen($2, $4, $6);} |
CASE expression IS cases OTHERS ARROW statement_ ENDCASE {$$ =