Source Documentation

Lab2

class Parser
#include <lab2.hh>

Public Functions

void Recover(void)
double Parse(void)

Public Members

Scanner scanner
class ParserEndOfFile
#include <lab2.hh>
class ParserError
#include <lab2.hh>
class Result
class Scanner
#include <lex.hh>

Public Functions

Token Scan(void)
inline Scanner()

Private Functions

void Reset(void)
void Accumulate(char)
void PutbackChar(unsigned char)
char *SymbolValue(void)
double NumberValue(void)
char GetChar(void)

Private Members

int state
int haveBuffered
unsigned char bufferedChar
char valueBuffer[kMaxTokenLength + 1]
long position
class ScannerError
#include <lex.hh>

Public Functions

inline ScannerError(char c, int s)
inline ScannerError(const char *s)
inline ScannerError()

Public Members

char errorCharacter
const char *message
int state
class Token
#include <lex.hh>

Public Functions

const char *Lookup(TokenType)
const char *Lookup(void)
inline Token()
inline Token(TokenType t)
inline Token(TokenType t, double x)
inline Token(TokenType t, char *s)

Public Members

TokenType type
double numberValue
char *symbolValue
class Trace
#include <lab2.hh>

Public Functions

inline Trace(const char *s)
inline ~Trace()

Private Members

const char *name

Private Static Attributes

static int indent = 0
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, Token &t)
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"}
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

Functions

ostream &operator<<(ostream&, Token&)
ostream &operator<<(ostream&, ScannerError&)

Variables

const int kMaxTokenLength = 1024
file main.cc
#include <iostream>#include <stdlib.h>#include “lab2.hh”#include “lex.hh”

Functions

int main(void)
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:

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: 
c++ int main(void) { Parser parser; double val;

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

inline And(Condition *l, Condition *r)
virtual VariableInformation *GenerateCode(QuadsList &q)

Protected Functions

virtual void print(ostream &o)
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)

Public Members

VariableInformation *id
Expression *index

Protected Functions

virtual void print(ostream &o)
class Assignment : public Statement
#include <ast.hh>

Public Functions

inline Assignment(LeftValue *l, Expression *r)
virtual VariableInformation *GenerateCode(QuadsList &q)

Public Members

LeftValue *target
Expression *value

Protected Functions

virtual void print(ostream &o)
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)

Protected Functions

void indent(ostream &o)
void indentMore(void)
void indentLess(void)
void beginChild(ostream &o)
void endChild(ostream &o)
void lastChild(ostream &o)
virtual void print(ostream &o)
virtual void xprint(ostream &o, const char *cls)

Protected Static Attributes

static int indentLevel = 0
static bool branches[10000]

Friends

friend ostream &operator<<(ostream &o, ASTNode &node)
friend ostream &operator<<(ostream &o, ASTNode *node)
class BinaryCondition : public Condition
#include <ast.hh>

Subclassed by And, Or

Public Functions

inline BinaryCondition(Condition *l, Condition *r)
virtual VariableInformation *GenerateCode(QuadsList &q) = 0

Public Members

Condition *left
Condition *right

Protected Functions

virtual void print(ostream &o)
virtual void xprint(ostream &o, const char *cls)
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

Public Members

Expression *left
Expression *right

Protected Functions

virtual void print(ostream &o)
virtual void xprint(ostream &o, const char*)
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

Public Members

Expression *left
Expression *right

Protected Functions

virtual void print(ostream &o)
virtual void xprint(ostream &o, const char *cls)
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)
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)
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)
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)
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)

Public Members

ElseIfList *preceding
Condition *condition
StatementList *body

Protected Functions

virtual void print(ostream &o)
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)
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)
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)

Public Members

ExpressionList *precedingExpressions
Expression *expression

Protected Functions

virtual void print(ostream &o)
class FunctionCall : public Expression
#include <ast.hh>

Public Functions

inline FunctionCall(FunctionInformation *f, ExpressionList *a)
virtual VariableInformation *GenerateCode(QuadsList &q)

Public Members

FunctionInformation *function
ExpressionList *arguments

Protected Functions

virtual void print(ostream &o)
class FunctionInformation : public SymbolInformation
#include <symtab.hh>

Public Functions

