\doxysection{jsgf.\+c}
\label{jsgf_8c_source}\index{src/libsphinxbase/lm/jsgf.c@{src/libsphinxbase/lm/jsgf.c}}
\textbf{ Go to the documentation of this file.}
\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{/*\ -\/*-\/\ c-\/basic-\/offset:\ 4;\ indent-\/tabs-\/mode:\ nil\ -\/*-\/\ */}}
\DoxyCodeLine{00002\ \textcolor{comment}{/*\ ====================================================================}}
\DoxyCodeLine{00003\ \textcolor{comment}{\ *\ Copyright\ (c)\ 2007\ Carnegie\ Mellon\ University.\ \ All\ rights}}
\DoxyCodeLine{00004\ \textcolor{comment}{\ *\ reserved.}}
\DoxyCodeLine{00005\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00006\ \textcolor{comment}{\ *\ Redistribution\ and\ use\ in\ source\ and\ binary\ forms,\ with\ or\ without}}
\DoxyCodeLine{00007\ \textcolor{comment}{\ *\ modification,\ are\ permitted\ provided\ that\ the\ following\ conditions}}
\DoxyCodeLine{00008\ \textcolor{comment}{\ *\ are\ met:}}
\DoxyCodeLine{00009\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00010\ \textcolor{comment}{\ *\ 1.\ Redistributions\ of\ source\ code\ must\ retain\ the\ above\ copyright}}
\DoxyCodeLine{00011\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer.\ }}
\DoxyCodeLine{00012\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00013\ \textcolor{comment}{\ *\ 2.\ Redistributions\ in\ binary\ form\ must\ reproduce\ the\ above\ copyright}}
\DoxyCodeLine{00014\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer\ in}}
\DoxyCodeLine{00015\ \textcolor{comment}{\ *\ \ \ \ the\ documentation\ and/or\ other\ materials\ provided\ with\ the}}
\DoxyCodeLine{00016\ \textcolor{comment}{\ *\ \ \ \ distribution.}}
\DoxyCodeLine{00017\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00018\ \textcolor{comment}{\ *\ This\ work\ was\ supported\ in\ part\ by\ funding\ from\ the\ Defense\ Advanced\ }}
\DoxyCodeLine{00019\ \textcolor{comment}{\ *\ Research\ Projects\ Agency\ and\ the\ National\ Science\ Foundation\ of\ the\ }}
\DoxyCodeLine{00020\ \textcolor{comment}{\ *\ United\ States\ of\ America,\ and\ the\ CMU\ Sphinx\ Speech\ Consortium.}}
\DoxyCodeLine{00021\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00022\ \textcolor{comment}{\ *\ THIS\ SOFTWARE\ IS\ PROVIDED\ BY\ CARNEGIE\ MELLON\ UNIVERSITY\ \`{}\`{}AS\ IS''\ AND\ }}
\DoxyCodeLine{00023\ \textcolor{comment}{\ *\ ANY\ EXPRESSED\ OR\ IMPLIED\ WARRANTIES,\ INCLUDING,\ BUT\ NOT\ LIMITED\ TO,\ }}
\DoxyCodeLine{00024\ \textcolor{comment}{\ *\ THE\ IMPLIED\ WARRANTIES\ OF\ MERCHANTABILITY\ AND\ FITNESS\ FOR\ A\ PARTICULAR}}
\DoxyCodeLine{00025\ \textcolor{comment}{\ *\ PURPOSE\ ARE\ DISCLAIMED.\ \ IN\ NO\ EVENT\ SHALL\ CARNEGIE\ MELLON\ UNIVERSITY}}
\DoxyCodeLine{00026\ \textcolor{comment}{\ *\ NOR\ ITS\ EMPLOYEES\ BE\ LIABLE\ FOR\ ANY\ DIRECT,\ INDIRECT,\ INCIDENTAL,}}
\DoxyCodeLine{00027\ \textcolor{comment}{\ *\ SPECIAL,\ EXEMPLARY,\ OR\ CONSEQUENTIAL\ DAMAGES\ (INCLUDING,\ BUT\ NOT\ }}
\DoxyCodeLine{00028\ \textcolor{comment}{\ *\ LIMITED\ TO,\ PROCUREMENT\ OF\ SUBSTITUTE\ GOODS\ OR\ SERVICES;\ LOSS\ OF\ USE,\ }}
\DoxyCodeLine{00029\ \textcolor{comment}{\ *\ DATA,\ OR\ PROFITS;\ OR\ BUSINESS\ INTERRUPTION)\ HOWEVER\ CAUSED\ AND\ ON\ ANY\ }}
\DoxyCodeLine{00030\ \textcolor{comment}{\ *\ THEORY\ OF\ LIABILITY,\ WHETHER\ IN\ CONTRACT,\ STRICT\ LIABILITY,\ OR\ TORT\ }}
\DoxyCodeLine{00031\ \textcolor{comment}{\ *\ (INCLUDING\ NEGLIGENCE\ OR\ OTHERWISE)\ ARISING\ IN\ ANY\ WAY\ OUT\ OF\ THE\ USE\ }}
\DoxyCodeLine{00032\ \textcolor{comment}{\ *\ OF\ THIS\ SOFTWARE,\ EVEN\ IF\ ADVISED\ OF\ THE\ POSSIBILITY\ OF\ SUCH\ DAMAGE.}}
\DoxyCodeLine{00033\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00034\ \textcolor{comment}{\ *\ ====================================================================}}
\DoxyCodeLine{00035\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00036\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00037\ }
\DoxyCodeLine{00038\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00039\ \textcolor{preprocessor}{\#include\ <assert.h>}}
\DoxyCodeLine{00040\ }
\DoxyCodeLine{00041\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/ckd\_alloc.h"{}}}
\DoxyCodeLine{00042\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/strfuncs.h"{}}}
\DoxyCodeLine{00043\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/hash\_table.h"{}}}
\DoxyCodeLine{00044\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/err.h"{}}}
\DoxyCodeLine{00045\ }
\DoxyCodeLine{00046\ \textcolor{preprocessor}{\#include\ "{}jsgf\_internal.h"{}}}
\DoxyCodeLine{00047\ \textcolor{preprocessor}{\#include\ "{}jsgf\_parser.h"{}}}
\DoxyCodeLine{00048\ \textcolor{preprocessor}{\#include\ "{}jsgf\_scanner.h"{}}}
\DoxyCodeLine{00049\ }
\DoxyCodeLine{00057\ jsgf\_atom\_t\ *}
\DoxyCodeLine{00058\ jsgf\_atom\_new(\textcolor{keywordtype}{char}\ *name,\ \textcolor{keywordtype}{float}\ weight)}
\DoxyCodeLine{00059\ \{}
\DoxyCodeLine{00060\ \ \ \ \ jsgf\_atom\_t\ *atom;}
\DoxyCodeLine{00061\ }
\DoxyCodeLine{00062\ \ \ \ \ atom\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*atom));}
\DoxyCodeLine{00063\ \ \ \ \ atom-\/>name\ =\ ckd\_salloc(name);}
\DoxyCodeLine{00064\ \ \ \ \ atom-\/>weight\ =\ weight;}
\DoxyCodeLine{00065\ \ \ \ \ \textcolor{keywordflow}{return}\ atom;}
\DoxyCodeLine{00066\ \}}
\DoxyCodeLine{00067\ }
\DoxyCodeLine{00068\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00069\ jsgf\_atom\_free(jsgf\_atom\_t\ *atom)}
\DoxyCodeLine{00070\ \{}
\DoxyCodeLine{00071\ \ \ \ \ \textcolor{keywordflow}{if}\ (atom\ ==\ NULL)}
\DoxyCodeLine{00072\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00073\ \ \ \ \ ckd\_free(atom-\/>name);}
\DoxyCodeLine{00074\ \ \ \ \ ckd\_free(atom);}
\DoxyCodeLine{00075\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00076\ \}}
\DoxyCodeLine{00077\ }
\DoxyCodeLine{00078\ jsgf\_t\ *}
\DoxyCodeLine{00079\ jsgf\_grammar\_new(jsgf\_t\ *parent)}
\DoxyCodeLine{00080\ \{}
\DoxyCodeLine{00081\ \ \ \ \ jsgf\_t\ *grammar;}
\DoxyCodeLine{00082\ }
\DoxyCodeLine{00083\ \ \ \ \ grammar\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*grammar));}
\DoxyCodeLine{00084\ \ \ \ \ \textcolor{comment}{/*\ If\ this\ is\ an\ imported/subgrammar,\ then\ we\ will\ share\ a\ global}}
\DoxyCodeLine{00085\ \textcolor{comment}{\ \ \ \ \ *\ namespace\ with\ the\ parent\ grammar.\ */}}
\DoxyCodeLine{00086\ \ \ \ \ \textcolor{keywordflow}{if}\ (parent)\ \{}
\DoxyCodeLine{00087\ \ \ \ \ \ \ \ \ grammar-\/>rules\ =\ parent-\/>rules;}
\DoxyCodeLine{00088\ \ \ \ \ \ \ \ \ grammar-\/>imports\ =\ parent-\/>imports;}
\DoxyCodeLine{00089\ \ \ \ \ \ \ \ \ grammar-\/>searchpath\ =\ parent-\/>searchpath;}
\DoxyCodeLine{00090\ \ \ \ \ \ \ \ \ grammar-\/>parent\ =\ parent;}
\DoxyCodeLine{00091\ \ \ \ \ \}}
\DoxyCodeLine{00092\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00093\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ *jsgf\_path;}
\DoxyCodeLine{00094\ }
\DoxyCodeLine{00095\ \ \ \ \ \ \ \ \ grammar-\/>rules\ =\ hash\_table\_new(64,\ 0);}
\DoxyCodeLine{00096\ \ \ \ \ \ \ \ \ grammar-\/>imports\ =\ hash\_table\_new(16,\ 0);}
\DoxyCodeLine{00097\ }
\DoxyCodeLine{00098\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Silvio\ Moioli:\ no\ getenv()\ in\ Windows\ CE\ */}}
\DoxyCodeLine{00099\ \textcolor{preprocessor}{\ \ \ \ \ \ \ \ \#if\ !defined(\_WIN32\_WCE)}}
\DoxyCodeLine{00100\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((jsgf\_path\ =\ getenv(\textcolor{stringliteral}{"{}JSGF\_PATH"{}}))\ !=\ NULL)\ \{}
\DoxyCodeLine{00101\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ *word,\ *c;}
\DoxyCodeLine{00102\ }
\DoxyCodeLine{00103\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ FIXME:\ This\ should\ be\ a\ function\ in\ libsphinxbase.\ */}}
\DoxyCodeLine{00104\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ FIXME:\ Also\ nextword()\ is\ totally\ useless...\ */}}
\DoxyCodeLine{00105\ \ \ \ \ \ \ \ \ \ \ \ \ word\ =\ jsgf\_path\ =\ ckd\_salloc(jsgf\_path);}
\DoxyCodeLine{00106\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{while}\ ((c\ =\ strchr(word,\ \textcolor{charliteral}{':'})))\ \{}
\DoxyCodeLine{00107\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ *c\ =\ \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{00108\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ grammar-\/>searchpath\ =\ glist\_add\_ptr(grammar-\/>searchpath,\ word);}
\DoxyCodeLine{00109\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ word\ =\ c\ +\ 1;}
\DoxyCodeLine{00110\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00111\ \ \ \ \ \ \ \ \ \ \ \ \ grammar-\/>searchpath\ =\ glist\_add\_ptr(grammar-\/>searchpath,\ word);}
\DoxyCodeLine{00112\ \ \ \ \ \ \ \ \ \ \ \ \ grammar-\/>searchpath\ =\ glist\_reverse(grammar-\/>searchpath);}
\DoxyCodeLine{00113\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00114\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00115\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Default\ to\ current\ directory.\ */}}
\DoxyCodeLine{00116\ \ \ \ \ \ \ \ \ \ \ \ \ grammar-\/>searchpath\ =\ glist\_add\_ptr(grammar-\/>searchpath,\ ckd\_salloc(\textcolor{stringliteral}{"{}."{}}));}
\DoxyCodeLine{00117\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00118\ \textcolor{preprocessor}{\ \ \ \ \ \ \ \ \#endif\ }}
\DoxyCodeLine{00119\ \ \ \ \ \}}
\DoxyCodeLine{00120\ }
\DoxyCodeLine{00121\ \ \ \ \ \textcolor{keywordflow}{return}\ grammar;}
\DoxyCodeLine{00122\ \}}
\DoxyCodeLine{00123\ }
\DoxyCodeLine{00124\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00125\ jsgf\_grammar\_free(jsgf\_t\ *jsgf)}
\DoxyCodeLine{00126\ \{}
\DoxyCodeLine{00127\ \ \ \ \ \textcolor{comment}{/*\ FIXME:\ Probably\ should\ just\ use\ refcounting\ instead.\ */}}
\DoxyCodeLine{00128\ \ \ \ \ \textcolor{keywordflow}{if}\ (jsgf-\/>parent\ ==\ NULL)\ \{}
\DoxyCodeLine{00129\ \ \ \ \ \ \ \ \ hash\_iter\_t\ *itor;}
\DoxyCodeLine{00130\ \ \ \ \ \ \ \ \ gnode\_t\ *gn;}
\DoxyCodeLine{00131\ }
\DoxyCodeLine{00132\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (itor\ =\ hash\_table\_iter(jsgf-\/>rules);\ itor;}
\DoxyCodeLine{00133\ \ \ \ \ \ \ \ \ \ \ \ \ \ itor\ =\ hash\_table\_iter\_next(itor))\ \{}
\DoxyCodeLine{00134\ \ \ \ \ \ \ \ \ \ \ \ \ ckd\_free((\textcolor{keywordtype}{char}\ *)itor-\/>ent-\/>key);}
\DoxyCodeLine{00135\ \ \ \ \ \ \ \ \ \ \ \ \ jsgf\_rule\_free((jsgf\_rule\_t\ *)itor-\/>ent-\/>val);}
\DoxyCodeLine{00136\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00137\ \ \ \ \ \ \ \ \ hash\_table\_free(jsgf-\/>rules);}
\DoxyCodeLine{00138\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (itor\ =\ hash\_table\_iter(jsgf-\/>imports);\ itor;}
\DoxyCodeLine{00139\ \ \ \ \ \ \ \ \ \ \ \ \ \ itor\ =\ hash\_table\_iter\_next(itor))\ \{}
\DoxyCodeLine{00140\ \ \ \ \ \ \ \ \ \ \ \ \ ckd\_free((\textcolor{keywordtype}{char}\ *)itor-\/>ent-\/>key);}
\DoxyCodeLine{00141\ \ \ \ \ \ \ \ \ \ \ \ \ jsgf\_grammar\_free((jsgf\_t\ *)itor-\/>ent-\/>val);}
\DoxyCodeLine{00142\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00143\ \ \ \ \ \ \ \ \ hash\_table\_free(jsgf-\/>imports);}
\DoxyCodeLine{00144\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (gn\ =\ jsgf-\/>searchpath;\ gn;\ gn\ =\ gnode\_next(gn))}
\DoxyCodeLine{00145\ \ \ \ \ \ \ \ \ \ \ \ \ ckd\_free(gnode\_ptr(gn));}
\DoxyCodeLine{00146\ \ \ \ \ \ \ \ \ glist\_free(jsgf-\/>searchpath);}
\DoxyCodeLine{00147\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (gn\ =\ jsgf-\/>links;\ gn;\ gn\ =\ gnode\_next(gn))}
\DoxyCodeLine{00148\ \ \ \ \ \ \ \ \ \ \ \ \ ckd\_free(gnode\_ptr(gn));}
\DoxyCodeLine{00149\ \ \ \ \ \ \ \ \ glist\_free(jsgf-\/>links);}
\DoxyCodeLine{00150\ \ \ \ \ \}}
\DoxyCodeLine{00151\ \ \ \ \ ckd\_free(jsgf-\/>name);}
\DoxyCodeLine{00152\ \ \ \ \ ckd\_free(jsgf-\/>version);}
\DoxyCodeLine{00153\ \ \ \ \ ckd\_free(jsgf-\/>charset);}
\DoxyCodeLine{00154\ \ \ \ \ ckd\_free(jsgf-\/>locale);}
\DoxyCodeLine{00155\ \ \ \ \ ckd\_free(jsgf);}
\DoxyCodeLine{00156\ \}}
\DoxyCodeLine{00157\ }
\DoxyCodeLine{00158\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00159\ jsgf\_rhs\_free(jsgf\_rhs\_t\ *rhs)}
\DoxyCodeLine{00160\ \{}
\DoxyCodeLine{00161\ \ \ \ \ gnode\_t\ *gn;}
\DoxyCodeLine{00162\ }
\DoxyCodeLine{00163\ \ \ \ \ \textcolor{keywordflow}{if}\ (rhs\ ==\ NULL)}
\DoxyCodeLine{00164\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00165\ }
\DoxyCodeLine{00166\ \ \ \ \ jsgf\_rhs\_free(rhs-\/>alt);}
\DoxyCodeLine{00167\ \ \ \ \ \textcolor{keywordflow}{for}\ (gn\ =\ rhs-\/>atoms;\ gn;\ gn\ =\ gnode\_next(gn))}
\DoxyCodeLine{00168\ \ \ \ \ \ \ \ \ jsgf\_atom\_free(gnode\_ptr(gn));}
\DoxyCodeLine{00169\ \ \ \ \ glist\_free(rhs-\/>atoms);}
\DoxyCodeLine{00170\ \ \ \ \ ckd\_free(rhs);}
\DoxyCodeLine{00171\ \}}
\DoxyCodeLine{00172\ }
\DoxyCodeLine{00173\ jsgf\_atom\_t\ *}
\DoxyCodeLine{00174\ jsgf\_kleene\_new(jsgf\_t\ *jsgf,\ jsgf\_atom\_t\ *atom,\ \textcolor{keywordtype}{int}\ plus)}
\DoxyCodeLine{00175\ \{}
\DoxyCodeLine{00176\ \ \ \ \ jsgf\_rule\_t\ *rule;}
\DoxyCodeLine{00177\ \ \ \ \ jsgf\_atom\_t\ *rule\_atom;}
\DoxyCodeLine{00178\ \ \ \ \ jsgf\_rhs\_t\ *rhs;}
\DoxyCodeLine{00179\ }
\DoxyCodeLine{00180\ \ \ \ \ \textcolor{comment}{/*\ Generate\ an\ "{}internal"{}\ rule\ of\ the\ form\ (<NULL>\ |\ <name>\ <g0006>)\ */}}
\DoxyCodeLine{00181\ \ \ \ \ \textcolor{comment}{/*\ Or\ if\ plus\ is\ true,\ (<name>\ |\ <name>\ <g0006>)\ */}}
\DoxyCodeLine{00182\ \ \ \ \ rhs\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*rhs));}
\DoxyCodeLine{00183\ \ \ \ \ \textcolor{keywordflow}{if}\ (plus)}
\DoxyCodeLine{00184\ \ \ \ \ \ \ \ \ rhs-\/>atoms\ =\ glist\_add\_ptr(NULL,\ jsgf\_atom\_new(atom-\/>name,\ 1.0));}
\DoxyCodeLine{00185\ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00186\ \ \ \ \ \ \ \ \ rhs-\/>atoms\ =\ glist\_add\_ptr(NULL,\ jsgf\_atom\_new(\textcolor{stringliteral}{"{}<NULL>"{}},\ 1.0));}
\DoxyCodeLine{00187\ \ \ \ \ rule\ =\ jsgf\_define\_rule(jsgf,\ NULL,\ rhs,\ 0);}
\DoxyCodeLine{00188\ \ \ \ \ rule\_atom\ =\ jsgf\_atom\_new(rule-\/>name,\ 1.0);}
\DoxyCodeLine{00189\ \ \ \ \ rhs\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*rhs));}
\DoxyCodeLine{00190\ \ \ \ \ rhs-\/>atoms\ =\ glist\_add\_ptr(NULL,\ rule\_atom);}
\DoxyCodeLine{00191\ \ \ \ \ rhs-\/>atoms\ =\ glist\_add\_ptr(rhs-\/>atoms,\ atom);}
\DoxyCodeLine{00192\ \ \ \ \ rule-\/>rhs-\/>alt\ =\ rhs;}
\DoxyCodeLine{00193\ }
\DoxyCodeLine{00194\ \ \ \ \ \textcolor{keywordflow}{return}\ jsgf\_atom\_new(rule-\/>name,\ 1.0);}
\DoxyCodeLine{00195\ \}}
\DoxyCodeLine{00196\ }
\DoxyCodeLine{00197\ jsgf\_rule\_t\ *}
\DoxyCodeLine{00198\ jsgf\_optional\_new(jsgf\_t\ *jsgf,\ jsgf\_rhs\_t\ *exp)}
\DoxyCodeLine{00199\ \{}
\DoxyCodeLine{00200\ \ \ \ \ jsgf\_rhs\_t\ *rhs\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*rhs));}
\DoxyCodeLine{00201\ \ \ \ \ jsgf\_atom\_t\ *atom\ =\ jsgf\_atom\_new(\textcolor{stringliteral}{"{}<NULL>"{}},\ 1.0);}
\DoxyCodeLine{00202\ \ \ \ \ rhs-\/>alt\ =\ exp;}
\DoxyCodeLine{00203\ \ \ \ \ rhs-\/>atoms\ =\ glist\_add\_ptr(NULL,\ atom);}
\DoxyCodeLine{00204\ \ \ \ \ \textcolor{keywordflow}{return}\ jsgf\_define\_rule(jsgf,\ NULL,\ rhs,\ 0);}
\DoxyCodeLine{00205\ \}}
\DoxyCodeLine{00206\ }
\DoxyCodeLine{00207\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00208\ jsgf\_add\_link(jsgf\_t\ *grammar,\ jsgf\_atom\_t\ *atom,\ \textcolor{keywordtype}{int}\ from,\ \textcolor{keywordtype}{int}\ to)}
\DoxyCodeLine{00209\ \{}
\DoxyCodeLine{00210\ \ \ \ \ jsgf\_link\_t\ *link;}
\DoxyCodeLine{00211\ }
\DoxyCodeLine{00212\ \ \ \ \ link\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*link));}
\DoxyCodeLine{00213\ \ \ \ \ link-\/>from\ =\ from;}
\DoxyCodeLine{00214\ \ \ \ \ link-\/>to\ =\ to;}
\DoxyCodeLine{00215\ \ \ \ \ link-\/>atom\ =\ atom;}
\DoxyCodeLine{00216\ \ \ \ \ grammar-\/>links\ =\ glist\_add\_ptr(grammar-\/>links,\ link);}
\DoxyCodeLine{00217\ \}}
\DoxyCodeLine{00218\ }
\DoxyCodeLine{00219\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{char}\ *}
\DoxyCodeLine{00220\ extract\_grammar\_name(\textcolor{keywordtype}{char}\ *rule\_name)}
\DoxyCodeLine{00221\ \{}
\DoxyCodeLine{00222\ \ \ \ \ \textcolor{keywordtype}{char}*\ dot\_pos;}
\DoxyCodeLine{00223\ \ \ \ \ \textcolor{keywordtype}{char}*\ grammar\_name\ =\ ckd\_salloc(rule\_name+1);}
\DoxyCodeLine{00224\ \ \ \ \ \textcolor{keywordflow}{if}\ ((dot\_pos\ =\ strrchr(grammar\_name\ +\ 1,\ \textcolor{charliteral}{'.'}))\ ==\ NULL)\ \{}
\DoxyCodeLine{00225\ \ \ \ \ \ \ \ \ ckd\_free(grammar\_name);}
\DoxyCodeLine{00226\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00227\ \ \ \ \ \}}
\DoxyCodeLine{00228\ \ \ \ \ *dot\_pos=\textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{00229\ \ \ \ \ \textcolor{keywordflow}{return}\ grammar\_name;}
\DoxyCodeLine{00230\ \}}
\DoxyCodeLine{00231\ }
\DoxyCodeLine{00232\ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *}
\DoxyCodeLine{00233\ jsgf\_grammar\_name(jsgf\_t\ *jsgf)}
\DoxyCodeLine{00234\ \{}
\DoxyCodeLine{00235\ \ \ \ \ \textcolor{keywordflow}{return}\ jsgf-\/>name;}
\DoxyCodeLine{00236\ \}}
\DoxyCodeLine{00237\ }
\DoxyCodeLine{00238\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{char}\ *}
\DoxyCodeLine{00239\ jsgf\_fullname(jsgf\_t\ *jsgf,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *name)}
\DoxyCodeLine{00240\ \{}
\DoxyCodeLine{00241\ \ \ \ \ \textcolor{keywordtype}{char}\ *fullname;}
\DoxyCodeLine{00242\ }
\DoxyCodeLine{00243\ \ \ \ \ \textcolor{comment}{/*\ Check\ if\ it\ is\ already\ qualified\ */}}
\DoxyCodeLine{00244\ \ \ \ \ \textcolor{keywordflow}{if}\ (strchr(name\ +\ 1,\ \textcolor{charliteral}{'.'}))}
\DoxyCodeLine{00245\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ ckd\_salloc(name);}
\DoxyCodeLine{00246\ }
\DoxyCodeLine{00247\ \ \ \ \ \textcolor{comment}{/*\ Skip\ leading\ <\ in\ name\ */}}
\DoxyCodeLine{00248\ \ \ \ \ fullname\ =\ ckd\_malloc(strlen(jsgf-\/>name)\ +\ strlen(name)\ +\ 4);}
\DoxyCodeLine{00249\ \ \ \ \ sprintf(fullname,\ \textcolor{stringliteral}{"{}<\%s.\%s"{}},\ jsgf-\/>name,\ name\ +\ 1);}
\DoxyCodeLine{00250\ \ \ \ \ \textcolor{keywordflow}{return}\ fullname;}
\DoxyCodeLine{00251\ \}}
\DoxyCodeLine{00252\ }
\DoxyCodeLine{00253\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{char}\ *}
\DoxyCodeLine{00254\ jsgf\_fullname\_from\_rule(jsgf\_rule\_t\ *rule,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *name)}
\DoxyCodeLine{00255\ \{}
\DoxyCodeLine{00256\ \ \ \ \ \textcolor{keywordtype}{char}\ *fullname,\ *grammar\_name;}
\DoxyCodeLine{00257\ }
\DoxyCodeLine{00258\ \ \ \ \ \textcolor{comment}{/*\ Check\ if\ it\ is\ already\ qualified\ */}}
\DoxyCodeLine{00259\ \ \ \ \ \textcolor{keywordflow}{if}\ (strchr(name\ +\ 1,\ \textcolor{charliteral}{'.'}))}
\DoxyCodeLine{00260\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ ckd\_salloc(name);}
\DoxyCodeLine{00261\ }
\DoxyCodeLine{00262\ \ \ \ \ \textcolor{comment}{/*\ Skip\ leading\ <\ in\ name\ */}}
\DoxyCodeLine{00263\ \ \ \ \ \textcolor{keywordflow}{if}\ ((grammar\_name\ =\ extract\_grammar\_name(rule-\/>name))\ ==\ NULL)}
\DoxyCodeLine{00264\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ ckd\_salloc(name);}
\DoxyCodeLine{00265\ \ \ \ \ fullname\ =\ ckd\_malloc(strlen(grammar\_name)\ +\ strlen(name)\ +\ 4);}
\DoxyCodeLine{00266\ \ \ \ \ sprintf(fullname,\ \textcolor{stringliteral}{"{}<\%s.\%s"{}},\ grammar\_name,\ name\ +\ 1);}
\DoxyCodeLine{00267\ \ \ \ \ ckd\_free(grammar\_name);}
\DoxyCodeLine{00268\ }
\DoxyCodeLine{00269\ \ \ \ \ \textcolor{keywordflow}{return}\ fullname;}
\DoxyCodeLine{00270\ \}}
\DoxyCodeLine{00271\ }
\DoxyCodeLine{00272\ \textcolor{comment}{/*\ Extract\ as\ rulename\ everything\ after\ the\ secondlast\ dot,\ if\ existent.\ }}
\DoxyCodeLine{00273\ \textcolor{comment}{\ *\ Because\ everything\ before\ the\ secondlast\ dot\ is\ the\ path-\/specification.\ */}}
\DoxyCodeLine{00274\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{char}\ *}
\DoxyCodeLine{00275\ importname2rulename(\textcolor{keywordtype}{char}\ *importname)}
\DoxyCodeLine{00276\ \{}
\DoxyCodeLine{00277\ \ \ \ \ \textcolor{keywordtype}{char}\ *rulename\ =\ ckd\_salloc(importname);}
\DoxyCodeLine{00278\ \ \ \ \ \textcolor{keywordtype}{char}\ *last\_dotpos;}
\DoxyCodeLine{00279\ \ \ \ \ \textcolor{keywordtype}{char}\ *secondlast\_dotpos;}
\DoxyCodeLine{00280\ }
\DoxyCodeLine{00281\ \ \ \ \ \textcolor{keywordflow}{if}\ ((last\_dotpos\ =\ strrchr(rulename+1,\ \textcolor{charliteral}{'.'}))\ !=\ NULL)\ \{}
\DoxyCodeLine{00282\ \ \ \ \ \ \ \ \ *last\_dotpos=\textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{00283\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((secondlast\_dotpos\ =\ strrchr(rulename+1,\ \textcolor{charliteral}{'.'}))\ !=\ NULL)\ \{}
\DoxyCodeLine{00284\ \ \ \ \ \ \ \ \ \ \ \ \ *last\_dotpos=\textcolor{charliteral}{'.'};}
\DoxyCodeLine{00285\ \ \ \ \ \ \ \ \ \ \ \ \ *secondlast\_dotpos=\textcolor{charliteral}{'<'};}
\DoxyCodeLine{00286\ \ \ \ \ \ \ \ \ \ \ \ \ secondlast\_dotpos\ =\ ckd\_salloc(secondlast\_dotpos);}
\DoxyCodeLine{00287\ \ \ \ \ \ \ \ \ \ \ \ \ ckd\_free(rulename);}
\DoxyCodeLine{00288\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ secondlast\_dotpos;}
\DoxyCodeLine{00289\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00290\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00291\ \ \ \ \ \ \ \ \ \ \ \ \ *last\_dotpos=\textcolor{charliteral}{'.'};}
\DoxyCodeLine{00292\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ rulename;}
\DoxyCodeLine{00293\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00294\ \ \ \ \ \}}
\DoxyCodeLine{00295\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00296\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ rulename;}
\DoxyCodeLine{00297\ \ \ \ \ \}}
\DoxyCodeLine{00298\ \}}
\DoxyCodeLine{00299\ }
\DoxyCodeLine{00300\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{int}\ expand\_rule(jsgf\_t\ *grammar,\ jsgf\_rule\_t\ *rule);}
\DoxyCodeLine{00301\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00302\ expand\_rhs(jsgf\_t\ *grammar,\ jsgf\_rule\_t\ *rule,\ jsgf\_rhs\_t\ *rhs)}
\DoxyCodeLine{00303\ \{}
\DoxyCodeLine{00304\ \ \ \ \ gnode\_t\ *gn;}
\DoxyCodeLine{00305\ \ \ \ \ \textcolor{keywordtype}{int}\ lastnode;}
\DoxyCodeLine{00306\ }
\DoxyCodeLine{00307\ \ \ \ \ \textcolor{comment}{/*\ Last\ node\ expanded\ in\ this\ sequence.\ */}}
\DoxyCodeLine{00308\ \ \ \ \ lastnode\ =\ rule-\/>entry;}
\DoxyCodeLine{00309\ }
\DoxyCodeLine{00310\ \ \ \ \ \textcolor{comment}{/*\ Iterate\ over\ atoms\ in\ rhs\ and\ generate\ links/nodes\ */}}
\DoxyCodeLine{00311\ \ \ \ \ \textcolor{keywordflow}{for}\ (gn\ =\ rhs-\/>atoms;\ gn;\ gn\ =\ gnode\_next(gn))\ \{}
\DoxyCodeLine{00312\ \ \ \ \ \ \ \ \ jsgf\_atom\_t\ *atom\ =\ gnode\_ptr(gn);}
\DoxyCodeLine{00313\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (jsgf\_atom\_is\_rule(atom))\ \{}
\DoxyCodeLine{00314\ \ \ \ \ \ \ \ \ \ \ \ \ jsgf\_rule\_t\ *subrule;}
\DoxyCodeLine{00315\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ *fullname;}
\DoxyCodeLine{00316\ \ \ \ \ \ \ \ \ \ \ \ \ gnode\_t\ *subnode;}
\DoxyCodeLine{00317\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{void}\ *val;}
\DoxyCodeLine{00318\ }
\DoxyCodeLine{00319\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Special\ case\ for\ <NULL>\ and\ <VOID>\ pseudo-\/rules\ */}}
\DoxyCodeLine{00320\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (0\ ==\ strcmp(atom-\/>name,\ \textcolor{stringliteral}{"{}<NULL>"{}}))\ \{}
\DoxyCodeLine{00321\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Emit\ a\ NULL\ transition\ */}}
\DoxyCodeLine{00322\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ jsgf\_add\_link(grammar,\ atom,}
\DoxyCodeLine{00323\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lastnode,\ grammar-\/>nstate);}
\DoxyCodeLine{00324\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lastnode\ =\ grammar-\/>nstate;}
\DoxyCodeLine{00325\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ++grammar-\/>nstate;}
\DoxyCodeLine{00326\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{continue};}
\DoxyCodeLine{00327\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00328\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (0\ ==\ strcmp(atom-\/>name,\ \textcolor{stringliteral}{"{}<VOID>"{}}))\ \{}
\DoxyCodeLine{00329\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Make\ this\ entire\ RHS\ unspeakable\ */}}
\DoxyCodeLine{00330\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00331\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00332\ }
\DoxyCodeLine{00333\ \ \ \ \ \ \ \ \ \ \ \ \ fullname\ =\ jsgf\_fullname\_from\_rule(rule,\ atom-\/>name);}
\DoxyCodeLine{00334\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (hash\_table\_lookup(grammar-\/>rules,\ fullname,\ \&val)\ ==\ -\/1)\ \{}
\DoxyCodeLine{00335\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Undefined\ rule\ in\ RHS:\ \%s\(\backslash\)n"{}},\ fullname);}
\DoxyCodeLine{00336\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ckd\_free(fullname);}
\DoxyCodeLine{00337\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00338\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00339\ \ \ \ \ \ \ \ \ \ \ \ \ ckd\_free(fullname);}
\DoxyCodeLine{00340\ \ \ \ \ \ \ \ \ \ \ \ \ subrule\ =\ val;}
\DoxyCodeLine{00341\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Look\ for\ this\ in\ the\ stack\ of\ expanded\ rules\ */}}
\DoxyCodeLine{00342\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (subnode\ =\ grammar-\/>rulestack;\ subnode;\ subnode\ =\ gnode\_next(subnode))}
\DoxyCodeLine{00343\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (gnode\_ptr(subnode)\ ==\ (\textcolor{keywordtype}{void}\ *)subrule)}
\DoxyCodeLine{00344\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00345\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (subnode\ !=\ NULL)\ \{}
\DoxyCodeLine{00346\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Allow\ right-\/recursion\ only.\ */}}
\DoxyCodeLine{00347\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (gnode\_next(gn)\ !=\ NULL)\ \{}
\DoxyCodeLine{00348\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Only\ right-\/recursion\ is\ permitted\ (in\ \%s.\%s)\(\backslash\)n"{}},}
\DoxyCodeLine{00349\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ grammar-\/>name,\ rule-\/>name);}
\DoxyCodeLine{00350\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00351\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00352\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Add\ a\ link\ back\ to\ the\ beginning\ of\ this\ rule\ instance\ */}}
\DoxyCodeLine{00353\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Right\ recursion\ \%s\ \%d\ =>\ \%d\(\backslash\)n"{}},\ atom-\/>name,\ lastnode,\ subrule-\/>entry);}
\DoxyCodeLine{00354\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ jsgf\_add\_link(grammar,\ atom,\ lastnode,\ subrule-\/>entry);}
\DoxyCodeLine{00355\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00356\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00357\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Expand\ the\ subrule\ */}}
\DoxyCodeLine{00358\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (expand\_rule(grammar,\ subrule)\ ==\ -\/1)}
\DoxyCodeLine{00359\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00360\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Add\ a\ link\ into\ the\ subrule.\ */}}
\DoxyCodeLine{00361\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ jsgf\_add\_link(grammar,\ atom,}
\DoxyCodeLine{00362\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lastnode,\ subrule-\/>entry);}
\DoxyCodeLine{00363\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lastnode\ =\ subrule-\/>exit;}
\DoxyCodeLine{00364\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00365\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00366\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00367\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Add\ a\ link\ for\ this\ token\ and\ create\ a\ new\ exit\ node.\ */}}
\DoxyCodeLine{00368\ \ \ \ \ \ \ \ \ \ \ \ \ jsgf\_add\_link(grammar,\ atom,}
\DoxyCodeLine{00369\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lastnode,\ grammar-\/>nstate);}
\DoxyCodeLine{00370\ \ \ \ \ \ \ \ \ \ \ \ \ lastnode\ =\ grammar-\/>nstate;}
\DoxyCodeLine{00371\ \ \ \ \ \ \ \ \ \ \ \ \ ++grammar-\/>nstate;}
\DoxyCodeLine{00372\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00373\ \ \ \ \ \}}
\DoxyCodeLine{00374\ }
\DoxyCodeLine{00375\ \ \ \ \ \textcolor{keywordflow}{return}\ lastnode;}
\DoxyCodeLine{00376\ \}}
\DoxyCodeLine{00377\ }
\DoxyCodeLine{00378\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00379\ expand\_rule(jsgf\_t\ *grammar,\ jsgf\_rule\_t\ *rule)}
\DoxyCodeLine{00380\ \{}
\DoxyCodeLine{00381\ \ \ \ \ jsgf\_rhs\_t\ *rhs;}
\DoxyCodeLine{00382\ \ \ \ \ \textcolor{keywordtype}{float}\ norm;}
\DoxyCodeLine{00383\ }
\DoxyCodeLine{00384\ \ \ \ \ \textcolor{comment}{/*\ Push\ this\ rule\ onto\ the\ stack\ */}}
\DoxyCodeLine{00385\ \ \ \ \ grammar-\/>rulestack\ =\ glist\_add\_ptr(grammar-\/>rulestack,\ rule);}
\DoxyCodeLine{00386\ }
\DoxyCodeLine{00387\ \ \ \ \ \textcolor{comment}{/*\ Normalize\ weights\ for\ all\ alternatives\ exiting\ rule-\/>entry\ */}}
\DoxyCodeLine{00388\ \ \ \ \ norm\ =\ 0;}
\DoxyCodeLine{00389\ \ \ \ \ \textcolor{keywordflow}{for}\ (rhs\ =\ rule-\/>rhs;\ rhs;\ rhs\ =\ rhs-\/>alt)\ \{}
\DoxyCodeLine{00390\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (rhs-\/>atoms)\ \{}
\DoxyCodeLine{00391\ \ \ \ \ \ \ \ \ \ \ \ \ jsgf\_atom\_t\ *atom\ =\ gnode\_ptr(rhs-\/>atoms);}
\DoxyCodeLine{00392\ \ \ \ \ \ \ \ \ \ \ \ \ norm\ +=\ atom-\/>weight;}
\DoxyCodeLine{00393\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00394\ \ \ \ \ \}}
\DoxyCodeLine{00395\ }
\DoxyCodeLine{00396\ \ \ \ \ rule-\/>entry\ =\ grammar-\/>nstate++;}
\DoxyCodeLine{00397\ \ \ \ \ rule-\/>exit\ =\ grammar-\/>nstate++;}
\DoxyCodeLine{00398\ \ \ \ \ \textcolor{keywordflow}{if}\ (norm\ ==\ 0)\ norm\ =\ 1;}
\DoxyCodeLine{00399\ \ \ \ \ \textcolor{keywordflow}{for}\ (rhs\ =\ rule-\/>rhs;\ rhs;\ rhs\ =\ rhs-\/>alt)\ \{}
\DoxyCodeLine{00400\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ lastnode;}
\DoxyCodeLine{00401\ }
\DoxyCodeLine{00402\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (rhs-\/>atoms)\ \{}
\DoxyCodeLine{00403\ \ \ \ \ \ \ \ \ \ \ \ \ jsgf\_atom\_t\ *atom\ =\ gnode\_ptr(rhs-\/>atoms);}
\DoxyCodeLine{00404\ \ \ \ \ \ \ \ \ \ \ \ \ atom-\/>weight\ /=\ norm;}
\DoxyCodeLine{00405\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00406\ \ \ \ \ \ \ \ \ lastnode\ =\ expand\_rhs(grammar,\ rule,\ rhs);}
\DoxyCodeLine{00407\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (lastnode\ ==\ -\/1)\ \{}
\DoxyCodeLine{00408\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00409\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00410\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00411\ \ \ \ \ \ \ \ \ \ \ \ \ jsgf\_add\_link(grammar,\ NULL,\ lastnode,\ rule-\/>exit);}
\DoxyCodeLine{00412\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00413\ \ \ \ \ \}}
\DoxyCodeLine{00414\ }
\DoxyCodeLine{00415\ \ \ \ \ \textcolor{comment}{/*\ Pop\ this\ rule\ from\ the\ rule\ stack\ */}}
\DoxyCodeLine{00416\ \ \ \ \ grammar-\/>rulestack\ =\ gnode\_free(grammar-\/>rulestack,\ NULL);}
\DoxyCodeLine{00417\ \ \ \ \ \textcolor{keywordflow}{return}\ rule-\/>exit;}
\DoxyCodeLine{00418\ \}}
\DoxyCodeLine{00419\ }
\DoxyCodeLine{00420\ jsgf\_rule\_iter\_t\ *}
\DoxyCodeLine{00421\ jsgf\_rule\_iter(jsgf\_t\ *grammar)}
\DoxyCodeLine{00422\ \{}
\DoxyCodeLine{00423\ \ \ \ \ \textcolor{keywordflow}{return}\ hash\_table\_iter(grammar-\/>rules);}
\DoxyCodeLine{00424\ \}}
\DoxyCodeLine{00425\ }
\DoxyCodeLine{00426\ jsgf\_rule\_t\ *}
\DoxyCodeLine{00427\ jsgf\_get\_rule(jsgf\_t\ *grammar,\ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *name)}
\DoxyCodeLine{00428\ \{}
\DoxyCodeLine{00429\ \ \ \ \ \textcolor{keywordtype}{void}\ *val;}
\DoxyCodeLine{00430\ }
\DoxyCodeLine{00431\ \ \ \ \ \textcolor{keywordflow}{if}\ (hash\_table\_lookup(grammar-\/>rules,\ name,\ \&val)\ <\ 0)}
\DoxyCodeLine{00432\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00433\ \ \ \ \ \textcolor{keywordflow}{return}\ (jsgf\_rule\_t\ *)val;}
\DoxyCodeLine{00434\ \}}
\DoxyCodeLine{00435\ }
\DoxyCodeLine{00436\ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *}
\DoxyCodeLine{00437\ jsgf\_rule\_name(jsgf\_rule\_t\ *rule)}
\DoxyCodeLine{00438\ \{}
\DoxyCodeLine{00439\ \ \ \ \ \textcolor{keywordflow}{return}\ rule-\/>name;}
\DoxyCodeLine{00440\ \}}
\DoxyCodeLine{00441\ }
\DoxyCodeLine{00442\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00443\ jsgf\_rule\_public(jsgf\_rule\_t\ *rule)}
\DoxyCodeLine{00444\ \{}
\DoxyCodeLine{00445\ \ \ \ \ \textcolor{keywordflow}{return}\ rule-\/>public;}
\DoxyCodeLine{00446\ \}}
\DoxyCodeLine{00447\ }
\DoxyCodeLine{00448\ \textcolor{keyword}{static}\ fsg\_model\_t\ *}
\DoxyCodeLine{00449\ jsgf\_build\_fsg\_internal(jsgf\_t\ *grammar,\ jsgf\_rule\_t\ *rule,}
\DoxyCodeLine{00450\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ logmath\_t\ *lmath,\ float32\ lw,\ \textcolor{keywordtype}{int}\ do\_closure)}
\DoxyCodeLine{00451\ \{}
\DoxyCodeLine{00452\ \ \ \ \ fsg\_model\_t\ *fsg;}
\DoxyCodeLine{00453\ \ \ \ \ glist\_t\ nulls;}
\DoxyCodeLine{00454\ \ \ \ \ gnode\_t\ *gn;}
\DoxyCodeLine{00455\ }
\DoxyCodeLine{00456\ \ \ \ \ \textcolor{comment}{/*\ Clear\ previous\ links\ */}}
\DoxyCodeLine{00457\ \ \ \ \ \textcolor{keywordflow}{for}\ (gn\ =\ grammar-\/>links;\ gn;\ gn\ =\ gnode\_next(gn))\ \{}
\DoxyCodeLine{00458\ \ \ \ \ \ \ \ \ ckd\_free(gnode\_ptr(gn));}
\DoxyCodeLine{00459\ \ \ \ \ \}}
\DoxyCodeLine{00460\ \ \ \ \ glist\_free(grammar-\/>links);}
\DoxyCodeLine{00461\ \ \ \ \ grammar-\/>links\ =\ NULL;}
\DoxyCodeLine{00462\ \ \ \ \ rule-\/>entry\ =\ rule-\/>exit\ =\ 0;}
\DoxyCodeLine{00463\ \ \ \ \ grammar-\/>nstate\ =\ 0;}
\DoxyCodeLine{00464\ \ \ \ \ expand\_rule(grammar,\ rule);}
\DoxyCodeLine{00465\ }
\DoxyCodeLine{00466\ \ \ \ \ fsg\ =\ fsg\_model\_init(rule-\/>name,\ lmath,\ lw,\ grammar-\/>nstate);}
\DoxyCodeLine{00467\ \ \ \ \ fsg-\/>start\_state\ =\ rule-\/>entry;}
\DoxyCodeLine{00468\ \ \ \ \ fsg-\/>final\_state\ =\ rule-\/>exit;}
\DoxyCodeLine{00469\ \ \ \ \ grammar-\/>links\ =\ glist\_reverse(grammar-\/>links);}
\DoxyCodeLine{00470\ \ \ \ \ \textcolor{keywordflow}{for}\ (gn\ =\ grammar-\/>links;\ gn;\ gn\ =\ gnode\_next(gn))\ \{}
\DoxyCodeLine{00471\ \ \ \ \ \ \ \ \ jsgf\_link\_t\ *link\ =\ gnode\_ptr(gn);}
\DoxyCodeLine{00472\ }
\DoxyCodeLine{00473\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (link-\/>atom)\ \{}
\DoxyCodeLine{00474\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (jsgf\_atom\_is\_rule(link-\/>atom))\ \{}
\DoxyCodeLine{00475\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fsg\_model\_null\_trans\_add(fsg,\ link-\/>from,\ link-\/>to,}
\DoxyCodeLine{00476\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ logmath\_log(lmath,\ link-\/>atom-\/>weight));}
\DoxyCodeLine{00477\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00478\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00479\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ wid\ =\ fsg\_model\_word\_add(fsg,\ link-\/>atom-\/>name);}
\DoxyCodeLine{00480\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fsg\_model\_trans\_add(fsg,\ link-\/>from,\ link-\/>to,}
\DoxyCodeLine{00481\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ logmath\_log(lmath,\ link-\/>atom-\/>weight),\ wid);}
\DoxyCodeLine{00482\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00483\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00484\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00485\ \ \ \ \ \ \ \ \ \ \ \ \ fsg\_model\_null\_trans\_add(fsg,\ link-\/>from,\ link-\/>to,\ 0);}
\DoxyCodeLine{00486\ \ \ \ \ \ \ \ \ \}\ \ \ \ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00487\ \ \ \ \ \}}
\DoxyCodeLine{00488\ \ \ \ \ \textcolor{keywordflow}{if}\ (do\_closure)\ \{}
\DoxyCodeLine{00489\ \ \ \ \ \ \ \ \ nulls\ =\ fsg\_model\_null\_trans\_closure(fsg,\ NULL);}
\DoxyCodeLine{00490\ \ \ \ \ \ \ \ \ glist\_free(nulls);}
\DoxyCodeLine{00491\ \ \ \ \ \}}
\DoxyCodeLine{00492\ }
\DoxyCodeLine{00493\ \ \ \ \ \textcolor{keywordflow}{return}\ fsg;}
\DoxyCodeLine{00494\ \}}
\DoxyCodeLine{00495\ }
\DoxyCodeLine{00496\ fsg\_model\_t\ *}
\DoxyCodeLine{00497\ jsgf\_build\_fsg(jsgf\_t\ *grammar,\ jsgf\_rule\_t\ *rule,}
\DoxyCodeLine{00498\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ logmath\_t\ *lmath,\ float32\ lw)}
\DoxyCodeLine{00499\ \{}
\DoxyCodeLine{00500\ \ \ \ \ \textcolor{keywordflow}{return}\ jsgf\_build\_fsg\_internal(grammar,\ rule,\ lmath,\ lw,\ TRUE);}
\DoxyCodeLine{00501\ \}}
\DoxyCodeLine{00502\ }
\DoxyCodeLine{00503\ fsg\_model\_t\ *}
\DoxyCodeLine{00504\ jsgf\_build\_fsg\_raw(jsgf\_t\ *grammar,\ jsgf\_rule\_t\ *rule,}
\DoxyCodeLine{00505\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ logmath\_t\ *lmath,\ float32\ lw)}
\DoxyCodeLine{00506\ \{}
\DoxyCodeLine{00507\ \ \ \ \ \textcolor{keywordflow}{return}\ jsgf\_build\_fsg\_internal(grammar,\ rule,\ lmath,\ lw,\ FALSE);}
\DoxyCodeLine{00508\ \}}
\DoxyCodeLine{00509\ }
\DoxyCodeLine{00510\ fsg\_model\_t\ *}
\DoxyCodeLine{00511\ jsgf\_read\_file(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *file,\ logmath\_t\ *\ lmath,\ float32\ lw)}
\DoxyCodeLine{00512\ \{}
\DoxyCodeLine{00513\ \ \ \ \ fsg\_model\_t\ *fsg;}
\DoxyCodeLine{00514\ \ \ \ \ jsgf\_rule\_t\ *rule;}
\DoxyCodeLine{00515\ \ \ \ \ jsgf\_t\ *jsgf;}
\DoxyCodeLine{00516\ \ \ \ \ jsgf\_rule\_iter\_t\ *itor;}
\DoxyCodeLine{00517\ }
\DoxyCodeLine{00518\ \ \ \ \ \textcolor{keywordflow}{if}\ ((jsgf\ =\ jsgf\_parse\_file(file,\ NULL))\ ==\ NULL)\ \{}
\DoxyCodeLine{00519\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Error\ parsing\ file:\ \%s\(\backslash\)n"{}},\ file);}
\DoxyCodeLine{00520\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00521\ \ \ \ \ \}}
\DoxyCodeLine{00522\ }
\DoxyCodeLine{00523\ \ \ \ \ rule\ =\ NULL;}
\DoxyCodeLine{00524\ \ \ \ \ \textcolor{keywordflow}{for}\ (itor\ =\ jsgf\_rule\_iter(jsgf);\ itor;}
\DoxyCodeLine{00525\ \ \ \ \ \ \ \ \ \ itor\ =\ jsgf\_rule\_iter\_next(itor))\ \{}
\DoxyCodeLine{00526\ \ \ \ \ \ \ \ \ rule\ =\ jsgf\_rule\_iter\_rule(itor);}
\DoxyCodeLine{00527\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (jsgf\_rule\_public(rule))\ \{}
\DoxyCodeLine{00528\ \ \ \ \ \ \ \ \ \ \ \ \ jsgf\_rule\_iter\_free(itor);}
\DoxyCodeLine{00529\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00530\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00531\ \ \ \ \ \}}
\DoxyCodeLine{00532\ \ \ \ \ \textcolor{keywordflow}{if}\ (rule\ ==\ NULL)\ \{}
\DoxyCodeLine{00533\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}No\ public\ rules\ found\ in\ \%s\(\backslash\)n"{}},\ file);}
\DoxyCodeLine{00534\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00535\ \ \ \ \ \}}
\DoxyCodeLine{00536\ \ \ \ \ fsg\ =\ jsgf\_build\_fsg(jsgf,\ rule,\ lmath,\ lw);}
\DoxyCodeLine{00537\ \ \ \ \ jsgf\_grammar\_free(jsgf);}
\DoxyCodeLine{00538\ \ \ \ \ \textcolor{keywordflow}{return}\ fsg;}
\DoxyCodeLine{00539\ \}}
\DoxyCodeLine{00540\ }
\DoxyCodeLine{00541\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00542\ jsgf\_write\_fsg(jsgf\_t\ *grammar,\ jsgf\_rule\_t\ *rule,\ FILE\ *outfh)}
\DoxyCodeLine{00543\ \{}
\DoxyCodeLine{00544\ \ \ \ \ fsg\_model\_t\ *fsg;}
\DoxyCodeLine{00545\ \ \ \ \ logmath\_t\ *lmath\ =\ logmath\_init(1.0001,\ 0,\ 0);}
\DoxyCodeLine{00546\ }
\DoxyCodeLine{00547\ \ \ \ \ \textcolor{keywordflow}{if}\ ((fsg\ =\ jsgf\_build\_fsg\_raw(grammar,\ rule,\ lmath,\ 1.0))\ ==\ NULL)}
\DoxyCodeLine{00548\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ error\_out;}
\DoxyCodeLine{00549\ }
\DoxyCodeLine{00550\ \ \ \ \ fsg\_model\_write(fsg,\ outfh);}
\DoxyCodeLine{00551\ \ \ \ \ logmath\_free(lmath);}
\DoxyCodeLine{00552\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00553\ }
\DoxyCodeLine{00554\ error\_out:}
\DoxyCodeLine{00555\ \ \ \ \ logmath\_free(lmath);}
\DoxyCodeLine{00556\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00557\ \}}
\DoxyCodeLine{00558\ jsgf\_rule\_t\ *}
\DoxyCodeLine{00559\ jsgf\_define\_rule(jsgf\_t\ *jsgf,\ \textcolor{keywordtype}{char}\ *name,\ jsgf\_rhs\_t\ *rhs,\ \textcolor{keywordtype}{int}\ \textcolor{keyword}{public})}
\DoxyCodeLine{00560\ \{}
\DoxyCodeLine{00561\ \ \ \ \ jsgf\_rule\_t\ *rule;}
\DoxyCodeLine{00562\ \ \ \ \ \textcolor{keywordtype}{void}\ *val;}
\DoxyCodeLine{00563\ }
\DoxyCodeLine{00564\ \ \ \ \ \textcolor{keywordflow}{if}\ (name\ ==\ NULL)\ \{}
\DoxyCodeLine{00565\ \ \ \ \ \ \ \ \ name\ =\ ckd\_malloc(strlen(jsgf-\/>name)\ +\ 16);}
\DoxyCodeLine{00566\ \ \ \ \ \ \ \ \ sprintf(name,\ \textcolor{stringliteral}{"{}<\%s.g\%05d>"{}},\ jsgf-\/>name,\ hash\_table\_inuse(jsgf-\/>rules));}
\DoxyCodeLine{00567\ \ \ \ \ \}}
\DoxyCodeLine{00568\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00569\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ *newname;}
\DoxyCodeLine{00570\ }
\DoxyCodeLine{00571\ \ \ \ \ \ \ \ \ newname\ =\ jsgf\_fullname(jsgf,\ name);}
\DoxyCodeLine{00572\ \ \ \ \ \ \ \ \ name\ =\ newname;}
\DoxyCodeLine{00573\ \ \ \ \ \}}
\DoxyCodeLine{00574\ }
\DoxyCodeLine{00575\ \ \ \ \ rule\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*rule));}
\DoxyCodeLine{00576\ \ \ \ \ rule-\/>refcnt\ =\ 1;}
\DoxyCodeLine{00577\ \ \ \ \ rule-\/>name\ =\ ckd\_salloc(name);}
\DoxyCodeLine{00578\ \ \ \ \ rule-\/>rhs\ =\ rhs;}
\DoxyCodeLine{00579\ \ \ \ \ rule-\/>public\ =\ \textcolor{keyword}{public};}
\DoxyCodeLine{00580\ }
\DoxyCodeLine{00581\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Defined\ rule:\ \%s\%s\(\backslash\)n"{}},}
\DoxyCodeLine{00582\ \ \ \ \ \ \ \ \ \ \ \ rule-\/>public\ ?\ \textcolor{stringliteral}{"{}PUBLIC\ "{}}\ :\ \textcolor{stringliteral}{"{}"{}},}
\DoxyCodeLine{00583\ \ \ \ \ \ \ \ \ \ \ \ rule-\/>name);}
\DoxyCodeLine{00584\ \ \ \ \ val\ =\ hash\_table\_enter(jsgf-\/>rules,\ name,\ rule);}
\DoxyCodeLine{00585\ \ \ \ \ \textcolor{keywordflow}{if}\ (val\ !=\ (\textcolor{keywordtype}{void}\ *)rule)\ \{}
\DoxyCodeLine{00586\ \ \ \ \ \ \ \ \ E\_WARN(\textcolor{stringliteral}{"{}Multiply\ defined\ symbol:\ \%s\(\backslash\)n"{}},\ name);}
\DoxyCodeLine{00587\ \ \ \ \ \}}
\DoxyCodeLine{00588\ \ \ \ \ \textcolor{keywordflow}{return}\ rule;}
\DoxyCodeLine{00589\ \}}
\DoxyCodeLine{00590\ }
\DoxyCodeLine{00591\ jsgf\_rule\_t\ *}
\DoxyCodeLine{00592\ jsgf\_rule\_retain(jsgf\_rule\_t\ *rule)}
\DoxyCodeLine{00593\ \{}
\DoxyCodeLine{00594\ \ \ \ \ ++rule-\/>refcnt;}
\DoxyCodeLine{00595\ \ \ \ \ \textcolor{keywordflow}{return}\ rule;}
\DoxyCodeLine{00596\ \}}
\DoxyCodeLine{00597\ }
\DoxyCodeLine{00598\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00599\ jsgf\_rule\_free(jsgf\_rule\_t\ *rule)}
\DoxyCodeLine{00600\ \{}
\DoxyCodeLine{00601\ \ \ \ \ \textcolor{keywordflow}{if}\ (rule\ ==\ NULL)}
\DoxyCodeLine{00602\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00603\ \ \ \ \ \textcolor{keywordflow}{if}\ (-\/-\/rule-\/>refcnt\ >\ 0)}
\DoxyCodeLine{00604\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ rule-\/>refcnt;}
\DoxyCodeLine{00605\ \ \ \ \ jsgf\_rhs\_free(rule-\/>rhs);}
\DoxyCodeLine{00606\ \ \ \ \ ckd\_free(rule-\/>name);}
\DoxyCodeLine{00607\ \ \ \ \ ckd\_free(rule);}
\DoxyCodeLine{00608\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00609\ \}}
\DoxyCodeLine{00610\ }
\DoxyCodeLine{00611\ }
\DoxyCodeLine{00612\ \textcolor{comment}{/*\ FIXME:\ This\ should\ go\ in\ libsphinxutil\ */}}
\DoxyCodeLine{00613\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{char}\ *}
\DoxyCodeLine{00614\ path\_list\_search(glist\_t\ paths,\ \textcolor{keywordtype}{char}\ *path)}
\DoxyCodeLine{00615\ \{}
\DoxyCodeLine{00616\ \ \ \ \ gnode\_t\ *gn;}
\DoxyCodeLine{00617\ }
\DoxyCodeLine{00618\ \ \ \ \ \textcolor{keywordflow}{for}\ (gn\ =\ paths;\ gn;\ gn\ =\ gnode\_next(gn))\ \{}
\DoxyCodeLine{00619\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ *fullpath;}
\DoxyCodeLine{00620\ \ \ \ \ \ \ \ \ FILE\ *tmp;}
\DoxyCodeLine{00621\ }
\DoxyCodeLine{00622\ \ \ \ \ \ \ \ \ fullpath\ =\ string\_join(gnode\_ptr(gn),\ \textcolor{stringliteral}{"{}/"{}},\ path,\ NULL);}
\DoxyCodeLine{00623\ \ \ \ \ \ \ \ \ tmp\ =\ fopen(fullpath,\ \textcolor{stringliteral}{"{}r"{}});}
\DoxyCodeLine{00624\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (tmp\ !=\ NULL)\ \{}
\DoxyCodeLine{00625\ \ \ \ \ \ \ \ \ \ \ \ \ fclose(tmp);}
\DoxyCodeLine{00626\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ fullpath;}
\DoxyCodeLine{00627\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00628\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00629\ \ \ \ \ \ \ \ \ \ \ \ \ ckd\_free(fullpath);}
\DoxyCodeLine{00630\ \ \ \ \ \}}
\DoxyCodeLine{00631\ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00632\ \}}
\DoxyCodeLine{00633\ }
\DoxyCodeLine{00634\ jsgf\_rule\_t\ *}
\DoxyCodeLine{00635\ jsgf\_import\_rule(jsgf\_t\ *jsgf,\ \textcolor{keywordtype}{char}\ *name)}
\DoxyCodeLine{00636\ \{}
\DoxyCodeLine{00637\ \ \ \ \ \textcolor{keywordtype}{char}\ *c,\ *path,\ *newpath;}
\DoxyCodeLine{00638\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ namelen,\ packlen;}
\DoxyCodeLine{00639\ \ \ \ \ \textcolor{keywordtype}{void}\ *val;}
\DoxyCodeLine{00640\ \ \ \ \ jsgf\_t\ *imp;}
\DoxyCodeLine{00641\ \ \ \ \ \textcolor{keywordtype}{int}\ import\_all;}
\DoxyCodeLine{00642\ }
\DoxyCodeLine{00643\ \ \ \ \ \textcolor{comment}{/*\ Trim\ the\ leading\ and\ trailing\ <>\ */}}
\DoxyCodeLine{00644\ \ \ \ \ namelen\ =\ strlen(name);}
\DoxyCodeLine{00645\ \ \ \ \ path\ =\ ckd\_malloc(namelen\ -\/\ 2\ +\ 6);\ \textcolor{comment}{/*\ room\ for\ a\ trailing\ .gram\ */}}
\DoxyCodeLine{00646\ \ \ \ \ strcpy(path,\ name\ +\ 1);}
\DoxyCodeLine{00647\ \ \ \ \ \textcolor{comment}{/*\ Split\ off\ the\ first\ part\ of\ the\ name\ */}}
\DoxyCodeLine{00648\ \ \ \ \ c\ =\ strrchr(path,\ \textcolor{charliteral}{'.'});}
\DoxyCodeLine{00649\ \ \ \ \ \textcolor{keywordflow}{if}\ (c\ ==\ NULL)\ \{}
\DoxyCodeLine{00650\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Imported\ rule\ is\ not\ qualified:\ \%s\(\backslash\)n"{}},\ name);}
\DoxyCodeLine{00651\ \ \ \ \ \ \ \ \ ckd\_free(path);}
\DoxyCodeLine{00652\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00653\ \ \ \ \ \}}
\DoxyCodeLine{00654\ \ \ \ \ packlen\ =\ c\ -\/\ path;}
\DoxyCodeLine{00655\ \ \ \ \ *c\ =\ \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{00656\ }
\DoxyCodeLine{00657\ \ \ \ \ \textcolor{comment}{/*\ Look\ for\ import\ foo.*\ */}}
\DoxyCodeLine{00658\ \ \ \ \ import\_all\ =\ (strlen(name)\ >\ 2\ \&\&\ 0\ ==\ strcmp(name\ +\ namelen\ -\/\ 3,\ \textcolor{stringliteral}{"{}.*>"{}}));}
\DoxyCodeLine{00659\ }
\DoxyCodeLine{00660\ \ \ \ \ \textcolor{comment}{/*\ Construct\ a\ filename.\ */}}
\DoxyCodeLine{00661\ \ \ \ \ \textcolor{keywordflow}{for}\ (c\ =\ path;\ *c;\ ++c)}
\DoxyCodeLine{00662\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (*c\ ==\ \textcolor{charliteral}{'.'})\ *c\ =\ \textcolor{charliteral}{'/'};}
\DoxyCodeLine{00663\ \ \ \ \ strcat(path,\ \textcolor{stringliteral}{"{}.gram"{}});}
\DoxyCodeLine{00664\ \ \ \ \ newpath\ =\ path\_list\_search(jsgf-\/>searchpath,\ path);}
\DoxyCodeLine{00665\ \ \ \ \ ckd\_free(path);}
\DoxyCodeLine{00666\ \ \ \ \ \textcolor{keywordflow}{if}\ (newpath\ ==\ NULL)}
\DoxyCodeLine{00667\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00668\ }
\DoxyCodeLine{00669\ \ \ \ \ path\ =\ newpath;}
\DoxyCodeLine{00670\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Importing\ \%s\ from\ \%s\ to\ \%s\(\backslash\)n"{}},\ name,\ path,\ jsgf-\/>name);}
\DoxyCodeLine{00671\ }
\DoxyCodeLine{00672\ \ \ \ \ \textcolor{comment}{/*\ FIXME:\ Also,\ we\ need\ to\ make\ sure\ that\ path\ is\ fully\ qualified}}
\DoxyCodeLine{00673\ \textcolor{comment}{\ \ \ \ \ *\ here,\ by\ adding\ any\ prefixes\ from\ jsgf-\/>name\ to\ it.\ */}}
\DoxyCodeLine{00674\ \ \ \ \ \textcolor{comment}{/*\ See\ if\ we\ have\ parsed\ it\ already\ */}}
\DoxyCodeLine{00675\ \ \ \ \ \textcolor{keywordflow}{if}\ (hash\_table\_lookup(jsgf-\/>imports,\ path,\ \&val)\ ==\ 0)\ \{}
\DoxyCodeLine{00676\ \ \ \ \ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Already\ imported\ \%s\(\backslash\)n"{}},\ path);}
\DoxyCodeLine{00677\ \ \ \ \ \ \ \ \ imp\ =\ val;}
\DoxyCodeLine{00678\ \ \ \ \ \ \ \ \ ckd\_free(path);}
\DoxyCodeLine{00679\ \ \ \ \ \}}
\DoxyCodeLine{00680\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00681\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ If\ not,\ parse\ it.\ */}}
\DoxyCodeLine{00682\ \ \ \ \ \ \ \ \ imp\ =\ jsgf\_parse\_file(path,\ jsgf);}
\DoxyCodeLine{00683\ \ \ \ \ \ \ \ \ val\ =\ hash\_table\_enter(jsgf-\/>imports,\ path,\ imp);}
\DoxyCodeLine{00684\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (val\ !=\ (\textcolor{keywordtype}{void}\ *)imp)\ \{}
\DoxyCodeLine{00685\ \ \ \ \ \ \ \ \ \ \ \ \ E\_WARN(\textcolor{stringliteral}{"{}Multiply\ imported\ file:\ \%s\(\backslash\)n"{}},\ path);}
\DoxyCodeLine{00686\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00687\ \ \ \ \ \}}
\DoxyCodeLine{00688\ \ \ \ \ \textcolor{keywordflow}{if}\ (imp\ !=\ NULL)\ \{}
\DoxyCodeLine{00689\ \ \ \ \ \ \ \ \ hash\_iter\_t\ *itor;}
\DoxyCodeLine{00690\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Look\ for\ public\ rules\ matching\ rulename.\ */}}
\DoxyCodeLine{00691\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (itor\ =\ hash\_table\_iter(imp-\/>rules);\ itor;}
\DoxyCodeLine{00692\ \ \ \ \ \ \ \ \ \ \ \ \ \ itor\ =\ hash\_table\_iter\_next(itor))\ \{}
\DoxyCodeLine{00693\ \ \ \ \ \ \ \ \ \ \ \ \ hash\_entry\_t\ *he\ =\ itor-\/>ent;}
\DoxyCodeLine{00694\ \ \ \ \ \ \ \ \ \ \ \ \ jsgf\_rule\_t\ *rule\ =\ hash\_entry\_val(he);}
\DoxyCodeLine{00695\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ rule\_matches;}
\DoxyCodeLine{00696\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ *rule\_name\ =\ importname2rulename(name);}
\DoxyCodeLine{00697\ }
\DoxyCodeLine{00698\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (import\_all)\ \{}
\DoxyCodeLine{00699\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Match\ package\ name\ (symbol\ table\ is\ shared)\ */}}
\DoxyCodeLine{00700\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ rule\_matches\ =\ !strncmp(rule\_name,\ rule-\/>name,\ packlen\ +\ 1);}
\DoxyCodeLine{00701\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00702\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00703\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Exact\ match\ */}}
\DoxyCodeLine{00704\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ rule\_matches\ =\ !strcmp(rule\_name,\ rule-\/>name);}
\DoxyCodeLine{00705\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00706\ \ \ \ \ \ \ \ \ \ \ \ \ ckd\_free(rule\_name);}
\DoxyCodeLine{00707\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (rule-\/>public\ \&\&\ rule\_matches)\ \{}
\DoxyCodeLine{00708\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{void}\ *val;}
\DoxyCodeLine{00709\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ *newname;}
\DoxyCodeLine{00710\ }
\DoxyCodeLine{00711\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Link\ this\ rule\ into\ the\ current\ namespace.\ */}}
\DoxyCodeLine{00712\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ c\ =\ strrchr(rule-\/>name,\ \textcolor{charliteral}{'.'});}
\DoxyCodeLine{00713\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ assert(c\ !=\ NULL);}
\DoxyCodeLine{00714\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ newname\ =\ jsgf\_fullname(jsgf,\ c);}
\DoxyCodeLine{00715\ }
\DoxyCodeLine{00716\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Imported\ \%s\(\backslash\)n"{}},\ newname);}
\DoxyCodeLine{00717\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ val\ =\ hash\_table\_enter(jsgf-\/>rules,\ newname,}
\DoxyCodeLine{00718\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ jsgf\_rule\_retain(rule));}
\DoxyCodeLine{00719\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (val\ !=\ (\textcolor{keywordtype}{void}\ *)rule)\ \{}
\DoxyCodeLine{00720\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_WARN(\textcolor{stringliteral}{"{}Multiply\ defined\ symbol:\ \%s\(\backslash\)n"{}},\ newname);}
\DoxyCodeLine{00721\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00722\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (!import\_all)\ \{}
\DoxyCodeLine{00723\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ hash\_table\_iter\_free(itor);}
\DoxyCodeLine{00724\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ rule;}
\DoxyCodeLine{00725\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00726\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00727\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00728\ \ \ \ \ \}}
\DoxyCodeLine{00729\ }
\DoxyCodeLine{00730\ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00731\ \}}
\DoxyCodeLine{00732\ }
\DoxyCodeLine{00733\ jsgf\_t\ *}
\DoxyCodeLine{00734\ jsgf\_parse\_file(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *filename,\ jsgf\_t\ *parent)}
\DoxyCodeLine{00735\ \{}
\DoxyCodeLine{00736\ \ \ \ \ yyscan\_t\ yyscanner;}
\DoxyCodeLine{00737\ \ \ \ \ jsgf\_t\ *jsgf;}
\DoxyCodeLine{00738\ \ \ \ \ \textcolor{keywordtype}{int}\ yyrv;}
\DoxyCodeLine{00739\ \ \ \ \ FILE\ *in\ =\ NULL;}
\DoxyCodeLine{00740\ }
\DoxyCodeLine{00741\ \ \ \ \ yylex\_init(\&yyscanner);}
\DoxyCodeLine{00742\ \ \ \ \ \textcolor{keywordflow}{if}\ (filename\ ==\ NULL)\ \{}
\DoxyCodeLine{00743\ \ \ \ \ \ \ \ \ yyset\_in(stdin,\ yyscanner);}
\DoxyCodeLine{00744\ \ \ \ \ \}}
\DoxyCodeLine{00745\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00746\ \ \ \ \ \ \ \ \ in\ =\ fopen(filename,\ \textcolor{stringliteral}{"{}r"{}});}
\DoxyCodeLine{00747\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (in\ ==\ NULL)\ \{}
\DoxyCodeLine{00748\ \ \ \ \ \ \ \ \ \ \ \ \ E\_ERROR\_SYSTEM(\textcolor{stringliteral}{"{}Failed\ to\ open\ \%s\ for\ parsing"{}},\ filename);}
\DoxyCodeLine{00749\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00750\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00751\ \ \ \ \ \ \ \ \ yyset\_in(in,\ yyscanner);}
\DoxyCodeLine{00752\ \ \ \ \ \}}
\DoxyCodeLine{00753\ }
\DoxyCodeLine{00754\ \ \ \ \ jsgf\ =\ jsgf\_grammar\_new(parent);}
\DoxyCodeLine{00755\ \ \ \ \ yyrv\ =\ yyparse(yyscanner,\ jsgf);}
\DoxyCodeLine{00756\ \ \ \ \ \textcolor{keywordflow}{if}\ (yyrv\ !=\ 0)\ \{}
\DoxyCodeLine{00757\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ parse\ JSGF\ grammar\ from\ '\%s'\(\backslash\)n"{}},\ filename\ ?\ filename\ :\ \textcolor{stringliteral}{"{}(stdin)"{}});}
\DoxyCodeLine{00758\ \ \ \ \ \ \ \ \ jsgf\_grammar\_free(jsgf);}
\DoxyCodeLine{00759\ \ \ \ \ \ \ \ \ yylex\_destroy(yyscanner);}
\DoxyCodeLine{00760\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00761\ \ \ \ \ \}}
\DoxyCodeLine{00762\ \ \ \ \ \textcolor{keywordflow}{if}\ (in)}
\DoxyCodeLine{00763\ \ \ \ \ \ \ \ \ fclose(in);}
\DoxyCodeLine{00764\ \ \ \ \ yylex\_destroy(yyscanner);}
\DoxyCodeLine{00765\ }
\DoxyCodeLine{00766\ \ \ \ \ \textcolor{keywordflow}{return}\ jsgf;}
\DoxyCodeLine{00767\ \}}

\end{DoxyCode}
