/******************************************************************************
** Copyright (C) 2001, University of Alabama in Huntsville. All rights 
** reserved.
**
** File   : CGIFSyntaxAnalyzer.h
**
** Author : Saranyu Tantipongsakul, Da Sun
**
** Description:
**    This file provides the interface for CCGIFSyntaxAnalyzer class.
**
** History:
** 01-Dec-01 Saranyu Tantipongsakul, Da Sun
**     File Created.  Copied major portions from parser written in C by Da.
******************************************************************************/
#pragma warning(disable: 4786)  // Disable debug information warning

#include "CGIFLexicalAnalyzer.h"
#include "CGEngine/CKnowledgeBase.h"
#include "CGEngine/CTypeHierarchy.h"
#include "CGEngine/CTypeDefinition.h"
#include "CGEngine/CRelationHierarchy.h"
#include "CGEngine/CRelationDefinition.h"
#include "CGEngine/CCatalogOfIndividuals.h"
#include "CGEngine/CSheetOfAssertion.h"
#include "CGEngine/ACCGFactory.h"
#include "CGEngine/CCGFactory.h"

/*******************************************************************
Define a simple version of CGIF file as following

  DefLabel    ::= "*" Identifier?
  BoundLabel  ::= "?" Identifier?
  concept     ::= "[" Type(1)? {corefLinks?, referent? }? comment? "]"
  corefLinks  ::= DefLabel | BoundLabel*
  referent    ::= ":" {designator?}
  designator  ::= Locator | Quantofier
  locator     ::= name | IndividualMarker | Indexical

  relation    ::= "(" Type(N) Arc* comment? ")"
  Arc         ::= concept | BoundLabel
  Type(N)     ::= Identifier
  
  CG          ::= (concept | relation |comment)*  

  ----------------------------------------------------------------------
  then we have the new expression of the above expression
  concept     ::= "[" Indentifier? {corefLinks?, referent? }? comment? "]"
  corefLinks  ::= DefLabel | BoundLabel*
  quantifier  ::= "@" (Identifier ( "{" Arc* "}" )?)
  referent    ::= ":" { name | IndividualMarker | Indexical | quantifier}?
  relation    ::= "(" Identifier Arc* comment? ")"
  Arc         ::= concept | BoundLabel
  DefLabel    ::= "*" Identifier?
  BoundLabel  ::= "?" Identifier?
  
  CG          ::= (concept | relation |comment)*  
***************************************************/

typedef struct stringlisttype
{
	char value[TOKENSIZE];
	stringlisttype* next;
} stringliststruct;

typedef struct quantifiertype
{
	char name[TOKENSIZE];
    stringlisttype* stringlist;
}quantifierstruct;

typedef struct coreflinktype
{
	char value[TOKENSIZE];
	char kind[KINDSIZE];
	coreflinktype *next;
} coreflinkstruct;

typedef struct referenttype
{
	char kind[KINDSIZE];
	char value[TOKENSIZE];
    quantifiertype* quantifier;

	referenttype *next;
}referentstruct;

typedef struct concepttype
{
	char name[NAMESIZE];
    coreflinktype *coreflinks;
    referenttype *referlinks;
	char comment[COMMENTSIZE];
} conceptstruct;

typedef struct arctype
{
	char con_bound[NAMESIZE];  //it is a "concept" or "boundlabel" 
	concepttype *concept;
	char boundLabel[NAMESIZE];
}arcstruct;

typedef struct arclisttype
{
	arctype *arc;
	arclisttype *next;
}arclist;

typedef struct relationtype
{
	char name[NAMESIZE];
    arclisttype *arcs;
    char comment[COMMENTSIZE];
} relationstruct;


class CCGIFSyntaxAnalyzer
{
	public:
		CCGIFSyntaxAnalyzer(CCGIFLexicalAnalyzer* pTokenizer);
		~CCGIFSyntaxAnalyzer();
        CKnowledgeBase* install(ACCGFactory *pFactory);

	private:
		CKnowledgeBase* m_pKB;
		CTypeHierarchy* m_pTH;
		CRelationHierarchy* m_pRH;
		CCatalogOfIndividuals* m_pCOI;
		CSheetOfAssertion* m_pSOA;


	private:
		void error(char *msg);
		int getIndexIndiv(simpleTokenType *simpleTokenList, int* position,
							char* label, int tokenNumber);
		int getDefLabel(simpleTokenType *simpleTokenList, int* position,
							char* label, int tokenNumber);
		int getBoundLabel (simpleTokenType *simpleTokenList, int* position, 
							char*label, int tokenNumber);
		int getReferent (simpleTokenType *simpleTokenList, int* position, 
							char* ref, int tokenNumber);
		int getComment (simpleTokenType *simpleTokenList, int* position, 
							char* com, int tokenNumber);
		int getCorefLinks(simpleTokenType *simpleTokenList, int* position, 
							char* coref,char* type, int tokenNumber);
		int getConcept(simpleTokenType *simpleTokenList, int* position, 
							concepttype* concept, int tokenNumber);
		int getArc(simpleTokenType *simpleTokenList, int* position, 
							arctype *arc, int tokenNumber);
		int getRelation(simpleTokenType *simpleTokenList, int* position, 
							relationtype* relation, int tokenNumber);
		int getOneElement(simpleTokenType* simpleTokenList, int tokenNumber,
							concepttype *concept, relationtype *relation,
							char comment[COMMENTSIZE],int *position);

	private:
		CCGIFLexicalAnalyzer* m_pTokenizer;
		simpleTokenType* simpleTokenList;
		int m_nNewTokenNumber;
		int *seg_ptr; // seg pointer
		int position;
		int CONCEPT, RELATION, COMMENT;
		concepttype *concept;
		relationtype *relation;		           
	    char comment[COMMENTSIZE];
};