Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • tddd55/tddd55-lab
  • yunli455/tddd55-lab
  • dimpi105/tddd55-lab
  • aleen361/tddd55-lab
4 results
Select Git revision
Show changes
Commits on Source (3)
...@@ -75,7 +75,7 @@ int main(int argc, char **argv) ...@@ -75,7 +75,7 @@ int main(int argc, char **argv)
int token; int token;
extern FILE *yyin; extern FILE *yyin;
extern int yylex(); extern int yylex();
/* /*
* Open the input file, if any * Open the input file, if any
*/ */
...@@ -92,7 +92,7 @@ int main(int argc, char **argv) ...@@ -92,7 +92,7 @@ int main(int argc, char **argv)
perror(argv[1]); perror(argv[1]);
exit(1); exit(1);
} }
break; break;
default: default:
cerr << "Usage: " << argv[0] << " [ filename ]\n"; cerr << "Usage: " << argv[0] << " [ filename ]\n";
exit(1); exit(1);
......
/*
This test checks the fibonacci function
Author: John Tinnerholm
*/
declare
a : integer;
function fib (x : integer) : integer
begin
if x == 0 then
begin
return 0;
end
elseif x == 1 then
begin
return 1;
end
else
begin
return fib(x - 1) + fib(x-2);
end
if;
end;
begin
a := 0;
while a < 10 do
begin
putint(fib(a));
putline();
a := a + 1;
end
while;
end;
...@@ -25,7 +25,7 @@ double Parser::Parse(void) ...@@ -25,7 +25,7 @@ double Parser::Parse(void)
* Parse the input using recursive descent parsing. You'll need * Parse the input using recursive descent parsing. You'll need
* an object of type Scanner to get tokens from the input. Call * an object of type Scanner to get tokens from the input. Call
* the Scan method of this object to get the next token in the * the Scan method of this object to get the next token in the
* input stream. * input stream.
*/ */
/* --- End your code --- */ /* --- End your code --- */
...@@ -37,17 +37,17 @@ void Parser::Recover(void) ...@@ -37,17 +37,17 @@ void Parser::Recover(void)
{ {
cerr << "Error recovery.\n" << flush; cerr << "Error recovery.\n" << flush;
/* --- Your code here --- /* --- Your code here ---
* *
* Error recovery routine * Error recovery routine
* *
* Unless you come up with something better, this function should * Unless you come up with something better, this function should
* scan tokens until it sees the end of line or end of file. * scan tokens until it sees the end of line or end of file.
* Parsing may be resumed at that point. This means that if an end * Parsing may be resumed at that point. This means that if an end
* of line token caused the error, this routine doesn't need to do * of line token caused the error, this routine doesn't need to do
* anything. * anything.
* *
* Be sure not to scan more tokens than necessary, or it won't be * Be sure not to scan more tokens than necessary, or it won't be
* possible to resume parsing. * possible to resume parsing.
*/ */
......
...@@ -17,6 +17,7 @@ using namespace std; ...@@ -17,6 +17,7 @@ using namespace std;
class Parser class Parser
{ {
public: public:
Scanner scanner;
void Recover(void); // Error recovery routine void Recover(void); // Error recovery routine
double Parse(void); // Main entry point double Parse(void); // Main entry point
}; };
......
...@@ -81,12 +81,12 @@ void Scanner::PutbackChar(unsigned char c) ...@@ -81,12 +81,12 @@ void Scanner::PutbackChar(unsigned char c)
Token Scanner::Scan(void) Token Scanner::Scan(void)
{ {
int c; int c;
Reset(); Reset();
while (1) while (1)
{ {
c = GetChar(); c = GetChar();
switch (state) switch (state)
{ {
case 0: // Initial state case 0: // Initial state
...@@ -311,7 +311,6 @@ ostream& operator<<(ostream& s, ScannerError& e) ...@@ -311,7 +311,6 @@ ostream& operator<<(ostream& s, ScannerError& e)
// Lookup just returns the textual representation of a token type. The // Lookup just returns the textual representation of a token type. The
// no argument version returns the type of the token. // no argument version returns the type of the token.
// //
const char *Token::Lookup(void) const char *Token::Lookup(void)
{ {
return kTokenTypeNames[type]; return kTokenTypeNames[type];
......
...@@ -14,7 +14,7 @@ int main(void) ...@@ -14,7 +14,7 @@ int main(void)
try try
{ {
cout << "Expression: " << flush; cout << "Expression: " << flush;
/* Implement the parser.Parse method */ /* Implement the parser.Parse method */
val = parser.Parse(); val = parser.Parse();
cout << "Result: " << val << '\n' << flush; cout << "Result: " << val << '\n' << flush;
} }
......
...@@ -241,7 +241,7 @@ void BinaryOperation::xprint(ostream& o, const char *cls) ...@@ -241,7 +241,7 @@ void BinaryOperation::xprint(ostream& o, const char *cls)
endChild(o); endChild(o);
} }
void Plus::print(ostream& o) { xprint(o, "Plus"); } // void Plus::print(ostream& o) { xprint(o, "Plus"); } //
void Minus::print(ostream& o) { xprint(o, "Minus"); } void Minus::print(ostream& o) { xprint(o, "Minus"); }
void Times::print(ostream& o) { xprint(o, "Times"); } void Times::print(ostream& o) { xprint(o, "Times"); }
void Divide::print(ostream& o) {xprint(o, "Divide"); } void Divide::print(ostream& o) {xprint(o, "Divide"); }
...@@ -328,7 +328,7 @@ void BinaryCondition::xprint(ostream& o, const char *cls) ...@@ -328,7 +328,7 @@ void BinaryCondition::xprint(ostream& o, const char *cls)
} }
void And::print(ostream& o) { xprint(o, "And"); } void And::print(ostream& o) { xprint(o, "And"); }
void Or::print(ostream& o) { xprint(o, "Or"); } // void Or::print(ostream& o) { xprint(o, "Or"); } //
void Not::print(ostream& o) void Not::print(ostream& o)
{ {
......
This diff is collapsed.
...@@ -31,7 +31,7 @@ int main(int argc, char **argv) ...@@ -31,7 +31,7 @@ int main(int argc, char **argv)
{ {
int option; int option;
extern FILE *yyin; extern FILE *yyin;
/* /*
Set up the symbol table. Set up the symbol table.
Adds builtin functions used by the language. Adds builtin functions used by the language.
...@@ -55,7 +55,7 @@ int main(int argc, char **argv) ...@@ -55,7 +55,7 @@ int main(int argc, char **argv)
kIReadFunction->SetReturnType(kIntegerType); kIReadFunction->SetReturnType(kIntegerType);
kFReadFunction->SetReturnType(kRealType); kFReadFunction->SetReturnType(kRealType);
kIReadFunction->SetReturnType(kIntegerType); kIReadFunction->SetReturnType(kIntegerType);
currentFunction->AddSymbol(kIntegerType); currentFunction->AddSymbol(kIntegerType);
currentFunction->AddSymbol(kRealType); currentFunction->AddSymbol(kRealType);
currentFunction->AddSymbol(kIPrintFunction); currentFunction->AddSymbol(kIPrintFunction);
...@@ -82,13 +82,13 @@ int main(int argc, char **argv) ...@@ -82,13 +82,13 @@ int main(int argc, char **argv)
break; break;
case '?': case '?':
Usage(argv[0]); Usage(argv[0]);
break; break;
case 'c': case 'c':
std::cout << "/*Using standard-library*/\n"; std::cout << "/*Using standard-library*/\n";
std::cout << "#include \"stl.h\"\n"; std::cout << "#include \"stl.h\"\n";
SymbolInformation::outputFormat = SymbolInformation::kCFormat; SymbolInformation::outputFormat = SymbolInformation::kCFormat;
currentFunction->outputFormat = SymbolInformation::kCFormat; currentFunction->outputFormat = SymbolInformation::kCFormat;
break; break;
} }
} }
...@@ -102,7 +102,7 @@ int main(int argc, char **argv) ...@@ -102,7 +102,7 @@ int main(int argc, char **argv)
} }
optind += 1; optind += 1;
} }
if (optind < argc) if (optind < argc)
Usage(argv[0]); Usage(argv[0]);
...@@ -113,4 +113,3 @@ int main(int argc, char **argv) ...@@ -113,4 +113,3 @@ int main(int argc, char **argv)
int retcode = yyparse(); int retcode = yyparse();
return retcode; return retcode;
} }
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
extern char *yytext; extern char *yytext;
extern int yylineno, errorCount, warningCount; extern int yylineno, errorCount, warningCount;
extern FunctionInformation *currentFunction; extern FunctionInformation *currentFunction;
extern int yylex(void); extern int yylex(void);
extern void yyerror(const char *); extern void yyerror(const char *);
...@@ -45,12 +45,12 @@ extern ostream& warning(void); ...@@ -45,12 +45,12 @@ extern ostream& warning(void);
FunctionCall *call; FunctionCall *call;
LeftValue *lvalue; LeftValue *lvalue;
ElseIfList *elseIfList; ElseIfList *elseIfList;
VariableInformation *variable; VariableInformation *variable;
TypeInformation *type; TypeInformation *type;
FunctionInformation *function; FunctionInformation *function;
::string *id; ::string *id;
int integer; int integer;
double real; double real;
void *null; void *null;
...@@ -184,10 +184,10 @@ functions : functions function ...@@ -184,10 +184,10 @@ functions : functions function
*/ */
function : function :
{ {
cerr << "Function here" << endl; cerr << "Function here" << endl;
} }
; ;
/* --- End your code --- */ /* --- End your code --- */
...@@ -249,7 +249,7 @@ type : id ...@@ -249,7 +249,7 @@ type : id
else else
{ {
typeInfo = info->SymbolAsType(); typeInfo = info->SymbolAsType();
if (typeInfo == NULL) if (typeInfo == NULL)
{ {
error() << *($1) << " is not a type" << "\n" <<flush; error() << *($1) << " is not a type" << "\n" <<flush;
...@@ -299,7 +299,7 @@ statements : statements statement ...@@ -299,7 +299,7 @@ statements : statements statement
if ($2 == NULL) if ($2 == NULL)
$$ = NULL; $$ = NULL;
else else
$$ = new StatementList($1, $2); $$ = new StatementList($1, $2);
} }
| /* Empty */ | /* Empty */
{ {
...@@ -403,7 +403,7 @@ returnstmt : RETURN expression ...@@ -403,7 +403,7 @@ returnstmt : RETURN expression
error() << " attempt to return " error() << " attempt to return "
<< ShortSymbols << expr->valueType << '\n'; << ShortSymbols << expr->valueType << '\n';
error() << " in function declared to return " error() << " in function declared to return "
<< ShortSymbols << ShortSymbols
<< currentFunction->GetReturnType() << currentFunction->GetReturnType()
<< LongSymbols << '\n'; << LongSymbols << '\n';
$$ = NULL; $$ = NULL;
...@@ -454,13 +454,13 @@ variable : id ...@@ -454,13 +454,13 @@ variable : id
<< *($1) << *($1)
<< "\n" << "\n"
<< flush; << flush;
$$ = NULL; $$ = NULL;
} }
else else
{ {
varInfo = info->SymbolAsVariable(); varInfo = info->SymbolAsVariable();
if (varInfo == NULL) if (varInfo == NULL)
{ {
error() error()
...@@ -545,7 +545,7 @@ call : funcname '(' expressions ')' ...@@ -545,7 +545,7 @@ call : funcname '(' expressions ')'
id : ID id : ID
{ {
$$ = new ::string(yytext); $$ = new ::string(yytext);
} }
; ;
...@@ -576,13 +576,13 @@ real : REAL ...@@ -576,13 +576,13 @@ real : REAL
*/ */
expression : expression :
{ {
cerr << "Expression here" << endl; cerr << "Expression here" << endl;
} }
; ;
/* --- End your code --- */ /* --- End your code --- */
expressions : expressionz expressions : expressionz
{ {
...@@ -620,10 +620,10 @@ expressionz : expressionz ',' expression ...@@ -620,10 +620,10 @@ expressionz : expressionz ',' expression
*/ */
condition : condition :
{ {
cerr << "Condition here" << endl; cerr << "Condition here" << endl;
} }
; ;
/* --- End your code --- */ /* --- End your code --- */
...@@ -632,7 +632,7 @@ condition : ...@@ -632,7 +632,7 @@ condition :
int errorCount = 0; int errorCount = 0;
int warningCount = 0; int warningCount = 0;
/* --- Your code here --- /* --- Your code here ---
* *
...@@ -642,14 +642,14 @@ int warningCount = 0; ...@@ -642,14 +642,14 @@ int warningCount = 0;
/* It is reasonable to believe that you will need a function /* It is reasonable to believe that you will need a function
* that checks that two expressions are of compatible types, * that checks that two expressions are of compatible types,
* and if possible makes a type conversion. * and if possible makes a type conversion.
* For your convenience a skeleton for such a function is * For your convenience a skeleton for such a function is
* provided below. It will be very similar to CheckAssignmentTypes. * provided below. It will be very similar to CheckAssignmentTypes.
*/ */
/* /*
* CheckCompatibleTypes checks that the expressions indirectly pointed * CheckCompatibleTypes checks that the expressions indirectly pointed
* to by left and right are compatible. If type conversion is * to by left and right are compatible. If type conversion is
* necessary, the pointers left and right point to will be modified to * necessary, the pointers left and right point to will be modified to
* point to the node representing type conversion. That's why you have * point to the node representing type conversion. That's why you have
* to pass a pointer to pointer to Expression in these arguments. * to pass a pointer to pointer to Expression in these arguments.
*/ */
...@@ -702,7 +702,7 @@ char CheckAssignmentTypes(LeftValue **left, Expression **right) ...@@ -702,7 +702,7 @@ char CheckAssignmentTypes(LeftValue **left, Expression **right)
/* /*
* CheckFunctionParameters is used to check parameters passed to a * CheckFunctionParameters is used to check parameters passed to a
* function. func is the function we're passing parameters to, formals * function. func is the function we're passing parameters to, formals
* is a pointer to the last formal parameter we're checking against * is a pointer to the last formal parameter we're checking against
* and params is a pointer to the ExpressionList we're checking. If * and params is a pointer to the ExpressionList we're checking. If
* type conversion is necessary, the Expressions pointed to by the * type conversion is necessary, the Expressions pointed to by the
...@@ -769,7 +769,7 @@ char CheckFunctionParameters(FunctionInformation *func, ...@@ -769,7 +769,7 @@ char CheckFunctionParameters(FunctionInformation *func,
<< params->expression->valueType << params->expression->valueType
<< '\n' << '\n'
<< LongSymbols << flush; << LongSymbols << flush;
return 0; return 0;
} }
} }
} }
......
...@@ -20,7 +20,7 @@ using std::ostream; ...@@ -20,7 +20,7 @@ using std::ostream;
// really necessary. If strings were considered read only (no // really necessary. If strings were considered read only (no
// destructive update), then it would be possible to keep a pool of // destructive update), then it would be possible to keep a pool of
// strings with reference counts and just pass along references to // strings with reference counts and just pass along references to
// that pool. // that pool.
// //
class string class string
...@@ -36,7 +36,7 @@ private: ...@@ -36,7 +36,7 @@ private:
public: public:
//To kill warnings and make this custom string play nice... //To kill warnings and make this custom string play nice...
class error {}; // Exception thrown when out of memory class error {}; // Exception thrown when out of memory
string(); // Default constructor creates empty string string(); // Default constructor creates empty string
...@@ -54,7 +54,7 @@ public: ...@@ -54,7 +54,7 @@ public:
string& operator=(const string&); // Assignment operator string& operator=(const string&); // Assignment operator
string& operator=(const char *); // Assignment operator string& operator=(const char *); // Assignment operator
string& operator=(const char); // Assignment operator string& operator=(const char); // Assignment operator
string& operator+=(const string&); // Append operator string& operator+=(const string&); // Append operator
string& operator+=(const char); // Append operator string& operator+=(const char); // Append operator
string& operator+=(const char *); // Append operator string& operator+=(const char *); // Append operator
......
...@@ -44,7 +44,7 @@ ostream& SymbolInformation::print(ostream& o) ...@@ -44,7 +44,7 @@ ostream& SymbolInformation::print(ostream& o)
break; break;
case kCFormat: case kCFormat:
o << id; o << id;
break; break;
default: default:
o << "Bad output format\n"; o << "Bad output format\n";
abort(); abort();
...@@ -80,10 +80,10 @@ ostream& TypeInformation::print(ostream& o) ...@@ -80,10 +80,10 @@ ostream& TypeInformation::print(ostream& o)
{ {
o << id; o << id;
} }
o << " [" << size << "]"; o << " [" << size << "]";
break; break;
case kShortFormat: case kShortFormat:
if (elementType != NULL) if (elementType != NULL)
{ {
...@@ -103,7 +103,7 @@ ostream& TypeInformation::print(ostream& o) ...@@ -103,7 +103,7 @@ ostream& TypeInformation::print(ostream& o)
} }
else else
{ {
o << id; o << id;
} }
break; break;
...@@ -200,21 +200,21 @@ ostream& FunctionInformation::print(ostream& o) ...@@ -200,21 +200,21 @@ ostream& FunctionInformation::print(ostream& o)
tmp = tmp->prev; tmp = tmp->prev;
} }
o << LongSymbols; o << LongSymbols;
o << '\n'; o << '\n';
} }
else else
{ {
o << " Locals: none\n"; o << " Locals: none\n";
} }
o << " Body: " << (void*)body << '\n'; o << " Body: " << (void*)body << '\n';
if (body) o << body; if (body) o << body;
o << '\n'; o << '\n';
o << " Quads: " << (void*)quads << '\n'; o << " Quads: " << (void*)quads << '\n';
if (quads) o << quads; if (quads) o << quads;
o << '\n'; o << '\n';
o << symbolTable; o << symbolTable;
break; break;
...@@ -240,59 +240,59 @@ ostream& FunctionInformation::print(ostream& o) ...@@ -240,59 +240,59 @@ ostream& FunctionInformation::print(ostream& o)
break; break;
case kCFormat: case kCFormat:
/*C-Code preamble*/ /*C-Code preamble*/
if (returnType == NULL) { if (returnType == NULL) {
o << "void\n"; o << "void\n";
} else { } else {
o << returnType << "\n"; o << returnType << "\n";
} }
o << "" << id << "("; o << "" << id << "(";
//Parameters //Parameters
if (lastParam != NULL) if (lastParam != NULL)
{ {
tmp = lastParam; tmp = lastParam;
while (tmp != NULL) while (tmp != NULL)
{ {
if (tmp->type->elementType != NULL){ //Array... if (tmp->type->elementType != NULL){ //Array...
o << tmp->type->elementType << "*" << "\t"; o << tmp->type->elementType << "*" << "\t";
} else { } else {
// o << tmp->type->elementType; // o << tmp->type->elementType;
o << tmp->type << "\t"; o << tmp->type << "\t";
} }
if (tmp->prev == NULL) { if (tmp->prev == NULL) {
o << tmp->id; o << tmp->id;
} else { } else {
o << tmp->id << ","; o << tmp->id << ",";
} }
tmp = tmp->prev; tmp = tmp->prev;
} }
} }
o << ")\n"; o << ")\n";
/* Generate C-Code for the body! */ /* Generate C-Code for the body! */
o << "{\n"; o << "{\n";
if (lastLocal) if (lastLocal)
{ {
o << "//Locals:\n"; o << "//Locals:\n";
tmp = lastLocal; tmp = lastLocal;
while (tmp != NULL) while (tmp != NULL)
{ {
if (tmp->type->elementType != NULL){ //Array... if (tmp->type->elementType != NULL){ //Array...
o << tmp->type->elementType; o << tmp->type->elementType;
o << "\t"; o << "\t";
o << tmp; o << tmp;
o << tmp->type << ";\n"; o << tmp->type << ";\n";
} }
else { else {
o << tmp->type << "\t" << tmp << ";\n"; o << tmp->type << "\t" << tmp << ";\n";
} }
tmp = tmp->prev; tmp = tmp->prev;
} }
} }
quads->print_c(o); quads->print_c(o);
o << "}\n"; o << "}\n";
break; break;
default: default:
o << "Bad output format.\n"; o << "Bad output format.\n";
abort(); abort();
...@@ -430,14 +430,14 @@ FunctionInformation *FunctionInformation::AddFunction(const ::string& name, ...@@ -430,14 +430,14 @@ FunctionInformation *FunctionInformation::AddFunction(const ::string& name,
if (xinfo != NULL && xinfo->tag == kTypeInformation) if (xinfo != NULL && xinfo->tag == kTypeInformation)
{ {
cerr << "Bug: you tried to create a function that's also a type\n"; cerr << "Bug: you tried to create a function that's also a type\n";
abort(); abort();
} }
xinfo = symbolTable.LookupSymbol(name); xinfo = symbolTable.LookupSymbol(name);
if (xinfo != NULL) if (xinfo != NULL)
{ {
cerr << "Bug: you tried to create a function with a name " cerr << "Bug: you tried to create a function with a name "
<< "that's already in use\n"; << "that's already in use\n";
} }
fn->id = name; fn->id = name;
...@@ -458,7 +458,7 @@ VariableInformation *FunctionInformation::TemporaryVariable(TypeInformation *typ ...@@ -458,7 +458,7 @@ VariableInformation *FunctionInformation::TemporaryVariable(TypeInformation *typ
return info; return info;
} }
char FunctionInformation::OkToAddSymbol(const ::string& name) char FunctionInformation::OkToAddSymbol(const ::string& name)
{ {
......