Version 6.3
Copyright © 2005 - 2012 Lowell D. Thomas
APG
  … ABNF Parser Generator
All Data Structures Files Functions Variables Typedefs Macros Pages
main.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  APG Version 6.3
3  Copyright (C) 2005 - 2012 Lowell D. Thomas, all rights reserved
4 
5  author: Lowell D. Thomas
6  email: lowell@coasttocoastresearch.com
7  website: http://www.coasttocoastresearch.com
8 
9  This program is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see
21  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
22  or write to the Free Software Foundation, Inc.,
23  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 *******************************************************************************/
25 #include "Apg.h"
26 #include "ApgUtilities.h"
27 #include "MEGACO.h"
28 #include <iostream>
29 
33 #define MASSERT(cond, msg) if(!(cond)){vTerminalAlertMsg(__LINE__, __FILE__, (msg));}
34 
35 using namespace std;
36 
45 int main()
46 {
47  apg_uint uiTrace = APG_FALSE;
48  apg_uint uiStats = APG_TRUE;
49  apg_uint uiState = APG_TRUE;
50  apg_uint uiSuccess;
51  const char* cpFileName = "MEGACO.input";
52  char* cpInput = NULL;
53  apg_achar* acpInput = NULL;
54  apg_uint uiInputLen = 0;
55 
56  printf("\nMEGACO: BEGIN\n");
57  void* vpMemCtx = vpMemCtor();
58  MASSERT(vpMemCtx, "unable to get memory component");
59 
60  MEGACO oMegaco(vTerminalAlert);
61  APG_CALLBACK saSyntaxRuleCallbacks[oMegaco.RULEID_COUNT];
62  APG_CALLBACK saAstRuleCallbacks[oMegaco.RULEID_COUNT];
63 
64  oMegaco.vDefineSyntaxCallbacks(saSyntaxRuleCallbacks, NULL);
65  oMegaco.uiSyntaxCallbacks(saSyntaxRuleCallbacks, NULL);
66 
67  oMegaco.vDefineAstCallbacks(saAstRuleCallbacks, NULL);
68  oMegaco.vAstInitCallbacks(saAstRuleCallbacks, NULL);
69 
70  uiInputLen = uiGetFileSize(cpFileName);
71  MASSERT(uiInputLen, "unable to open input string file");
72  cpInput = (char*)vpMemAlloc(vpMemCtx, uiInputLen + 10);
73  MASSERT(cpInput, "unable to allocate memory for string file");
74  acpInput = (apg_achar*)vpMemAlloc(vpMemCtx, (uiInputLen + 10) * sizeof(apg_achar));
75  MASSERT(acpInput, "unable to allocate memory for apg_achar input string");
76  uiInputLen = uiGetFile(cpFileName, (void*)cpInput);
77  MASSERT(uiInputLen, "unable to open input string file");
78  cpInput[uiInputLen] = 0;
79  vCharToAChar(acpInput, cpInput, uiInputLen);
80 
81  if(uiTrace){
82  printf("MEGACO: TRACE ENABLED\n");
83  oMegaco.uiTraceEnable();
84  }
85 
86  if(uiStats){
87  oMegaco.uiStatsEnable();
88  }
89 
90  printf("MEGACO: SYNTAX ANALYSIS\n");
91  uiSuccess = oMegaco.uiSyntaxAnalysis(oMegaco.RULEID_MEGACOMESSAGE, acpInput, uiInputLen);
92  if(uiState){
93  printf("\nMEGACO: STATE\n");
94  FILE* spOut = stdout;
95  APG_PARSER_STATE sState;
96  oMegaco.uiState(&sState);
97  vDisplayState(spOut, &sState);
98  }
99  MASSERT(uiSuccess, "oMegaco.uiSyntaxAnalysis: failed\n");
100  printf("oMegaco.uiSyntaxAnalysis: success\n");
101 
102  if(uiStats){
103 // printf("\nPARSER: STATISTICS\n");
104  FILE* spOut = stdout;
105  apg_uint uiTest;
106  apg_uint uiBufferSize = 50000;
107  char caBuffer[uiBufferSize];
108  APG_PARSER_STATS* spStats = (APG_PARSER_STATS*)&caBuffer[0];
109  oMegaco.uiStatsGet(NULL, &uiTest);
110  if(uiTest <= uiBufferSize){
111  oMegaco.uiStatsGet(spStats, &uiTest);
112  fprintf(spOut, "\nMEGACO STATISTICS:\n");
113  vDisplayOperatorStats(spOut, spStats);
114  vDisplayRuleStats(spOut, spStats, "count");
115  } else{
116  fprintf(spOut, "\nNO nMEGACO STATISTICS: insufficient buffer size: needed: %u: have: %u\n",
117  (apg_uint)uiTest, (apg_uint)uiBufferSize);
118  }
119  }
120 
121  printf("MEGACO: TRANSLATE AST\n");
122  oMegaco.uiAstTranslate(NULL, NULL);
123  printf("\nMEGACO: AST TRANSLATION: native APG format\n");
124  vDisplayAstRecords(NULL, oMegaco.vpGetParserCtx());
125  printf("\nMEGACO: AST TRANSLATION: XML format\n");
126  uiAstToXml(NULL, oMegaco.vpGetParserCtx());
127 
128  printf("\nMEGACO: END SUCCESS\n");
129  return EXIT_SUCCESS;
130 }
APG_PARSER_STATS
full set of statistics gathered during parsing, uiParserSyntaxAnalysis()
Definition: Apg.h:591
APG_PARSER_STATE
the state of the parser after parsing an input string.
Definition: Apg.h:561
apg_uint
unsigned int apg_uint
Definition: Apg.h:169
APG_CALLBACK
apg_uint(* APG_CALLBACK)(APG_CBDATA *spData)
Definition: Apg.h:515
vpMemCtor
void * vpMemCtor()
Definition: Memory.c:77
vDisplayState
void vDisplayState(FILE *spOut, APG_PARSER_STATE *spState)
Definition: Utilities.c:391
vpMemAlloc
void * vpMemAlloc(void *vpCtx, apg_uint uiBytes)
Definition: Memory.c:130
uiGetFileSize
apg_uint uiGetFileSize(const char *cpFileName)
Definition: Files.c:40
vDisplayAstRecords
void vDisplayAstRecords(FILE *spOut, void *vpParserCtx)
Definition: Utilities.c:183
vDisplayOperatorStats
void vDisplayOperatorStats(FILE *spOut, APG_PARSER_STATS *spStats)
Definition: Utilities.c:296
Apg.h
Required header file for all APG-generated parsers. Contains important configuration macros and decla...
APG_TRUE
#define APG_TRUE
Definition: Apg.h:187
vDisplayRuleStats
void vDisplayRuleStats(FILE *spOut, APG_PARSER_STATS *spStats, const char *cpType)
Definition: Utilities.c:338
MASSERT
#define MASSERT(cond, msg)
Definition: main.cpp:33
ApgUtilities.h
Declarations of all APG utility functions.
apg_achar
unsigned char apg_achar
Definition: Apg.h:183
APG_PARSER_STATE::uiState
apg_uint uiState
Definition: Apg.h:563
uiAstToXml
apg_uint uiAstToXml(FILE *spOut, void *vpParserCtx)
Definition: Utilities.c:479
APG_FALSE
#define APG_FALSE
Definition: Apg.h:190
uiGetFile
apg_uint uiGetFile(const char *cpFileName, void *vpBuffer)
Definition: Files.c:71
vCharToAChar
void vCharToAChar(apg_achar *acpAChars, const char *cpChars, apg_uint uiLen)
Definition: Tools.c:79
vTerminalAlert
void vTerminalAlert(unsigned int uiLine, const char *cpFile)
Definition: Utilities.c:557
main
int main()
Definition: main.cpp:42
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/licenses.html or write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.