inline FunctionInformation(const ::string &i)
inline virtual FunctionInformation *SymbolAsFunction(void)
void SetParent(FunctionInformation*)
void SetReturnType(TypeInformation*)
void SetBody(StatementList*)
void SetQuads(QuadsList*)
FunctionInformation *GetParent(void)
TypeInformation *GetReturnType(void)
VariableInformation *GetLastParam(void)
StatementList *GetBody(void)
QuadsList *GetQuads(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)
char OkToAddSymbol(const ::string&)
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
QuadsList *quads

Friends

friend class SymbolTable
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)
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)
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

VariableInformation *id

Protected Functions

virtual void print(ostream &o)
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

Condition *condition
StatementList *thenStatements
ElseIfList *elseIfList
StatementList *elseStatements

Protected Functions

virtual void print(ostream &o)
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)
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)
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)
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)
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)
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)
class Not : public Condition
#include <ast.hh>

Public Functions

inline Not(Condition *r)
virtual VariableInformation *GenerateCode(QuadsList &q)

Public Members

Condition *right

Protected Functions

virtual void print(ostream &o)
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)
class Or : public BinaryCondition
#include <ast.hh>

Public Functions

inline Or(Condition *l, Condition *r)
virtual VariableInformation *GenerateCode(QuadsList &q)

Protected Functions

virtual void print(ostream &o)
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)
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)
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

tQuadType opcode
SymbolInformation *sym1
SymbolInformation *sym2
SymbolInformation *sym3
long int1
long int2
long int3
double real1
double real2
double real3

Private Functions

ostream &print(ostream&)

Friends

friend ostream &operator<<(ostream &o, Quad *q)
friend ostream &operator<<(ostream &o, Quad &q)
class QuadsList
#include <codegen.hh>

Public Functions

void print_c(ostream&)
inline QuadsList()
QuadsList &operator+=(Quad *q)
inline long NextLabel(void)

Private Functions

ostream &print(ostream&)

Private Members

QuadsListElement *head
QuadsListElement *tail

Private Static Attributes

static long labelCounter

Friends

friend class QuadsListIterator
friend ostream &operator<<(ostream &o, QuadsList *q)
friend ostream &operator<<(ostream &o, QuadsList &q)
class QuadsList::QuadsListElement

Public Functions

inline QuadsListElement(Quad *d, QuadsListElement *n)
inline ~QuadsListElement()

Public Members

Quad *data
QuadsListElement *next
class QuadsListIterator
#include <codegen.hh>

Public Functions

inline QuadsListIterator(QuadsList *ql)
inline Quad *Next(void)

Private Members

QuadsList::QuadsListElement *elem
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)
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)
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)
class StatementList : public ASTNode
#include <ast.hh>

Public Functions

inline StatementList(StatementList *l, Statement *s)
virtual VariableInformation *GenerateCode(QuadsList &q)

Public Members

Statement *statement
StatementList *precedingStatements

Protected Functions

virtual void print(ostream &o)
class string
#include <string.hh>

Public Functions

string()
string(char*)
string(const char*)
string(char, int)
string(const string&)
string(int)
~string()
void upcase(void)
void downcase(void)
string &operator=(const string&)
string &operator=(const char*)
string &operator=(const char)
string &operator+=(const string&)
string &operator+=(const char)
string &operator+=(const char*)
unsigned long hash(void) const
int length(void) const
char &operator[](int)
const char operator[](const int) const

Private Functions

void ensure_size(int)

Private Members

char *text
int size
int position
int chunk_size

Friends

friend string &operator+(const string &s1, const string &s2)
friend string &operator+(const string &s1, const char c)
friend string &operator+(const string &s1, const int i)
friend int operator==(const string &s1, const string &s2)
friend int operator!=(const string &s1, const string &s2)
friend int operator<(const string &s1, const string &s2)
friend int operator>(const string &s1, const string &s2)
friend int operator>=(const string &s1, const string &s2)
friend int operator<=(const string &s1, const string &s2)
friend ostream &operator<<(ostream &o, const string &s)
friend ostream &operator<<(ostream &o, const string *s)
class SymbolInformation
#include <symtab.hh>

Subclassed by FunctionInformation, TypeInformation, VariableInformation

Public Types

