Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 01-scanners/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enum Token {
TOKEN_RIGHT,
TOKEN_EXIT,
TOKEN_ERROR,
TOKEN_INT
};


Expand Down
11 changes: 9 additions & 2 deletions 01-scanners/lexer.hh
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
#pragma once

#include "Commands.h"

struct Command {
Token command;
int steps;
};

#if ! defined(yyFlexLexerOnce)
#include <FlexLexer.h>
#endif

#undef YY_DECL
#define YY_DECL int Lexer::ScanToken()
#define YY_DECL Command* Lexer::ScanToken()

// ScanToken is function that calls Scanner; It returns int values, we will use enums!
class Lexer: public yyFlexLexer {
public:
virtual ~Lexer() {}
// virtual int yylex();
virtual int ScanToken();
virtual Command* ScanToken();
};
56 changes: 49 additions & 7 deletions 01-scanners/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,58 @@

%}

int [0-9]+


%%
[ \t\r\n]+ {}

h|help { return TOKEN_HELP; }
up|u { std::cout << "up" << std::endl; return TOKEN_UP; }
down|d { std::cout << "down" << std::endl; return TOKEN_DOWN; }
left|l { std::cout << "left" << std::endl; return TOKEN_LEFT; }
right|r { std::cout << "right" << std::endl; return TOKEN_RIGHT; }
exit|e { return TOKEN_EXIT; }
h|help {
Command* command = new Command();
command->command = TOKEN_HELP;
return command;
}
up|u {
std::cout << "up" << std::endl;
Command* command = new Command();
command->command = TOKEN_UP;
return command;
}
down|d {
std::cout << "down" << std::endl;
Command* command = new Command();
command->command = TOKEN_DOWN;
return command;
}
left|l {
std::cout << "left" << std::endl;
Command* command = new Command();
command->command = TOKEN_LEFT;
return command;
}
right|r {
std::cout << "right" << std::endl;
Command* command = new Command();
command->command = TOKEN_RIGHT;
return command;
}
exit|e {
Command* command = new Command();
command->command = TOKEN_EXIT;
return command;
}

{int} {
std::cout << yytext << std::endl;
Command* command = new Command();
command->command = TOKEN_EXIT;
command->steps = std::stoi(yytext);
return command;
}

