Source Documentation¶
Lab2¶
-
class
Parser¶ - #include <lab2.hh>
-
class
ParserEndOfFile¶ - #include <lab2.hh>
-
class
ParserError¶ - #include <lab2.hh>
-
class
Result¶
-
class
Scanner¶ - #include <lex.hh>
-
class
ScannerError¶ - #include <lex.hh>
-
class
Token¶ - #include <lex.hh>
-
class
Trace¶ - #include <lab2.hh>
Private Members
-
const char *
name¶
Private Static Attributes
-
static int
indent= 0¶
-
const char *
-
namespace
std¶
-
module
test¶ Written by Martin Olivesten in 2023 for TDDD55. Full permission to use in anyway with no restrictions. :) Run: `python3 test.py -h` for usage.
Functions
-
parse_tests(TextIOWrapper file)¶
-
run_tests(List[Tuple[str, str]] tests, str prog)¶
-
main(TextIOWrapper file, str prog)¶
Variables
-
GREEN= '\x1b[32m'¶
-
YELLOW= '\x1b[33m'¶
-
RED= '\x1b[31m'¶
-
RESET= '\x1b[39m'¶
-
PASS= f'{GREEN}PASS{RESET}'¶
-
FAIL= f'{RED}FAIL{RESET}'¶
-
MAYBE= f'{YELLOW}MAYBE{RESET}'¶
-
REAL_REGEX= r'-?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][+-]?[0-9]+)?'¶
-
dump= ''¶
-
ap= ArgParse()¶
-
help¶
-
default¶
-
args= ap.parse_args()¶
-
-
file
lab2.cc - #include “lab2.hh”
-
file
lab2.hh - #include <iostream>#include “lex.hh”
-
file
lex.cc - #include “lex.hh”#include <stdlib.h>#include <string.h>#include <stdio.h>#include <ctype.h>
Functions
-
ostream &
operator<<(ostream &s, ScannerError &e)¶
Variables
-
static const char *
kTokenTypeNames[] = {"uninitialized", "number", "identifier", "plus", "minus", "times", "divide", "left paren", "right paren", "assignment", "end of file", "end of line", "power"}¶
-
ostream &
-
file
lex.hh - #include <iostream>
Enums
-
enum
TokenType¶ Values:
-
enumerator
kUninitialized¶
-
enumerator
kNumber¶
-
enumerator
kIdentifier¶
-
enumerator
kPlus¶
-
enumerator
kMinus¶
-
enumerator
kTimes¶
-
enumerator
kDivide¶
-
enumerator
kLeftParen¶
-
enumerator
kRightParen¶
-
enumerator
kAssign¶
-
enumerator
kEndMark¶
-
enumerator
kEndOfLine¶
-
enumerator
kPower¶
-
enumerator
Variables
-
const int
kMaxTokenLength= 1024¶
-
enum
-
file
main.cc - #include <iostream>#include <stdlib.h>#include “lab2.hh”#include “lex.hh”
Functions
-
int
main(void)¶
-
int
-
file
README.md
-
file
test.py
-
dir
/home/adrpo33/teaching/tdde66/ai/webpage/tddd55-lab/lab2 Instructions on how to run the test script¶
This folder contains the code for lab 2. A test script exists to test your implementation. To run this test script, you must execute the following command in the terminal on the IDA Lab Computers.
python3 test.py test.txt --program ./lab2
This assumes that you have successfully compiled the project by executing the makefile and that the program has been configured to read successfully from stdin.
Furthermore, the test script assumes that the lab has been completed according to the instructions, mistakes might cause the test script to malfunction.
To make the project make sure that you execute:
c++ int main(void) { Parser parser; double val;make ```` in the command line interface. ## Notes (Important) * The test script assumes that you have Python 3 installed and that you run on the IDA Lab Systems. It is has not been tested on other systems such as MacOS or GNU/Linux. * Initially the program will just enter an infinite loop. Hence, you will need to modify the program and get the basic scaffolding in place before you can run the test script. Thus, it might be a good idea to test of some of the arthimatic expressions in the terminal before proceeding with the test script. To avoid inifnite loops when using files from stdin the following code snippet could be helpful. Here we specifiy that if we encounter the end of file token we will throw an `ParserEndOfFile` error. ````c++ double Parser::<your name of the start expression>() { //Note almost correct C++ but you will need to modify this code in order to make it run corretly. Do not simply just copy paste. currentToken = scanner.Scan(); if (currentToken.type == kEndMark) { throw ParserEndOfFile(); } return <name of your expression method>(); } ```` This will be cought by the main loop which is configured in the following way:while (true) { try { cout << “Expression: ” << flush; /* Implement the parser.Parse method */ val = parser.Parse(); cout << “Result: ” << val << ‘
’ << flush; } catch (
ScannerError& e) { cerr << e << ‘
’ << flush; parser.Recover(); } catch (
ParserError) { parser.Recover(); } catch (ParserEndOfFile) { cerr << “End of file\n” << flush; exit(0); } } } ```Note that the program will terminate if the end of file token is encountered.
Lab3-4¶
-
class
And: public BinaryCondition¶ - #include <ast.hh>
Public Functions
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
virtual VariableInformation *
-
class
ArrayReference: public LeftValue¶ - #include <ast.hh>
Public Functions
-
inline
ArrayReference(VariableInformation *i, Expression *x)¶
-
virtual void
GenerateAssignment(QuadsList &q, VariableInformation *val)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
Assignment: public Statement¶ - #include <ast.hh>
Public Functions
-
inline
Assignment(LeftValue *l, Expression *r)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
ASTNode¶ - #include <ast.hh>
Subclassed by Condition, ElseIfList, Expression, ExpressionList, Statement, StatementList
Public Functions
-
virtual VariableInformation *
GenerateCode(QuadsList &q) = 0¶
-
virtual VariableInformation *
GenerateCodeAndJump(QuadsList &q, long label)¶
-
virtual VariableInformation *
-
class
BinaryCondition: public Condition¶ - #include <ast.hh>
Public Functions
-
virtual VariableInformation *
GenerateCode(QuadsList &q) = 0¶
-
virtual VariableInformation *
-
class
BinaryOperation: public Expression¶ - #include <ast.hh>
Subclassed by Divide, Minus, Plus, Power, Times
Public Functions
-
inline
BinaryOperation(Expression *l, Expression *r)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q) = 0¶
-
inline
-
class
BinaryRelation: public Condition¶ - #include <ast.hh>
Subclassed by Equal, GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual, NotEqual
Public Functions
-
inline
BinaryRelation(Expression *l, Expression *r)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q) = 0¶
-
inline
-
class
BooleanConstant: public Condition¶ - #include <ast.hh>
Public Functions
-
inline
BooleanConstant(int v)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Public Members
-
bool
value¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
CallStatement: public Statement¶ - #include <ast.hh>
Public Functions
-
inline
CallStatement(FunctionCall *c)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Public Members
-
FunctionCall *
call¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
Condition: public ASTNode¶ - #include <ast.hh>
Subclassed by BinaryCondition, BinaryRelation, BooleanConstant, Not
Public Functions
-
virtual VariableInformation *
GenerateCode(QuadsList &q) = 0¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
virtual VariableInformation *
-
class
Divide: public BinaryOperation¶ - #include <ast.hh>
Public Functions
-
inline
Divide(Expression *l, Expression *r)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
ElseIfList: public ASTNode¶ - #include <ast.hh>
Public Functions
-
inline
ElseIfList(ElseIfList *p, Condition *c, StatementList *b)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
-
virtual VariableInformation *
GenerateCodeAndJump(QuadsList &q, long lbl)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
Equal: public BinaryRelation¶ - #include <ast.hh>
Public Functions
-
inline
Equal(Expression *l, Expression *r)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
error¶ - #include <string.hh>
-
class
Expression: public ASTNode¶ - #include <ast.hh>
Subclassed by BinaryOperation, FunctionCall, IntegerConstant, IntegerToReal, LeftValue, RealConstant, TruncateReal, UnaryMinus
Public Functions
-
inline
Expression(TypeInformation *t)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q) = 0¶
Public Members
-
TypeInformation *
valueType¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
ExpressionList: public ASTNode¶ - #include <ast.hh>
Public Functions
-
inline
ExpressionList(ExpressionList *pe, Expression *e)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
-
virtual void
GenerateParameterList(QuadsList &q, VariableInformation *firstParam)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
FunctionCall: public Expression¶ - #include <ast.hh>
Public Functions
-
inline
FunctionCall(FunctionInformation *f, ExpressionList *a)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
FunctionInformation: public SymbolInformation¶ - #include <symtab.hh>
Public Functions
-
inline virtual FunctionInformation *
SymbolAsFunction(void)¶
-
void
SetParent(FunctionInformation*)¶
-
void
SetReturnType(TypeInformation*)¶
-
void
SetBody(StatementList*)¶
-
FunctionInformation *
GetParent(void)¶
-
TypeInformation *
GetReturnType(void)¶
-
VariableInformation *
GetLastParam(void)¶
-
StatementList *
GetBody(void)¶
-
FunctionInformation *
AddFunction(const ::string&, FunctionInformation*)¶
-
VariableInformation *
AddParameter(const ::string&, TypeInformation*)¶
-
VariableInformation *
AddVariable(const ::string&, TypeInformation*)¶
-
SymbolInformation *
AddSymbol(SymbolInformation*)¶
-
TypeInformation *
AddArrayType(TypeInformation*, int)¶
-
VariableInformation *
TemporaryVariable(TypeInformation *type)¶
-
void
GenerateCode(void)¶
-
SymbolInformation *
LookupIdentifier(const ::string&)¶
Protected Functions
-
virtual ostream &
print(ostream&)¶
Protected Attributes
-
long
temporaryCount¶
Private Members
-
FunctionInformation *
parent¶
-
TypeInformation *
returnType¶
-
VariableInformation *
lastParam¶
-
VariableInformation *
lastLocal¶
-
SymbolTable
symbolTable¶
-
StatementList *
body¶
Friends
- friend class SymbolTable
-
inline virtual FunctionInformation *
-
class
GreaterThan: public BinaryRelation¶ - #include <ast.hh>
Public Functions
-
inline
GreaterThan(Expression *l, Expression *r)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
GreaterThanOrEqual: public BinaryRelation¶ - #include <ast.hh>
Public Functions
-
inline
GreaterThanOrEqual(Expression *l, Expression *r)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
Identifier: public LeftValue¶ - #include <ast.hh>
Public Functions
-
inline
Identifier(VariableInformation *i)¶
-
virtual void
GenerateAssignment(QuadsList &q, VariableInformation *val)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Public Members
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
IfStatement: public Statement¶ - #include <ast.hh>
Public Functions
-
inline
IfStatement(Condition *c, StatementList *ts, ElseIfList *eif, StatementList *es)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Public Members
-
StatementList *
thenStatements¶
-
ElseIfList *
elseIfList¶
-
StatementList *
elseStatements¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
IntegerConstant: public Expression¶ - #include <ast.hh>
Public Functions
-
inline
IntegerConstant(long int v)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Public Members
-
long int
value¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
IntegerToReal: public Expression¶ - #include <ast.hh>
Public Functions
-
inline
IntegerToReal(Expression *e)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Public Members
-
Expression *
value¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
LeftValue: public Expression¶ - #include <ast.hh>
Subclassed by ArrayReference, Identifier
Public Functions
-
inline
LeftValue(TypeInformation *t)¶
-
virtual void
GenerateAssignment(QuadsList &q, VariableInformation *val) = 0¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q) = 0¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
LessThan: public BinaryRelation¶ - #include <ast.hh>
Public Functions
-
inline
LessThan(Expression *l, Expression *r)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
LessThanOrEqual: public BinaryRelation¶ - #include <ast.hh>
Public Functions
-
inline
LessThanOrEqual(Expression *l, Expression *r)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
Minus: public BinaryOperation¶ - #include <ast.hh>
Public Functions
-
inline
Minus(Expression *l, Expression *r)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
NotEqual: public BinaryRelation¶ - #include <ast.hh>
Public Functions
-
inline
NotEqual(Expression *l, Expression *r)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
Or: public BinaryCondition¶ - #include <ast.hh>
Public Functions
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
virtual VariableInformation *
-
class
Plus: public BinaryOperation¶ - #include <ast.hh>
Public Functions
-
inline
Plus(Expression *l, Expression *r)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
Power: public BinaryOperation¶ - #include <ast.hh>
Public Functions
-
inline
Power(Expression *l, Expression *r)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
Quad¶ - #include <codegen.hh>
Public Functions
-
void
print_c(ostream &o)¶
-
inline
Quad(tQuadType o, SymbolInformation *a, SymbolInformation *b, SymbolInformation *c)¶
-
inline
Quad(tQuadType o, long a, SymbolInformation *b, SymbolInformation *c)¶
-
inline
Quad(tQuadType o, SymbolInformation *a, long b, SymbolInformation *c)¶
-
inline
Quad(tQuadType o, double a, SymbolInformation *b, SymbolInformation *c)¶
Public Members
-
SymbolInformation *
sym1¶
-
SymbolInformation *
sym2¶
-
SymbolInformation *
sym3¶
-
long
int1¶
-
long
int2¶
-
long
int3¶
-
double
real1¶
-
double
real2¶
-
double
real3¶
Private Functions
-
ostream &
print(ostream&)¶
-
void
-
class
QuadsList¶ - #include <codegen.hh>
Private Functions
-
ostream &
print(ostream&)¶
Private Static Attributes
-
static long
labelCounter¶
-
ostream &
-
class
QuadsListIterator¶ - #include <codegen.hh>
Private Members
-
QuadsList::QuadsListElement *
elem¶
-
QuadsList::QuadsListElement *
-
class
RealConstant: public Expression¶ - #include <ast.hh>
Public Functions
-
inline
RealConstant(double v)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Public Members
-
double
value¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
ReturnStatement: public Statement¶ - #include <ast.hh>
Public Functions
-
inline
ReturnStatement()¶
-
inline
ReturnStatement(Expression *e)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Public Members
-
Expression *
value¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
Statement: public ASTNode¶ - #include <ast.hh>
Subclassed by Assignment, CallStatement, IfStatement, ReturnStatement, WhileStatement
Public Functions
-
virtual VariableInformation *
GenerateCode(QuadsList &q) = 0¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
virtual VariableInformation *
-
class
StatementList: public ASTNode¶ - #include <ast.hh>
Public Functions
-
inline
StatementList(StatementList *l, Statement *s)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
string¶ - #include <string.hh>
Public Functions
-
string()¶
-
string(char*)¶
-
string(const char*)¶
-
string(char, int)¶
-
string(int)¶
-
~string()¶
-
void
upcase(void)¶
-
void
downcase(void)¶
-
unsigned long
hash(void) const¶
-
int
length(void) const¶
-
char &
operator[](int)¶
-
const char
operator[](const int) const¶
Private Functions
-
void
ensure_size(int)¶
Friends
-
-
class
SymbolInformation¶ - #include <symtab.hh>
Subclassed by FunctionInformation, TypeInformation, VariableInformation
Public Types
Public Functions
-
inline
SymbolInformation(SymbolInformationType t, const ::string &i)¶
-
inline virtual
~SymbolInformation()¶
-
inline virtual FunctionInformation *
SymbolAsFunction(void)¶
-
inline virtual VariableInformation *
SymbolAsVariable(void)¶
-
inline virtual TypeInformation *
SymbolAsType(void)¶
Public Static Attributes
-
static tFormatType
outputFormat= = SymbolInformation::kFullFormat¶
Protected Functions
-
virtual ostream &
print(ostream&)¶
Friends
- friend class SymbolTable
-
friend ostream &
LongSymbols(ostream &o)¶
-
friend ostream &
SummarySymbols(ostream &o)¶
-
friend ostream &
ShortSymbols(ostream &o)¶
-
friend ostream &
operator<<(ostream &o, SymbolInformation &i)¶
-
friend ostream &
operator<<(ostream &o, SymbolInformation *i)¶
-
inline
-
class
SymbolTable¶ - #include <symtab.hh>
Public Functions
-
SymbolTable()¶
-
void
AddSymbol(SymbolInformation*)¶
-
SymbolInformation *
LookupSymbol(const ::string&)¶
Public Static Attributes
-
static int
nextTemporary¶
Protected Functions
-
virtual ostream &
print(ostream&)¶
Friends
-
friend ostream &
operator<<(ostream &o, SymbolTable &t)¶
-
friend ostream &
operator<<(ostream &o, SymbolTable *t)¶
-
-
class
SymbolTableElement¶ - #include <symtab.hh>
-
class
Times: public BinaryOperation¶ - #include <ast.hh>
Public Functions
-
inline
Times(Expression *l, Expression *r)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
TruncateReal: public Expression¶ - #include <ast.hh>
Public Functions
-
inline
TruncateReal(Expression *e)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Public Members
-
Expression *
value¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
TypeInformation: public SymbolInformation¶ - #include <symtab.hh>
Public Functions
-
inline virtual TypeInformation *
SymbolAsType(void)¶
Protected Functions
-
virtual ostream &
print(ostream&)¶
Friends
- friend class SymbolTable
- friend class FunctionInformation
- friend class VariableInformation
-
inline virtual TypeInformation *
-
class
UnaryMinus: public Expression¶ - #include <ast.hh>
Public Functions
-
inline
UnaryMinus(Expression *e)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Public Members
-
Expression *
right¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
class
VariableInformation: public SymbolInformation¶ - #include <symtab.hh>
Public Functions
-
inline virtual VariableInformation *
SymbolAsVariable(void)¶
-
inline
VariableInformation(const ::string &i, TypeInformation *t)¶
Protected Functions
-
virtual ostream &
print(ostream&)¶
-
inline virtual VariableInformation *
-
class
WhileStatement: public Statement¶ - #include <ast.hh>
Public Functions
-
inline
WhileStatement(Condition *c, StatementList *b)¶
-
virtual VariableInformation *
GenerateCode(QuadsList &q)¶
Protected Functions
-
virtual void
print(ostream &o)¶
-
inline
-
namespace
std
-
file
ast.cc - #include “ast.hh”
-
file
ast.hh - #include “symtab.hh”#include “codegen.hh”
Enums
-
enum
ASTNodeType¶ Values:
-
enum
-
file
codegen.cc - #include <iostream>#include <iomanip>#include <stdlib.h>#include “ast.hh”#include “symtab.hh”#include “codegen.hh”
Defines
-
USEQ¶
Functions
-
static VariableInformation *
BinaryGenerateCode(QuadsList &q, tQuadType realop, tQuadType intop, ASTNode *left, ASTNode *right, ASTNode *node, TypeInformation *type = NULL)¶
-
-
file
codegen.hh Enums
-
enum
tQuadType¶ Values:
-
enumerator
iconst¶
-
enumerator
rconst¶
-
enumerator
iaddr¶
-
enumerator
itor¶
-
enumerator
rtrunc¶
-
enumerator
iadd¶
-
enumerator
isub¶
-
enumerator
imul¶
-
enumerator
idiv¶
-
enumerator
ipow¶
-
enumerator
radd¶
-
enumerator
rsub¶
-
enumerator
rmul¶
-
enumerator
rdiv¶
-
enumerator
rpow¶
-
enumerator
igt¶
-
enumerator
ilt¶
-
enumerator
ieq¶
-
enumerator
rgt¶
-
enumerator
rlt¶
-
enumerator
req¶
-
enumerator
iand¶
-
enumerator
ior¶
-
enumerator
inot¶
-
enumerator
jtrue¶
-
enumerator
jfalse¶
-
enumerator
jump¶
-
enumerator
clabel¶
-
enumerator
istore¶
-
enumerator
iload¶
-
enumerator
rstore¶
-
enumerator
rload¶
-
enumerator
creturn¶
-
enumerator
param¶
-
enumerator
call¶
-
enumerator
iassign¶
-
enumerator
rassign¶
-
enumerator
aassign¶
-
enumerator
hcf¶
-
enumerator
nop¶
-
enumerator
-
enum
-
file
main.cc - #include <stdlib.h>#include <stdio.h>#include <iostream>#include “ast.hh”#include “parser.hh”#include “symtab.hh”#include <unistd.h>
-
file
README.md
-
file
README.md
-
file
stl.h - #include <math.h>#include <stdio.h>
-
file
string.cc - #include <stdlib.h>#include <malloc.h>#include <cstring>#include <ctype.h>#include <strstream>#include “string.hh”
Defines
-
MAX(a, b)¶
Functions
-
-
file
string.hh - #include <iostream>
-
file
symtab.cc - #include <stdlib.h>#include “symtab.hh”#include “ast.hh”#include “string.hh”
Functions
-
ostream &
SummarySymbols(ostream &o)¶
-
ostream &
operator<<(ostream &o, SymbolTable &t)¶
-
ostream &
operator<<(ostream &o, SymbolTable *t)¶
-
ostream &
operator<<(ostream &o, SymbolInformation &i)¶
-
ostream &
operator<<(ostream &o, SymbolInformation *i)¶
-
ostream &
ShortSymbols(ostream &o)¶
-
ostream &
LongSymbols(ostream &o)¶
-
ostream &
CFormat(ostream &o)¶
Variables
-
FunctionInformation *
currentFunction¶
-
TypeInformation *
kIntegerType¶
-
TypeInformation *
kRealType¶
-
FunctionInformation *
kFPrintFunction¶
-
FunctionInformation *
kIPrintFunction¶
-
FunctionInformation *
kFReadFunction¶
-
FunctionInformation *
kIReadFunction¶
-
ostream &
-
file
symtab.hh - #include <iostream>#include “string.hh”
Enums
Functions
-
ostream &
ShortSymbols(ostream &o)
-
ostream &
LongSymbols(ostream &o)
-
ostream &
CFormat(ostream &o)
Variables
-
FunctionInformation *
currentFunction
-
FunctionInformation *
kFPrintFunction
-
FunctionInformation *
kIPrintFunction
-
FunctionInformation *
kFReadFunction
-
FunctionInformation *
kIReadFunction
-
TypeInformation *
kRealType
-
TypeInformation *
kIntegerType
-
ostream &
-
dir
/home/adrpo33/teaching/tdde66/ai/webpage/tddd55-lab/lab3-4 Code Skeleton for Lab 3 and Lab 4¶
This folder contains the code skeleton that you will be working on during Lab 3 and Lab 4.
Lab 3¶
For Lab 3 two files are of interest,
parser.yandscanner.l.To start, begin by integrating what you did in lab 1 to the
scanner.lfile. Here it is important that you are careful, and that you do not simply copy paste. Look at the structure ofscanner.land enter your rules s.t you do not remove any includes present in that file.Following this a good starting point for Lab 3 is to start by implementing support for the different expressions:
/* --- Your code here --- * * Insert the expression grammar here * The start symbol of the expression grammar is * expression. This is important since it's used * in a number of other places. * * Make sure that your code creates itor nodes in the * AST wherever necessary and that it only created * trees for expressions with compatible types! */ expression : { cerr << "Expression here" << endl; } ;
Followed by the different rules for the conditions of the language:
/* --- Your code here --- * * Insert the condition grammar here * The start symbol is condition. It's used * elsewhere, so make sure you get it right. * The boolean constants false and true are represented using the BooleanConstant AST-node. * The other logical relations are subclasses of BinaryRelation. * See the file ast.hh for the declarations. */ condition : { cerr << "Condition here" << endl; } ; /* --- End your code --- */
Testing Lab 3.¶
There is a number of test files in the
testdirectory that you may use to check if your parser works as expected.We also provide traces that you can use to compare the output of your solution to some expected output. To do this you can exceute the following sequence of commands. To check if you get about the same result for expressions you can run:
./compiler ./test/expression_test.prog > my_output.txt diff -y my_output.txt ../traces/trace_expression_test.txt
The same procedure can be done for the more advanced test, that is
traces/trace-lab3.txt./compiler ./test/test > my_output.txt diff -y my_output.txt ../traces/trace-lab3.txt
Lab 4¶
In Lab 4 you will work in file
codegen.ccsearch for/* --- Your code here --- */’ to see where you need to introduce your changes.A good place to start is the function
BinaryGenerateCode/* * * This function is used to generate code for all kinds of binary * operators and relations. The arguments to the function are the * following: * * q The QuadsList onto which the generated code is placed. * realop The quad to generate for the operator if the arguments * are of type real. * intop The quad to generate for the operator if the arguments * are of type integer. * left The AST for the left-hand side of the operator. The * value type of this AST must be the same as that of the * AST for the right-hand side. * right The AST for the right-hand side of the operator. The * value type of this AST must be the same as that of the * AST for the left-hand side. * node The operator node itself. You probably won't need this * unless you want to print an error message. * type If not NULL, this is the type of the result. This is * used for relations, where the type of the result is * always integer, even if the operands are real. If this * parameter is NULL, then the type of the result is the * same as the type of the operands (the parser needs to * make sure that the operands have the same type.) * * See the GenerateCode methods for the binary operators for * examples of how this function is used. * */ static VariableInformation *BinaryGenerateCode(QuadsList& q, tQuadType realop, tQuadType intop, ASTNode *left, ASTNode *right, ASTNode *node, TypeInformation *type = NULL) { /* --- Your code here --- */ return NULL; /* --- End your code --- */ }
Testing Lab 4¶
In order to test Lab 4, there exists a test script. For more details see the README in the test folder.
Other notes¶
For more exhaustive documentation see The Lab Skeleton Documentation
-
dir
/home/adrpo33/teaching/tdde66/ai/webpage/tddd55-lab/lab3-4/test This file contains various files for testing¶
expression_test.prog¶
This program simple executes and print simple expressions
factorial_test.prog¶
This program checks if functions and if/else statements are handled correctly. Checks the factorial function
fibonacci_test.prog¶
Similar to the factorial test, also tests recursions
sort_test.prog¶
This program simple executes selection sort on an unsorted array
*output_ref¶
These are references files specifing the expected output from the programs above. It is used by codegen-test.sh
How to test?¶
To test a program simply execute codegen-test.sh in the parent directory.
bash codegen-test.sh
How do I compile my own program?¶
Invoke the compiler. Pipe the output to a file with the suffix.c
Do not use the -d flag when doing this.compiler -c > <name>.c
Then compile this file with
gcc -lm <name.c>