Question: #include #include #include #include #include #include #include #include #include / / = = = - - - - - - - - - - -
#include
#include
#include
#include
#include
#include
#include
#include
#include
Lexer
The lexer returns tokens if it is an unknown character, otherwise one
of these for known things.
enum Token
tokeof
commands
tokdef
tokextern
primary
tokidentifier
toknumber
;
static std::string IdentifierStr; Filled in if tokidentifier
static double NumVal; Filled in if toknumber
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 isspaceLastChar
LastChar getchar;
if isalphaLastChar check if is an identifier: azAZazAZ
IdentifierStr LastChar;
fill in the code here
return tokidentifier;
if isdigitLastChar LastChar check if is a number:
fill in the code here
return toknumber;
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 tokeof;
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.
fprintfstderr "ready;
int toktyp gettok;
use ctrl d to send the EOF signal and end this program
while true
fprintfstderr "token typed
toktyp;
switch toktyp
case tokeof:
return ;
default:
toktyp 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
class NumberExprAST : public ExprAST
double Val;
public:
NumberExprASTdouble Val : ValVal
;
VariableExprAST Expression class for referencing a variable, like a
class VariableExprAST : public ExprAST
std::string Name;
public:
VariableExprASTconst std::string &Name : NameName
;
BinaryExprAST Expression class for a binary operator.
class BinaryExprAST : public ExprAST
char Op;
std::uniqueptr LHS RHS;
public:
BinaryExprASTchar Op std::uniqueptr LHS
std::uniqueptr RHS
: OpOp LHSstd::moveLHS RHSstd::moveRHS
;
CallExprAST Expression class for function calls.
class CallExprAST : public ExprAST
std::string Callee;
std::vector Args;
public:
CallExprASTconst std::string &Callee,
std::vector Args
: CalleeCallee Argsstd::moveArgs
;
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:
PrototypeASTconst std::string &Name, std::vector Args
: NameName Argsstd::moveArgs
const std::string &getName const return Name;
;
FunctionAST This class represents a function definition itself.
class FunctionAST
std::uniqueptr Proto;
std::uniqueptr Body;
public:
FunctionASTstd::uniqueptr Proto,
std::uniqueptr Body
: Protostd::moveProto Bodystd::moveBody
;
end anonymous namespace
Parser
CurTokgetNextToken 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
