gr_iface.cpp revision 66d9f39f64360565c0b7176218412135b8242528
1/* FILE: gr_iface.cpp 2 * DATE MODIFIED: 31-Aug-07 3 * DESCRIPTION: Part of the SREC graph compiler project source files. 4 * 5 * Copyright 2007, 2008 Nuance Communciations, Inc. * 6 * * 7 * Licensed under the Apache License, Version 2.0 (the 'License'); * 8 * you may not use this file except in compliance with the License. * 9 * * 10 * You may obtain a copy of the License at * 11 * http://www.apache.org/licenses/LICENSE-2.0 * 12 * * 13 * Unless required by applicable law or agreed to in writing, software * 14 * distributed under the License is distributed on an 'AS IS' BASIS, * 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 16 * See the License for the specific language governing permissions and * 17 * limitations under the License. * 18 * * 19 *---------------------------------------------------------------------------*/ 20 21#include <iostream> 22#include <string> 23#include <assert.h> 24#include <cstdio> 25 26#include "grph.h" 27 28#define DEBUG 0 29 30int Graph::addSubGraph (SubGraph *sbGraph) 31{ 32 int ruleId; 33 34 if (numSubGraph%BLKSIZE == 0) { 35 if (numSubGraph >0) { 36 SubGraph **newSubGraph= new SubGraph * [numSubGraph+BLKSIZE]; 37 int *newIndex= new int [numSubGraph+BLKSIZE]; 38 for (int ii= 0; ii < numSubGraph; ii++) { 39 newSubGraph[ii]= subGraph[ii]; 40 newIndex[ii]= subIndex[ii]; 41 } 42 delete [] subGraph; 43 delete [] subIndex; 44 subGraph= newSubGraph; 45 subIndex= newIndex; 46 } 47 else { 48 subGraph= new SubGraph * [BLKSIZE]; 49 subIndex= new int [BLKSIZE]; 50 } 51 } 52 ruleId= sbGraph->getRuleId(); 53 54 subGraph[numSubGraph]= sbGraph; 55 subIndex[numSubGraph]= ruleId; 56#if DEBUG 57 char rulLabel[128]; 58 59 if (sbGraph) { 60 sbGraph->getName (rulLabel, 128); 61 printf ("Adding rule %s with %d\n", rulLabel, ruleId); 62 } 63#endif 64 numSubGraph++; 65 return numSubGraph; 66} 67 68int Graph::getSubGraphIndex (int subId) 69{ 70 for (int ii= numSubGraph-1; ii >= 0; ii--) 71 if (subIndex[ii] == subId) 72 return ii; 73 return -1; 74} 75 76int Graph::getSubGraphIndex (SubGraph *sGraph) 77{ 78 for (int ii= numSubGraph-1; ii >= 0; ii--) 79 if (subGraph[ii] == sGraph) 80 return subIndex[ii]; 81 return -1; 82} 83 84/** Begin and end scope */ 85 86void Graph::BeginRule (SubGraph *subg) 87{ 88 subg->BeginScope (SCOPE_RULE, 0, 0); 89#if DEBUG 90 subg->DebugPrintDirective ("<ruleref>"); 91#endif 92 return; 93} 94 95void Graph::EndRule (SubGraph *subg) 96{ 97#if DEBUG 98 subg->DebugPrintDirective ("</ruleref>"); 99#endif 100 subg->EndScope(); 101 return; 102} 103 104void Graph::BeginItem (SubGraph *subg) 105{ 106 subg->BeginScope (SCOPE_ITEM, 0, 0); 107#if DEBUG 108 subg->DebugPrintDirective ("<item>"); 109#endif 110 return; 111} 112 113void Graph::BeginItemRepeat (SubGraph *subg, int minCount, int maxCount) 114{ 115 subg->BeginScope (SCOPE_REPEAT, minCount, maxCount); 116#if DEBUG 117 subg->DebugPrintDirective ("<item repeat>"); 118#endif 119 return; 120} 121 122void Graph::AddRuleRef (SubGraph *subg, int ruleNo) 123{ 124 subg->AddItem (-ruleNo, ruleNo); 125#if DEBUG 126 subg->DebugPrintDirective ("<add ruleref>"); 127 printf (" %d\n", ruleNo); 128#endif 129 return; 130} 131 132void Graph::AddLabel (SubGraph *subg, int labNo) 133{ 134 subg->AddItem (labNo, -1); 135#if DEBUG 136 subg->DebugPrintLabel (labNo); 137#endif 138 return; 139} 140 141void Graph::AddTag (SubGraph *subg, int tagNo) 142{ 143 subg->AddTag (tagNo); 144#if DEBUG 145 subg->DebugPrintLabel (tagNo); 146#endif 147 return; 148} 149 150void Graph::EndItem (SubGraph *subg) 151{ 152#if DEBUG 153 subg->DebugPrintDirective ("</item>"); 154#endif 155 subg->EndScope(); 156 return; 157} 158 159void Graph::BeginOneOf (SubGraph *subg) 160{ 161 subg->BeginScope (SCOPE_ONEOF, 0, 0); 162#if DEBUG 163 subg->DebugPrintDirective ("<one-of>"); 164#endif 165 return; 166} 167 168void Graph::EndOneOf (SubGraph *subg) 169{ 170#if DEBUG 171 subg->DebugPrintDirective ("</one-of>"); 172#endif 173 subg->EndScope (); 174 return; 175} 176 177void Graph::BeginCount (SubGraph *subg, int minCount, int maxCount) 178{ 179 subg->BeginScope (SCOPE_COUNT, minCount, maxCount); 180#if DEBUG 181 subg->DebugPrintDirective ("<count>"); 182#endif 183 return; 184} 185 186void Graph::EndCount (SubGraph *subg) 187{ 188#if DEBUG 189 subg->DebugPrintDirective ("</count>"); 190#endif 191 subg->EndScope(); 192 return; 193} 194 195void Graph::BeginOptional (SubGraph *subg) 196{ 197 subg->BeginScope (SCOPE_OPT, 0, 0); 198#if DEBUG 199 subg->DebugPrintDirective ("<item repeat= 0- >"); 200#endif 201 return; 202} 203 204void Graph::ExpandRules (SubGraph *subg) 205{ 206 subg->ExpandRules (subGraph, subIndex, numSubGraph); 207 return; 208} 209