enum tFormatType

Values:

enumerator kFullFormat
enumerator kSummaryFormat
enumerator kShortFormat
enumerator kCFormat

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 Members

SymbolInformationType tag
::string id
SymbolTable *table

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)
class SymbolTable
#include <symtab.hh>

Public Functions

SymbolTable()
void AddSymbol(SymbolInformation*)
SymbolInformation *LookupSymbol(const ::string&)

Public Members

SymbolTableElement **table
int tableSize

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>

Public Members

SymbolInformation *info
SymbolTableElement *next
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)
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)
class TypeInformation : public SymbolInformation
#include <symtab.hh>

Public Functions

inline virtual TypeInformation *SymbolAsType(void)
inline TypeInformation(const ::string &i, unsigned long s)

Public Members

TypeInformation *elementType
int arrayDimensions
unsigned long size

Protected Functions

virtual ostream &print(ostream&)

Friends

friend class SymbolTable
friend class FunctionInformation
friend class VariableInformation
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)
class VariableInformation : public SymbolInformation
#include <symtab.hh>

Public Functions

inline virtual VariableInformation *SymbolAsVariable(void)
inline VariableInformation(const ::string &i)
inline VariableInformation(const ::string &i, TypeInformation *t)

Public Members

TypeInformation *type
VariableInformation *prev

Protected Functions

virtual ostream &print(ostream&)
class WhileStatement : public Statement
#include <ast.hh>

Public Functions

inline WhileStatement(Condition *c, StatementList *b)
virtual VariableInformation *GenerateCode(QuadsList &q)

Public Members

Condition *condition
StatementList *body

Protected Functions

virtual void print(ostream &o)
namespace std
file ast.cc
#include “ast.hh”

Functions

ostream &operator<<(ostream &o, ASTNode *node)
ostream &operator<<(ostream &o, ASTNode &node)
file ast.hh
#include “symtab.hh”#include “codegen.hh”

Enums

enum ASTNodeType

Values:

Functions

ostream &operator<<(ostream &o, ASTNode &node)
ostream &operator<<(ostream &o, ASTNode *node)
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)
ostream &operator<<(ostream &o, QuadsList *q)
ostream &operator<<(ostream &o, QuadsList &q)
ostream &operator<<(ostream &o, Quad *q)
ostream &operator<<(ostream &o, Quad &q)
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
file main.cc
#include <stdlib.h>#include <stdio.h>#include <iostream>#include “ast.hh”#include “parser.hh”#include “symtab.hh”#include <unistd.h>

Functions

int yyparse(void)
void Usage(char *program)
int main(int argc, char **argv)

Variables

int yydebug
int errorCount
int warningCount
static const char *OPTION_STRING = "dhc"
file README.md
file README.md
file stl.h
#include <math.h>#include <stdio.h>

Defines

array_real(X)
array_integer(X)
real
integer
conditional
bool

Functions

integer putint(long x)
integer putreal(double x)
double getreal()
double getint()
integer putline()
file string.cc
#include <stdlib.h>#include <malloc.h>#include <cstring>#include <ctype.h>#include <strstream>#include “string.hh”

Defines

MAX(a, b)

Functions

string &operator+(const string &s1, const string &s2)
string &operator+(const string &s1, const char c)
string &operator+(const string &s1, const int i)
int operator==(const string &s1, const string &s2)
int operator!=(const string &s1, const string &s2)
int operator>=(const string &s1, const string &s2)
int operator<=(const string &s1, const string &s2)
int operator>(const string &s1, const string &s2)
int operator<(const string &s1, const string &s2)
ostream &operator<<(ostream &o, const string &s)
ostream &operator<<(ostream &o, const string *s)
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
file symtab.hh
#include <iostream>#include “string.hh”

Enums

enum SymbolInformationType

Values:

enumerator kFunctionInformation
enumerator kVariableInformation
enumerator kTypeInformation

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
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.y and scanner.l.

To start, begin by integrating what you did in lab 1 to the scanner.l file. Here it is important that you are careful, and that you do not simply copy paste. Look at the structure of scanner.l and 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 test directory 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.cc search 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

compiler -c > <name>.c 
Do not use the -d flag when doing this.

Then compile this file with

gcc -lm <name.c>