0

I am writing a parser for a compiler . So for the constructor I have the code :

//constructor
Parser::Parser(char* file)
{
  MyLex(file) ; 
}

Upon compiling using g++ parsy.cpp parsydriver.cpp , I am however getting this error saying :

parsy.cpp: In constructor ‘Parser::Parser(char*)’:
parsy.cpp:13: error: no matching function for call to ‘Lex::Lex()’
lexy2.h:34: note: candidates are: Lex::Lex(char*)
lexy2.h:31: note:                 Lex::Lex(const Lex&)
parsy.cpp:15: error: no match for call to ‘(Lex) (char*&)’

Where am I going wrong ? Lex myLex is declared as private in the Parser header . I am at my wit's end . I tried using this :

//constructor
Parser::Parser(char* file):myLex(file)
{ 
}

My Lexical Analyser constructor is :

Lex::Lex(char* filename): ch(0) 
{
  //Set up the list of reserved words
  reswords[begint] = "BEGIN";
  reswords[programt] = "PROGRAM";
  reswords[constt] = "CONST";
  reswords[vart] = "VAR";
  reswords[proceduret] = "PROCEDURE";
  reswords[ift] = "IF";
  reswords[whilet] = "WHILE";
  reswords[thent] = "THEN";
  reswords[elset] = "ELSE";
  reswords[realt] = "REAL";
  reswords[integert] = "INTEGER";
  reswords[chart] = "CHAR";
  reswords[arrayt] = "ARRAY";
  reswords[endt] = "END";

  //Open the file for reading
  file.open(filename);
}

but, this creates a bunch of undefined reference to Lexical Analyser file and functions ! I have included the files properly. But so far , I don't understand how to get over this problem.

UPDATE The header file inclusions are :

parsy.h file :

#ifndef PARSER_H
#define PARSER_H

// other library file includes

#include "lexy2.h"
class Parser
{
}...

parsy.cpp file :

// usual ilbraries

#include "parsy.h"

using namespace std ;

Parser::Parser(char* file) ....

parsydriver.cpp :

// usual libraries
#include "parsy.h"
using namespace std ;

int main()
..

lexy2.cpp file :

I have included the lexy2.h file. Should I be including the parser header files in the lexical analyser ones ? Doesn't seem likely. But how should I tackle them then ?

thestralFeather7
  • 529
  • 2
  • 10
  • 28
  • 3
    The second way is the proper way. The problems would still exist had the first worked. They're linker errors that sprout from you not linking correctly in some form. – chris Feb 26 '13 at 04:34
  • how can the linker errors be overcome ? I will edit and update the question where I put my header file includes. – thestralFeather7 Feb 26 '13 at 04:35
  • Take your pick: http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – chris Feb 26 '13 at 04:37
  • is parser derived from Lex? if so construct Lex before parser. If not derived then too construct Lex first. u said u declared myLex in the header. that means u declared with a default constructor? if so that is the cause as u have no defined the default constructor. try to declare a pointer to lex in the header and not the variable itself. – Koushik Shetty Feb 26 '13 at 05:10

1 Answers1

2

Code inside constructor runs when object is already constructed. Your class MyLex does not have default constructor. So you have to define default constructor or it should be:

//constructor
Parser::Parser(char* file): MyLex(file)
{
}

If you have "undefined symbol" linker errors then you forgot to add some .cpp files (maybe lexy2.cpp) to the project or compiler command line. Assuming that all undefined symbols located in lexy2.cpp then try g++ parsy.cpp parsydriver.cpp lexy2.cpp.

Sergey
  • 627
  • 1
  • 9
  • 16
  • ok so I have 4 files + 1driver file . how should I be compiling it in the shell? files are : lexy2.h lexy2.cpp parsy.h parsy.cpp and parsydriver.cpp – thestralFeather7 Feb 26 '13 at 04:59