Skip to content

Commit

Permalink
Rewrote the parser to have two lookahead tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabergia committed Dec 26, 2014
1 parent 0153b9a commit 2babdf9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
20 changes: 14 additions & 6 deletions sources/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@
Parser::Parser(std::istream& istream, std::ostream &ostream)
:lexer_(istream), ostream_(ostream)
{
advance();
for (int i = 0; i < NUM_LOOK_AEAHD_TOKENS; ++i) {
if (lexer_.hasNextToken()) {
nextTokens_[i] = lexer_.nextToken();
}
}
}

void Parser::advance()
{
if (lexer_.hasNextToken()) {
nextToken_ = lexer_.nextToken();
} else {
nextToken_ = Token(TokenType::END_OF_INPUT, "");
// Shift tokens one position back
for (int i = 0; i < NUM_LOOK_AEAHD_TOKENS - 1; ++i) {
nextTokens_[i] = nextTokens_[i + 1];
}
}

// Set last available token
nextTokens_[NUM_LOOK_AEAHD_TOKENS - 1] =
lexer_.hasNextToken()
? lexer_.nextToken()
: Token{TokenType::END_OF_INPUT, ""};
}

void Parser::parseProgram()
{
Expand Down
8 changes: 5 additions & 3 deletions sources/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ class Parser
double evalNextExpression();

private:
static const int NUM_LOOK_AEAHD_TOKENS = 2;

// A doubleToDoubleFunction is a pointer to a function taking a double and returning a double
using doubleToDoubleFunction = double(*)(double);

std::ostream &ostream_;
Lexer lexer_;
Token nextToken_;
Token nextTokens_[NUM_LOOK_AEAHD_TOKENS];
std::map<std::string, doubleToDoubleFunction> functions_ {
{"exp", std::exp},
{"log", std::log},
Expand All @@ -31,8 +33,8 @@ class Parser
{"tan", std::tan}
};

inline const Token &getNextToken() { return nextToken_; }
inline bool hasNextToken() const { return nextToken_.getTokenType() != TokenType::END_OF_INPUT; }
inline const Token &getNextToken() const { return nextTokens_[0]; }
inline bool hasNextToken() const { return getNextToken().getTokenType() != TokenType::END_OF_INPUT; }
void advance();

double evalNextTerm();
Expand Down

0 comments on commit 2babdf9

Please sign in to comment.