SlideShare a Scribd company logo
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);} |
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 {$$ =
checkExpression($2); $$ = checkReturns(case_return, $7);}; |
REDUCE operator reductions ENDREDUCE {$$ = $3;} ;
cases:
cases case {case_return = checkCases($2, case_return);}|
%empty {$$ = NAN_TYPE;};
case:
WHEN INT_LITERAL ARROW statement_ {$$ = $4;} ;
reductions:
reductions statement_ {$$ = checkArithmetic($1, $2);} |
{$$ = INT_TYPE;} %empty;
operator:
ADDOP |
RELOP |
EXPOP |
MULOP ;
expression:
expression OROP binary {$$ = checkLogical($1, $3);} |
binary ;
binary:
binary ANDOP relation {$$ = checkLogical($1, $3);} |
relation ;
relation:
relation RELOP term {$$ = checkRelational($1, $3);}|
term ;
term:
term ADDOP factor {$$ = checkArithmetic($1, $3);} |
factor ;
factor:
factor MULOP primary {$$ = checkArithmetic($1, $3);} |
factor REMOP exponent {$$ = checkRemainder($1, $3);} |
exponent ;
exponent:
unary |
unary EXPOP primary {$$ = checkArithmetic($1, $3);} ;
unary:
NOTOP primary {$$ = checkNegation($2);} |
primary;
primary:
'(' expression ')' {$$ = $2;} |
INT_LITERAL | BOOL_LITERAL | REAL_LITERAL |
IDENTIFIER {if (!symbols.find($1, $$)) appendError(UNDECLARED, $1);} ;
%%
void yyerror(const char* message)
{
appendError(SYNTAX, message);
}
int main(int argc, char *argv[])
{
firstLine();
yyparse();
lastLine();
return 0;
}
heres my types.cc code:
// Compiler Theory and Design
// Duane J. Jarc
// This file contains the bodies of the type checking functions
#include
#include
using namespace std;
#include "types.h"
#include "listing.h"
void checkAssignment(Types lValue, Types rValue, string message)
{
if(rValue == MISMATCH)
return;
if(lValue == BOOL_TYPE && rValue != BOOL_TYPE || lValue!=BOOL_TYPE && rValue
==BOOL_TYPE){
appendError(GENERAL_SEMANTIC, "Type Mismatch on " + message);
}
else if((lValue == INT_TYPE && rValue == REAL_TYPE) || (lValue == REAL_TYPE &&
rValue == INT_TYPE)){
appendError(GENERAL_SEMANTIC, "Illegal Narrowing " + message);
}
}
Types checkArithmetic(Types left, Types right)
{
if (left == MISMATCH || right == MISMATCH)
return MISMATCH;
if (left == BOOL_TYPE || right == BOOL_TYPE)
{
appendError(GENERAL_SEMANTIC, "Numeric Type Required");
return MISMATCH;
}
if (left == REAL_TYPE || right == REAL_TYPE) {
return REAL_TYPE;
}
return INT_TYPE;
}
Types checkLogical(Types left, Types right)
{
if (left == MISMATCH || right == MISMATCH)
return MISMATCH;
if (left != BOOL_TYPE || right != BOOL_TYPE)
{
appendError(GENERAL_SEMANTIC, "Boolean Type Required");
return MISMATCH;
}
return BOOL_TYPE;
}
Types checkRemainder(Types left, Types right){
if (left == MISMATCH || right == MISMATCH)
return MISMATCH;
if (left == BOOL_TYPE || right == BOOL_TYPE){
appendError(GENERAL_SEMANTIC, "Remainder Operator Requires Integer Operands");
return MISMATCH;
}
if (left == REAL_TYPE || right == REAL_TYPE){
appendError(GENERAL_SEMANTIC, "Remainder Operator Requires Integer Operands");
return MISMATCH;
}
return INT_TYPE;
}
Types checkIfThen(Types expression, Types left, Types right){
if(expression != BOOL_TYPE){
appendError(GENERAL_SEMANTIC, "If Expression Must Be Boolean");
return MISMATCH;
}
if(left != right){
appendError(GENERAL_SEMANTIC, "If-Then Type Mismatch");
return MISMATCH;
}
return expression;
}
Types checkNegation(Types right){
if (right != BOOL_TYPE){
appendError(GENERAL_SEMANTIC, "Boolean Type Required");
return MISMATCH;
}
return BOOL_TYPE;
}
Types checkRelational(Types left, Types right){
if (checkArithmetic(left, right) == MISMATCH)
return MISMATCH;
return BOOL_TYPE;
}
Types checkExpression(Types expression){
if(expression!=INT_TYPE){
appendError(GENERAL_SEMANTIC, " Case expression Not Integer ");
return MISMATCH;
}
return INT_TYPE;
}
Types checkCases(Types case_, Types cases){
if (cases != INT_TYPE){
if( cases != NAN_TYPE && case_ != cases){
appendError(GENERAL_SEMANTIC, "Case Types Mismatch");
return MISMATCH;
}
}
// if (case_ != INT_TYPE){
// appendError(GENERAL_SEMANTIC, "When Expression Not Integer");
// if (cases != NAN_TYPE && case_ == cases){
// return cases;
// }
// return case_;
// }
return NAN_TYPE;
}
Types checkReturns(Types cases, Types others){
if(others != INT_TYPE){
appendError(GENERAL_SEMANTIC, " Other Expression Not Integer");
return others;
}
if(cases != NAN_TYPE){
return cases;
}
return INT_TYPE;
}

