Question: #include #include #include #include #include #include #include #include #include / / = = = - - - - - - - - - - -

#include
#include
#include
#include
#include
#include
#include
#include
#include
//===----------------------------------------------------------------------===//
// Lexer
//===----------------------------------------------------------------------===//
// The lexer returns tokens [0-255] if it is an unknown character, otherwise one
// of these for known things.
enum Token {
tok_eof =-1,
// commands
tok_def =-2,
tok_extern =-3,
// primary
tok_identifier =-4,
tok_number =-5
};
static std::string IdentifierStr; // Filled in if tok_identifier
static double NumVal; // Filled in if tok_number
/// gettok - Return the next token from standard input.
static int gettok(){
static int LastChar ='';
// eat the input chars as it recognizes them and stores the last character read, but not processed, in LastChar. Skip any whitespace.
while (isspace(LastChar))
LastChar = getchar();
if (isalpha(LastChar)){// check if is an identifier: [a-zA-Z][a-zA-Z0-9]*
IdentifierStr = LastChar;
// fill in the code here
return tok_identifier;
}
if (isdigit(LastChar)|| LastChar =='.'){// check if is a number: [0-9.]+
// fill in the code here
return tok_number;
}
if (LastChar =='#'){// check if is a comment which starts with '#' and ends until end of line.
// fill in the code here
}
// Check for end of file. Don't eat the EOF.
if (LastChar == EOF)
return tok_eof;
// Otherwise, it is unknown, just return the character as its ascii value.
int ThisChar = LastChar;
LastChar = getchar();
return ThisChar;
}
////===----------------------------------------------------------------------===//
//// Main driver to test your lexer.
////===----------------------------------------------------------------------===//
// int main(){
//// Prime the first token.
// fprintf(stderr, "ready>");
// int tok_typ = gettok();
//// use ctrl + d to send the EOF signal and end this program
// while (true){
// fprintf(stderr, "token type>%d
", tok_typ);
// switch (tok_typ){
// case tok_eof:
// return 0;
// default:
// tok_typ = gettok();
// break;
//}
//}
//}
// the following code is for your next coding homework
//===----------------------------------------------------------------------===//
// Abstract Syntax Tree (aka Parse Tree)
//===----------------------------------------------------------------------===//
namespace {
/// ExprAST - Base class for all expression nodes.
class ExprAST {
public:
virtual ~ExprAST()= default;
};
/// NumberExprAST - Expression class for numeric literals like "1.0".
class NumberExprAST : public ExprAST {
double Val;
public:
NumberExprAST(double Val) : Val(Val){}
};
/// VariableExprAST - Expression class for referencing a variable, like "a".
class VariableExprAST : public ExprAST {
std::string Name;
public:
VariableExprAST(const std::string &Name) : Name(Name){}
};
/// BinaryExprAST - Expression class for a binary operator.
class BinaryExprAST : public ExprAST {
char Op;
std::unique_ptr LHS, RHS;
public:
BinaryExprAST(char Op, std::unique_ptr LHS,
std::unique_ptr RHS)
: Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)){}
};
/// CallExprAST - Expression class for function calls.
class CallExprAST : public ExprAST {
std::string Callee;
std::vector> Args;
public:
CallExprAST(const std::string &Callee,
std::vector> Args)
: Callee(Callee), Args(std::move(Args)){}
};
/// PrototypeAST - This class represents the "prototype" for a function,
/// which captures its name, and its argument names (thus implicitly the number
/// of arguments the function takes).
class PrototypeAST {
std::string Name;
std::vector Args;
public:
PrototypeAST(const std::string &Name, std::vector Args)
: Name(Name), Args(std::move(Args)){}
const std::string &getName() const { return Name; }
};
/// FunctionAST - This class represents a function definition itself.
class FunctionAST {
std::unique_ptr Proto;
std::unique_ptr Body;
public:
FunctionAST(std::unique_ptr Proto,
std::unique_ptr Body)
: Proto(std::move(Proto)), Body(std::move(Body)){}
};
}// end anonymous namespace
//===----------------------------------------------------------------------===//
// Parser
//===----------------------------------------------------------------------===//
/// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
/// token the parser is looking at. getNextToken reads another token from the
/// lexer and updates CurTok with its results.
static int CurTok;
static int getNextToken(){ return CurTok = gettok(); }
/// BinopPrecedence - Th

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!