Question: Creating a simple compiler, i really need someone to help me with this parse.cc--- #include #include #include parser.h using namespace std; void Parser::syntax_error() { cout
Creating a simple compiler, i really need someone to help me with this
parse.cc---
#include
#include
#include "parser.h"
using namespace std;
void Parser::syntax_error()
{
cout
exit(1);
}
// this function gets a token and checks if it is
// of the expected type. If it is, the token is
// returned, otherwise, synatx_error() is generated
// this function is particularly useful to match
// terminals in a right hand side of a rule.
Token Parser::expect(TokenType expected_type)
{
Token t = lexer.GetToken();
if (t.token_type != expected_type)
syntax_error();
return t;
}
// this function simply checks the next token without
// consuming the input
Token Parser::peek()
{
Token t = lexer.GetToken();
lexer.UngetToken(t);
return t;
}
// Parsing
//code here
int main()
{
LexicalAnalyzer lexer;
Token token;
token = lexer.GetToken();
token.Print();
while (token.token_type != END_OF_FILE)
{
token = lexer.GetToken();
token.Print();
}
}
parse.h--
#ifndef __PARSER_H__
#define __PARSER_H__
#include
#include "lexer.h"
class Parser {
private:
LexicalAnalyzer lexer;
void syntax_error();
Token expect(TokenType expected_type);
Token peek();
};
lexer.h--
#ifndef __LEXER__H__
#define __LEXER__H__
#include
#include
#include "inputbuf.h"
// ------- token types -------------------
typedef enum { END_OF_FILE = 0,
POLY, START, INPUT, EQUAL, LPAREN,
RPAREN, ID, COMMA, POWER, NUM,
PLUS, MINUS, SEMICOLON, ERROR
} TokenType;
class Token {
public:
void Print();
std::string lexeme;
TokenType token_type;
int line_no;
};
class LexicalAnalyzer {
public:
Token GetToken();
TokenType UngetToken(Token);
LexicalAnalyzer();
private:
std::vector
int line_no;
Token tmp;
InputBuffer input;
bool SkipSpace();
bool IsKeyword(std::string);
TokenType FindKeywordIndex(std::string);
Token ScanNumber();
Token ScanIdOrKeyword();
};
#endif //__LEXER__H__
lexer.cc--
#include
#include
#include
#include
#include
#include "lexer.h"
#include "inputbuf.h"
using namespace std;
string reserved[] = { "END_OF_FILE",
"POLY", "START", "INPUT", "EQUAL", "LPAREN",
"RPAREN", "ID", "COMMA", "POWER", "NUM",
"PLUS", "MINUS", "SEMICOLON", "ERROR"};
#define KEYWORDS_COUNT 3
string keyword[] = { "POLY", "START", "INPUT"};
void Token::Print()
{
cout lexeme
token_type]
line_no
}
LexicalAnalyzer::LexicalAnalyzer()
{
this->line_no = 1;
tmp.lexeme = "";
tmp.line_no = 1;
tmp.token_type = ERROR;
}
bool LexicalAnalyzer::SkipSpace()
{
char c;
bool space_encountered = false;
input.GetChar(c);
line_no += (c == ' ');
while (!input.EndOfInput() && isspace(c)) {
space_encountered = true;
input.GetChar(c);
line_no += (c == ' ');
}
if (!input.EndOfInput()) {
input.UngetChar(c);
}
return space_encountered;
}
bool LexicalAnalyzer::IsKeyword(string s)
{
for (int i = 0; i
if (s == keyword[i]) {
return true;
}
}
return false;
}
TokenType LexicalAnalyzer::FindKeywordIndex(string s)
{
for (int i = 0; i
if (s == keyword[i]) {
return (TokenType) (i + 1);
}
}
return ERROR;
}
Token LexicalAnalyzer::ScanNumber()
{
char c;
input.GetChar(c);
if (isdigit(c)) {
if (c == '0') {
tmp.lexeme = "0";
} else {
tmp.lexeme = "";
while (!input.EndOfInput() && isdigit(c)) {
tmp.lexeme += c;
input.GetChar(c);
}
if (!input.EndOfInput()) {
input.UngetChar(c);
}
}
tmp.token_type = NUM;
tmp.line_no = line_no;
return tmp;
} else {
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.lexeme = "";
tmp.token_type = ERROR;
tmp.line_no = line_no;
return tmp;
}
}
Token LexicalAnalyzer::ScanIdOrKeyword()
{
char c;
input.GetChar(c);
if (isalpha(c)) {
tmp.lexeme = "";
while (!input.EndOfInput() && isalnum(c)) {
tmp.lexeme += c;
input.GetChar(c);
}
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.line_no = line_no;
if (IsKeyword(tmp.lexeme))
tmp.token_type = FindKeywordIndex(tmp.lexeme);
else
tmp.token_type = ID;
} else {
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.lexeme = "";
tmp.token_type = ERROR;
}
return tmp;
}
// you should unget tokens in the reverse order in which they
// are obtained. If you execute
//
// t1 = lexer.GetToken();
// t2 = lexer.GetToken();
// t3 = lexer.GetToken();
//
// in this order, you should execute
//
// lexer.UngetToken(t3);
// lexer.UngetToken(t2);
// lexer.UngetToken(t1);
//
// if you want to unget all three tokens. Note that it does not
// make sense to unget t1 without first ungetting t2 and t3
//
TokenType LexicalAnalyzer::UngetToken(Token tok)
{
tokens.push_back(tok);;
return tok.token_type;
}
Token LexicalAnalyzer::GetToken()
{
char c;
// if there are tokens that were previously
// stored due to UngetToken(), pop a token and
// return it without reading from input
if (!tokens.empty()) {
tmp = tokens.back();
tokens.pop_back();
return tmp;
}
SkipSpace();
tmp.lexeme = "";
tmp.line_no = line_no;
tmp.token_type = END_OF_FILE;
if (!input.EndOfInput())
input.GetChar(c);
else
return tmp;
switch (c) {
case ';': tmp.token_type = SEMICOLON; return tmp;
case '^': tmp.token_type = POWER; return tmp;
case '-': tmp.token_type = MINUS; return tmp;
case '+': tmp.token_type = PLUS; return tmp;
case '=': tmp.token_type = EQUAL; return tmp;
case '(': tmp.token_type = LPAREN; return tmp;
case ')': tmp.token_type = RPAREN; return tmp;
case ',': tmp.token_type = COMMA; return tmp;
default:
if (isdigit(c)) {
input.UngetChar(c);
return ScanNumber();
} else if (isalpha(c)) {
input.UngetChar(c);
return ScanIdOrKeyword();
} else if (input.EndOfInput())
tmp.token_type = END_OF_FILE;
else
tmp.token_type = ERROR;
return tmp;
}
}
inputbuf.h
#ifndef __INPUT_BUFFER__H__
#define __INPUT_BUFFER__H__
#include
#include
class InputBuffer {
public:
void GetChar(char&);
char UngetChar(char);
std::string UngetString(std::string);
bool EndOfInput();
private:
std::vector
};
#endif //__INPUT_BUFFER__H__
inputbuf.cc--
#include
#include
#include
#include
#include
#include "inputbuf.h"
using namespace std;
bool InputBuffer::EndOfInput()
{
if (!input_buffer.empty())
return false;
else
return cin.eof();
}
char InputBuffer::UngetChar(char c)
{
if (c != EOF)
input_buffer.push_back(c);;
return c;
}
void InputBuffer::GetChar(char& c)
{
if (!input_buffer.empty()) {
c = input_buffer.back();
input_buffer.pop_back();
} else {
cin.get(c);
}
}
string InputBuffer::UngetString(string s)
{
for (int i = 0; i
input_buffer.push_back(s[s.size()-i-1]);
return s;
}
there is the 6 main file for this program



Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