More Related Content

DOCX
Home work for modifying the syntactic analyzer for the attached comp.docx
PDF
Declare Your Language: Type Checking
PDF
Dynamic Semantics
PDF
C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdf
PDF
A Language Designer’s Workbench. A one-stop shop for implementation and verif...
PDF
Scope Graphs: A fresh look at name binding in programming languages
PDF
Compiler Construction | Lecture 8 | Type Constraints
PDF
Compiler Construction | Lecture 9 | Constraint Resolution
Home work for modifying the syntactic analyzer for the attached comp.docx
Declare Your Language: Type Checking
Dynamic Semantics
C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdf
A Language Designer’s Workbench. A one-stop shop for implementation and verif...
Scope Graphs: A fresh look at name binding in programming languages
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 9 | Constraint Resolution

Similar to You should be able to use the same function to check a narrowing fun.pdf (20)

DOCX
project4-ast.DS_Storeproject4-astast.c#include symbolTa.docx
DOCX
CS 280 Fall 2022 Programming Assignment 2 November.docx
PDF
Declare Your Language: Name Resolution
PDF
Declarative Type System Specification with Statix
PPT
popl04.ppt
PDF
Declare Your Language: Dynamic Semantics
PDF
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
PDF
types, types, types
PDF
C++ code, please help! Troubleshooting and cannot for the life of me.pdf
PDF
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
PDF
SWP - A Generic Language Parser
PPT
Antlr V3
PDF
Dynamic Semantics Specification and Interpreter Generation
PDF
Current C++ code- parse-h -- This file contains the function prototype (1).pdf
PDF
Separation of Concerns in Language Definition
KEY
Verification with LoLA: 2 The LoLA Input Language
PDF
Term Rewriting
PDF
Declare Your Language: Constraint Resolution 2
PDF
201506 CSE340 Lecture 18
project4-ast.DS_Storeproject4-astast.c#include symbolTa.docx
CS 280 Fall 2022 Programming Assignment 2 November.docx
Declare Your Language: Name Resolution
Declarative Type System Specification with Statix
popl04.ppt
Declare Your Language: Dynamic Semantics
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
types, types, types
C++ code, please help! Troubleshooting and cannot for the life of me.pdf
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
SWP - A Generic Language Parser
Antlr V3
Dynamic Semantics Specification and Interpreter Generation
Current C++ code- parse-h -- This file contains the function prototype (1).pdf
Separation of Concerns in Language Definition
Verification with LoLA: 2 The LoLA Input Language
Term Rewriting
Declare Your Language: Constraint Resolution 2
201506 CSE340 Lecture 18
Ad

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Trump Administration's workforce development strategy
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
VCE English Exam - Section C Student Revision Booklet
Final Presentation General Medicine 03-08-2024.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Trump Administration's workforce development strategy
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Chinmaya Tiranga quiz Grand Finale.pdf
O7-L3 Supply Chain Operations - ICLT Program
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial diseases, their pathogenesis and prophylaxis
Orientation - ARALprogram of Deped to the Parents.pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Weekly quiz Compilation Jan -July 25.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Anesthesia in Laparoscopic Surgery in India
Pharma ospi slides which help in ospi learning
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
VCE English Exam - Section C Student Revision Booklet
Ad

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 {$$ =
  • 5. checkExpression($2); $$ = checkReturns(case_return, $7);}; | REDUCE operator reductions ENDREDUCE {$$ = $3;} ; cases: cases case {case_return = checkCases($2, case_return);}| %empty {$$ = NAN_TYPE;}; case: WHEN INT_LITERAL ARROW statement_ {$$ = $4;} ; reductions: reductions statement_ {$$ = checkArithmetic($1, $2);} | {$$ = INT_TYPE;} %empty; operator: ADDOP | RELOP | EXPOP | MULOP ; expression: expression OROP binary {$$ = checkLogical($1, $3);} | binary ; binary: binary ANDOP relation {$$ = checkLogical($1, $3);} | relation ; relation: relation RELOP term {$$ = checkRelational($1, $3);}| term ; term: term ADDOP factor {$$ = checkArithmetic($1, $3);} | factor ; factor: factor MULOP primary {$$ = checkArithmetic($1, $3);} | factor REMOP exponent {$$ = checkRemainder($1, $3);} | exponent ; exponent: unary | unary EXPOP primary {$$ = checkArithmetic($1, $3);} ;
  • 6. unary: NOTOP primary {$$ = checkNegation($2);} | primary; primary: '(' expression ')' {$$ = $2;} | INT_LITERAL | BOOL_LITERAL | REAL_LITERAL | IDENTIFIER {if (!symbols.find($1, $$)) appendError(UNDECLARED, $1);} ; %% void yyerror(const char* message) { appendError(SYNTAX, message); } int main(int argc, char *argv[]) { firstLine(); yyparse(); lastLine(); return 0; } heres my types.cc code: // Compiler Theory and Design // Duane J. Jarc // This file contains the bodies of the type checking functions #include #include using namespace std; #include "types.h" #include "listing.h" void checkAssignment(Types lValue, Types rValue, string message) { if(rValue == MISMATCH) return; if(lValue == BOOL_TYPE && rValue != BOOL_TYPE || lValue!=BOOL_TYPE && rValue ==BOOL_TYPE){
  • 7. appendError(GENERAL_SEMANTIC, "Type Mismatch on " + message); } else if((lValue == INT_TYPE && rValue == REAL_TYPE) || (lValue == REAL_TYPE && rValue == INT_TYPE)){ appendError(GENERAL_SEMANTIC, "Illegal Narrowing " + message); } } Types checkArithmetic(Types left, Types right) { if (left == MISMATCH || right == MISMATCH) return MISMATCH; if (left == BOOL_TYPE || right == BOOL_TYPE) { appendError(GENERAL_SEMANTIC, "Numeric Type Required"); return MISMATCH; } if (left == REAL_TYPE || right == REAL_TYPE) { return REAL_TYPE; } return INT_TYPE; } Types checkLogical(Types left, Types right) { if (left == MISMATCH || right == MISMATCH) return MISMATCH; if (left != BOOL_TYPE || right != BOOL_TYPE) { appendError(GENERAL_SEMANTIC, "Boolean Type Required"); return MISMATCH; } return BOOL_TYPE; } Types checkRemainder(Types left, Types right){ if (left == MISMATCH || right == MISMATCH) return MISMATCH; if (left == BOOL_TYPE || right == BOOL_TYPE){
  • 8. appendError(GENERAL_SEMANTIC, "Remainder Operator Requires Integer Operands"); return MISMATCH; } if (left == REAL_TYPE || right == REAL_TYPE){ appendError(GENERAL_SEMANTIC, "Remainder Operator Requires Integer Operands"); return MISMATCH; } return INT_TYPE; } Types checkIfThen(Types expression, Types left, Types right){ if(expression != BOOL_TYPE){ appendError(GENERAL_SEMANTIC, "If Expression Must Be Boolean"); return MISMATCH; } if(left != right){ appendError(GENERAL_SEMANTIC, "If-Then Type Mismatch"); return MISMATCH; } return expression; } Types checkNegation(Types right){ if (right != BOOL_TYPE){ appendError(GENERAL_SEMANTIC, "Boolean Type Required"); return MISMATCH; } return BOOL_TYPE; } Types checkRelational(Types left, Types right){ if (checkArithmetic(left, right) == MISMATCH) return MISMATCH; return BOOL_TYPE; } Types checkExpression(Types expression){ if(expression!=INT_TYPE){ appendError(GENERAL_SEMANTIC, " Case expression Not Integer "); return MISMATCH;
  • 9. } return INT_TYPE; } Types checkCases(Types case_, Types cases){ if (cases != INT_TYPE){ if( cases != NAN_TYPE && case_ != cases){ appendError(GENERAL_SEMANTIC, "Case Types Mismatch"); return MISMATCH; } } // if (case_ != INT_TYPE){ // appendError(GENERAL_SEMANTIC, "When Expression Not Integer"); // if (cases != NAN_TYPE && case_ == cases){ // return cases; // } // return case_; // } return NAN_TYPE; } Types checkReturns(Types cases, Types others){ if(others != INT_TYPE){ appendError(GENERAL_SEMANTIC, " Other Expression Not Integer"); return others; } if(cases != NAN_TYPE){ return cases; } return INT_TYPE; }