.* { std::cout << "ERROR " << yytext << std::endl; return TOKEN_ERROR; }
.* { std::cout << "ERROR " << yytext << std::endl;
Command* command = new Command();
command->command = TOKEN_ERROR;
return command;
}
%%
14 changes: 9 additions & 5 deletions 01-scanners/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ int main() {
Lexer lexer;
lexer.set_debug(true);
while (true) {
int token = lexer.ScanToken();
if (token == TOKEN_EXIT) {
Command* token = lexer.ScanToken();

if (token->command == TOKEN_EXIT) {
std::cout << "Good bye" << std::endl;
return 0;
} else if (token == TOKEN_HELP) {
PrintHelp();
} else if (token->command == TOKEN_HELP) {
PrintHelp();
} else {
Command* new_token = lexer.ScanToken();
std::cout << "Steps" << new_token->steps << std::endl;
}
std::cout << token << std::endl;
std::cout << token->command << std::endl;
}
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions 02-parsers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ BISON_TARGET(
parser.y
${ParserExample_SOURCE_DIR}/parser.cpp
COMPILE_FLAGS --graph
COMPILE_FLAGS -Wcounterexamples
DEFINES_FILE ${ParserExample_SOURCE_DIR}/parser.hh
)

Expand Down
18 changes: 12 additions & 6 deletions 02-parsers/parser.y
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
%skeleton "lalr1.cc"
%require "3.5"

%defines
/* %defines */
%define api.token.constructor
%define api.value.type variant
%define parse.assert
%define parse.trace
%define parse.error verbose
%define api.token.prefix {TOK_}

%code requires {
#include <string>
Expand All @@ -13,11 +16,9 @@
class Driver;
}


%define parse.trace
%define parse.error verbose

// Code as start of parser.cpp
%code {

#include "driver.hh"
#include "location.hh"

Expand All @@ -28,13 +29,16 @@
}

%lex-param { Scanner &scanner }
/* %lex-param { int x } */

// Params to parser constructor
%parse-param { Scanner &scanner }
%parse-param { Driver &driver }
/* %parse-param { int x} */

%locations

%define api.token.prefix {TOK_}

// token name in variable
%token
END 0 "end of file"
Expand All @@ -48,9 +52,11 @@
SEMICOLON ";"
;

// basic_symbol
%token <std::string> IDENTIFIER "identifier"
%token <int> NUMBER "number"
%nterm <int> exp
/* %nterm <Expression*> exp */

// Prints output in parsing option for debugging location terminal
%printer { yyo << $$; } <*>;
Expand Down
26 changes: 12 additions & 14 deletions 02-parsers/scanner.l
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
%option noyywrap noinput batch debug

%option c++
%option yyclass="Scanner"

%{
#include <cerrno>
#include <climits>
Expand All @@ -7,38 +12,31 @@
#include <iostream>
#include "driver.hh"
#include "parser.hh"
%}

%option noyywrap nounput noinput batch debug

%option c++
%option yyclass="Scanner"

%{
// Code definitions at the end of scanner.cpp

// A number symbol corresponding to the value in S.
yy::parser::symbol_type make_NUMBER(
yy::parser::symbol_type CastToNumber(
const std::string &s,
const yy::parser::location_type& loc
);

void Scanner::UpdateLocation() {
std::cout << "Token: " << yytext << " " << yyleng << std::endl;
if (driver.location_debug) {
std::cerr << "Action called " << driver.location << std::endl;
}
driver.location.columns(yyleng);
}

// Code run each time a pattern is matched.
#define YY_USER_ACTION UpdateLocation();
%}

id [a-zA-Z][a-zA-Z_0-9]*
int [0-9]+
blank [ \t\r]

%{
// Code run each time a pattern is matched.
#define YY_USER_ACTION UpdateLocation();
%}

%%

Expand Down Expand Up @@ -81,7 +79,7 @@ blank [ \t\r]
":=" return yy::parser::make_ASSIGN (loc);
";" return yy::parser::make_SEMICOLON(loc);

{int} return make_NUMBER(yytext, loc);
{int} return CastToNumber(yytext, loc);
{id} {
if (driver.location_debug) {
std::cerr << "ID found " << yytext << std::endl;
Expand All @@ -94,7 +92,7 @@ blank [ \t\r]
<<EOF>> return yy::parser::make_END (loc);
%%

yy::parser::symbol_type make_NUMBER(
yy::parser::symbol_type CastToNumber(
const std::string &s,
const yy::parser::location_type& loc
) {
Expand Down
5 changes: 3 additions & 2 deletions 03-parsers-with-ast/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ int Driver::parse(const std::string& f) {
}

void Driver::scan_begin() {
scanner.set_debug(trace_scanning);
if (file.empty () || file == "-") {
scanner.set_debug(trace_scanning);
if (file.empty() || file == "-") {

} else {
stream.open(file);
std::cout << file << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions 03-parsers-with-ast/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ blank [ \t\r]
loc.step();
%}

{blank}+ loc.step ();
\n+ loc.lines (yyleng); loc.step ();
{blank}+ {loc.step ();}
\n+ {loc.lines (yyleng); loc.step ();}

"-" {
std::cout << loc.begin.line << " " << loc.end.line << std::endl;
Expand Down
1 change: 1 addition & 0 deletions 04-visitors/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ add_executable(
expressions/IdentExpression.cpp
assignments/Assignment.cpp
assignments/AssignmentList.cpp
assignments/PrintStatement.cpp
visitors/Interpreter.cpp
visitors/PrintVisitor.cpp
Program.cpp
Expand Down
7 changes: 6 additions & 1 deletion 04-visitors/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@
Program::Program(
AssignmentList* assignments,
Expression* expression
): assignments_(assignments), expression_(expression) {}
): assignments_(assignments), expression_(expression) {}


void Program::Accept(Visitor *v) {
v->Visit(this);
}
6 changes: 4 additions & 2 deletions 04-visitors/Program.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include "assignments/AssignmentList.h"
#include "expressions/Expression.h"

class Program {
class Program: BaseElement {
public:
Program(AssignmentList* assignments, Expression* expression);
AssignmentList* assignments_;
AssignmentList* assignments_;
Expression* expression_;

void Accept(Visitor* visitor) override;
};
4 changes: 2 additions & 2 deletions 04-visitors/assignments/Assignment.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once

#include "expressions/Expression.h"
#include "base_elements/BaseElement.h"
#include "Statement.h"

#include <string>

class Assignment: public BaseElement {
class Assignment: public Statement {
public:
Assignment(const std::string& variable, Expression* expression);
void Accept(Visitor* visitor);
Expand Down
2 changes: 1 addition & 1 deletion 04-visitors/assignments/AssignmentList.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "AssignmentList.h"


void AssignmentList::AddAssignment(Assignment* assignment) {
void AssignmentList::AddAssignment(Statement* assignment) {
assignments_.push_back(assignment);
}

Expand Down
6 changes: 3 additions & 3 deletions 04-visitors/assignments/AssignmentList.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#pragma once

#include "assignments/Assignment.h"
#include "assignments/Statement.h"
#include "base_elements/BaseElement.h"
#include <vector>

class AssignmentList : public BaseElement {
public:
void AddAssignment(Assignment* assignment);
void AddAssignment(Statement* statement);
void Accept(Visitor* visitor);

std::vector<Assignment*> assignments_;
std::vector<Statement*> assignments_;
};
10 changes: 10 additions & 0 deletions 04-visitors/assignments/PrintStatement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "PrintStatement.h"

PrintStatement::PrintStatement(
Expression* expression
) : expression_(expression) {}


void PrintStatement::Accept(Visitor* visitor) {
visitor->Visit(this);
}
16 changes: 16 additions & 0 deletions 04-visitors/assignments/PrintStatement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "expressions/Expression.h"
#include "base_elements/BaseElement.h"

#include <string>

#include "Statement.h"

class PrintStatement: public Statement {
public:
PrintStatement(Expression* expression);
void Accept(Visitor* visitor);

Expression* expression_;
};
1 change: 1 addition & 0 deletions 04-visitors/assignments/Statement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Loading