Hi guys, and thanks for your replies.
My program supposed to be a simple lexical analyzer and it is using an external file containing some definitions supplied by the "client" (The course staff...).
Following are the three files: lex.h, lex.c (which I wrote) and resource.h, which is the external. I also added the list of compilation errors. Thanks in advance.
/*
File: lex.H
Abstract:
The "Basic Basic" lexical analizer interface
*/
#ifndef _LEX_H_
#define _LEX_H_
//
// Definitions
//
// The maximum input line handled (255 characters)
#define MAX_LINE 256
//
// Types
//
// Returned token types
typedef enum _TOKEN_TYPE
{
LIST_TOKEN,
RUN_TOKEN,
QUIT_TOKEN,
DEL_TOKEN,
NEW_TOKEN,
SET_TOKEN,
IF_TOKEN,
THEN_TOKEN,
ELSE_TOKEN,
GOTO_TOKEN,
PRINT_TOKEN,
END_TOKEN,
NUMBER_TOKEN,
VARIABLE_TOKEN,
OPERATOR_TOKEN,
ASSIGNMENT_TOKEN,
OPENP_TOKEN,
CLOSEP_TOKEN,
STRING_TOKEN,
NEWLINE_TOKEN,
UNRECOGNIZED_TOKEN // Not used...
} TOKEN_TYPE;
// Operator types
typedef enum _OPERATOR
{
ADD, // +
SUB, // -
MUL, // *
DIV, // /
EQL, // ==
NEQL, // !=
GT, // >
LT, // <
GTE, // >=
LTE // <=
} OPERATOR;
// Token attributes
typedef struct _TOKEN
{
TOKEN_TYPE t;
const char* psz; // Will point to a null terminated string identifying the token
double dVal; // Valid only for NUMBER_TOKEN
OPERATOR op; // Valid only for OPERATOR_TOKEN
bool bIsPosINT; // Valid only for NUMBER_TOKEN
} TOKEN;
//
// Our exception type for syntax errors
//
class CLexicalException
{
public:
CLexicalException(const char* sz = "Unrecognized token") : m_sz(sz) {}
const char* Reason() const { return m_sz; }
private:
const char* m_sz;
};
///////////////////////////////////////////////////////////////////////////////////
//
// The Lexical analyser class definition
//
class CLex
{
public:
// C'tor
CLex(istream& InStream);
//
// Methods
//
TOKEN GetNextToken(bool bPeek = false);
bool NextLine();
private:
//
// Helper functions
//
char* MyStrTok();
//
// Members
//
istream& m_InStream;
char m_szBuf[MAX_LINE];
char* m_pChar;
char m_ch;
bool m_bPeek;
};
#endif // _LEX_H_
/*
File: lex.C
Abstract:
The "Basic Basic" lexical analizer
*/
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "lex.H"
//
// C'tor
//
CLex::CLex(istream& InStream) : m_InStream(InStream), m_pChar(m_szBuf)
{
*m_szBuf = '\0';
}
/*
Function: GetNextToken
Abstract:
Extracts the next token from the current line. Once an endline is reached, we keep
returning it until CLex::NextLine() is called.
Parameters:
bPeek - If true, we return the next token in the stream, but don't advance. This means
that the next call will return the same token.
Returns: A TOKEN structure.
*/
TOKEN
CLex::GetNextToken(bool bPeek)
{
static TOKEN tok; // This needs to be static so peeking can work...
char* pChar;
double d;
//
// If we only peeked last time, we still need to process the last token
//
if (!m_bPeek)
{
tok.psz = MyStrTok();
}
m_bPeek = bPeek;
// Is this a new line token?
if (*tok.psz == '\0')
{
tok.t = NEWLINE_TOKEN;
return tok;
}
//
// Check for Basic-basic keywords
//
#define COMPARE_WORD(word) \
if (!strcmp(tok.psz, #word))\
{ \
tok.t = word##_TOKEN; \
return tok; \
}
COMPARE_WORD(LIST);
COMPARE_WORD(RUN);
COMPARE_WORD(QUIT);
COMPARE_WORD(DEL);
COMPARE_WORD(NEW);
COMPARE_WORD(SET);
COMPARE_WORD(IF);
COMPARE_WORD(THEN);
COMPARE_WORD(ELSE);
COMPARE_WORD(GOTO);
COMPARE_WORD(PRINT);
COMPARE_WORD(END);
//
// Is this a valid number?
//
// First see if this is an unsigned integer in integer format
d = strtol(tok.psz, &pChar, 10);
if (*pChar == '\0')
{
tok.bIsPosINT = (d > 0);
tok.t = NUMBER_TOKEN;
tok.dVal = d;
return tok;
}
// Next see if this might be any number
d = strtod(tok.psz, &pChar);
if (*pChar == '\0')
{
tok.bIsPosINT = false;
tok.t = NUMBER_TOKEN;
tok.dVal = d;
return tok;
}
//
// Is this a valid string token?
//
if (*tok.psz == '\"')
{
//
// See if the next quote is the one closing the token
//
if (strchr(tok.psz + 1, '\"') == strchr(tok.psz + 1, '\0') - 1)
{
tok.t = STRING_TOKEN;
return tok;
}
}
//
// One character tokens
//
if (1 == strlen(tok.psz))
{
switch (*tok.psz)
{
//
// Start with operators
//
#define COMPARE_SINGLE_CHAR_OPERATOR(ch,op_type) \
case ch: \
tok.t = OPERATOR_TOKEN; \
tok.op = op_type; \
return tok;
COMPARE_SINGLE_CHAR_OPERATOR('+', ADD);
COMPARE_SINGLE_CHAR_OPERATOR('-', SUB);
COMPARE_SINGLE_CHAR_OPERATOR('/', DIV);
COMPARE_SINGLE_CHAR_OPERATOR('*', MUL);
COMPARE_SINGLE_CHAR_OPERATOR('<', LT);
COMPARE_SINGLE_CHAR_OPERATOR('>', GT);
case '=':
// A single '=' is an assignment!
tok.t = ASSIGNMENT_TOKEN;
return tok;
case '(':
tok.t = OPENP_TOKEN;
return tok;
case ')':
tok.t = CLOSEP_TOKEN;
return tok;
default:
// This can only be a variable...
if (isupper(*tok.psz))
{
tok.t = VARIABLE_TOKEN;
return tok;
}
}
}
//
// Some more operators...
//
#define COMPARE_OPERATOR(str, op_type) \
if (!strcmp(tok.psz, str)) \
{ \
tok.t = OPERATOR_TOKEN; \
tok.op = op_type; \
return tok; \
}
COMPARE_OPERATOR(">=", GTE);
COMPARE_OPERATOR("<=", LTE);
COMPARE_OPERATOR("!=", NEQL);
COMPARE_OPERATOR("==", EQL);
//
// If we got here we didn't find any matching token
//
throw CLexicalException(tok.psz);
// Never reached...
tok.t = UNRECOGNIZED_TOKEN;
return tok;
}
/*
Function: NextLine
Abstract:
Moves the lexical analyzer to the next line in the input stream. The remaining characters
of the current line are dropped
Parameters: --
Returns: true if haven't reached EOF yet.
*/
bool
CLex::NextLine()
{
// Check if there is no more input
if (EOF == cin.peek())
{
return false;
}
// Extract the new line
m_InStream.getline(m_szBuf, MAX_LINE);
m_pChar = m_szBuf;
m_ch = *m_szBuf;
m_bPeek = false;
return true;
}
//
// Private functions
//
char*
CLex::MyStrTok()
{
char* pTemp;
//
// Restore character we replaced with '\0' (When we get here the first time in a new
// line, CLex::NextLine() sets m_ch to the first character so this is a NOP...).
//
*m_pChar = m_ch;
//
// Skip leading spaces
//
while (*m_pChar == ' ')
m_pChar++;
pTemp = m_pChar; // pTemp marks begining of token
//
// Parenthesis is a special case: if we identified it, we have to return it as is
//
if (*pTemp == '(' || *pTemp == ')')
{
m_pChar++;
}
else
{
//
// The next token includes all characters up to the next space, parenthesis or
// null terminator (apart characters inside strings)
//
while ( (*m_pChar != ' ') &&
(*m_pChar != '(') &&
(*m_pChar != ')') &&
(*m_pChar != '\0') )
{
//
// If the next character is a string, skip until its closing quote
//
if (*m_pChar == '\"')
{
char* pMatch = strchr(m_pChar + 1, '\"');
if (!pMatch)
{
//
// The string doesn't have a closing quote! So this "token" ends
// at the end of the line...
//
m_pChar = strchr(m_pChar + 1, '\0');
break;
}
m_pChar = pMatch;
}
// Skip this character
m_pChar++;
}
}
//
// Mark end of token with a null terminator (and save the next character so we can
// restore it in the next call...)
//
m_ch = *m_pChar;
*(m_pChar) = '\0';
return pTemp;
}
/*
File: resource.H
Abstract:
String resource constants
*/
#ifndef _RESOURCE_H_
#define _RESOURCE_H_
#define SYNTAX_ERROR "Syntax Error"
#define BAD_STATEMENT "Incorrect statement"
#define LINE_EXISTS "A Line with this line number already exists"
#define NO_LINE "The line number was not found"
#define NO_END "Finished execution without reaching END"
#define NO_PROGRAM "No program in memory!"
#define BAD_JUMP "Encountered jump to non-existent line"
#define MAX_TIME "Reached maximum execution time (infinite loop?)"
#define DIVIDE_ZERO "Divide by zero"
#endif // _RESOURCE_H_
***************************
Compilation results:
***************************
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(18): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(18): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(18): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(19): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(19): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(19): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(20): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(20): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(20): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(21): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(21): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(21): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(22): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(22): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(22): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(23): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(23): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(23): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(24): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(24): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(24): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(25): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(25): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(26): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(26): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(27): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(27): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(27): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(28): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(28): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(28): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(29): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(29): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(29): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(30): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(30): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(30): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(31): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(31): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(31): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(32): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdlib(17): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdlib(17): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdlib(17): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdlib(19): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdlib(19): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdlib(19): error C2059: syntax error : ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(18): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(18): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(18): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(19): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(19): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(19): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(20): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(20): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(20): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(21): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(21): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(21): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(22): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(22): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(22): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(23): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(23): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(23): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(24): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(24): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(24): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(25): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(25): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(26): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(26): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(27): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(27): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(27): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(28): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(28): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(28): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(29): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(29): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(29): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(30): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(30): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(30): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(31): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(31): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(31): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(32): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdlib(17): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdlib(17): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdlib(17): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdlib(19): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdlib(19): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdlib(19): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdlib(20): error C2143: syntax error : missing '{' before ':'
D:\Microsoft\Microsoft Visual Studio .NET 2003\Vc7\include\cstdlib(20): fatal error C1003: error count exceeds 100; stopping compilation