\section{src/libsphinxbase/lm/jsgf.c File Reference}
\label{jsgf_8c}\index{src/libsphinxbase/lm/jsgf.\+c@{src/libsphinxbase/lm/jsgf.\+c}}


This file implements the data structures for parsing J\+S\+GF grammars into Sphinx finite-\/state grammars.  


{\ttfamily \#include $<$string.\+h$>$}\newline
{\ttfamily \#include $<$assert.\+h$>$}\newline
{\ttfamily \#include \char`\"{}sphinxbase/ckd\+\_\+alloc.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}sphinxbase/strfuncs.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}sphinxbase/hash\+\_\+table.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}sphinxbase/err.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}jsgf\+\_\+internal.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}jsgf\+\_\+parser.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}jsgf\+\_\+scanner.\+h\char`\"{}}\newline
\subsection*{Functions}
\begin{DoxyCompactItemize}
\item 
\mbox{\label{jsgf_8c_a0e59b63784fa8761358bb2bdf568bec1}} 
\textbf{ jsgf\+\_\+atom\+\_\+t} $\ast$ {\bfseries jsgf\+\_\+atom\+\_\+new} (char $\ast$name, float weight)
\item 
\mbox{\label{jsgf_8c_a289d683bf62d1a5aaac6917fa64f54f2}} 
int {\bfseries jsgf\+\_\+atom\+\_\+free} (\textbf{ jsgf\+\_\+atom\+\_\+t} $\ast$atom)
\item 
\textbf{ jsgf\+\_\+t} $\ast$ \textbf{ jsgf\+\_\+grammar\+\_\+new} (\textbf{ jsgf\+\_\+t} $\ast$parent)
\begin{DoxyCompactList}\small\item\em Create a new J\+S\+GF grammar. \end{DoxyCompactList}\item 
\mbox{\label{jsgf_8c_a0a5185af0b0f1c19d85ae553c5ecc51b}} 
void \textbf{ jsgf\+\_\+grammar\+\_\+free} (\textbf{ jsgf\+\_\+t} $\ast$jsgf)
\begin{DoxyCompactList}\small\item\em Free a J\+S\+GF grammar. \end{DoxyCompactList}\item 
\mbox{\label{jsgf_8c_ab9dc2ab39224cda76c43822150cf7e67}} 
\textbf{ jsgf\+\_\+atom\+\_\+t} $\ast$ {\bfseries jsgf\+\_\+kleene\+\_\+new} (\textbf{ jsgf\+\_\+t} $\ast$jsgf, \textbf{ jsgf\+\_\+atom\+\_\+t} $\ast$atom, int plus)
\item 
\mbox{\label{jsgf_8c_a4a07cb0721add8783f0ea24dfacd6c0f}} 
\textbf{ jsgf\+\_\+rule\+\_\+t} $\ast$ {\bfseries jsgf\+\_\+optional\+\_\+new} (\textbf{ jsgf\+\_\+t} $\ast$jsgf, \textbf{ jsgf\+\_\+rhs\+\_\+t} $\ast$exp)
\item 
\mbox{\label{jsgf_8c_acb5af4a04abc56a8584eba9b937f8154}} 
void {\bfseries jsgf\+\_\+add\+\_\+link} (\textbf{ jsgf\+\_\+t} $\ast$grammar, \textbf{ jsgf\+\_\+atom\+\_\+t} $\ast$atom, int from, int to)
\item 
\mbox{\label{jsgf_8c_a5c84e0a3cebe022d4f7ab8b204c8da43}} 
char const  $\ast$ \textbf{ jsgf\+\_\+grammar\+\_\+name} (\textbf{ jsgf\+\_\+t} $\ast$jsgf)
\begin{DoxyCompactList}\small\item\em Get the grammar name from the file. \end{DoxyCompactList}\item 
\mbox{\label{jsgf_8c_afd426d7e3799f54b4d5c0acff7684233}} 
\textbf{ jsgf\+\_\+rule\+\_\+iter\+\_\+t} $\ast$ \textbf{ jsgf\+\_\+rule\+\_\+iter} (\textbf{ jsgf\+\_\+t} $\ast$grammar)
\begin{DoxyCompactList}\small\item\em Get an iterator over all rules in a grammar. \end{DoxyCompactList}\item 
\mbox{\label{jsgf_8c_a18fc91b6b6e79fb183147502df372b5e}} 
\textbf{ jsgf\+\_\+rule\+\_\+t} $\ast$ \textbf{ jsgf\+\_\+get\+\_\+rule} (\textbf{ jsgf\+\_\+t} $\ast$grammar, char const $\ast$name)
\begin{DoxyCompactList}\small\item\em Get a rule by name from a grammar. \end{DoxyCompactList}\item 
\mbox{\label{jsgf_8c_a94a20b28b8bb3a179e567caec972d002}} 
char const  $\ast$ \textbf{ jsgf\+\_\+rule\+\_\+name} (\textbf{ jsgf\+\_\+rule\+\_\+t} $\ast$rule)
\begin{DoxyCompactList}\small\item\em Get the rule name from a rule. \end{DoxyCompactList}\item 
\mbox{\label{jsgf_8c_a49d5d9f6c1edda3bb087545ecfa21809}} 
int \textbf{ jsgf\+\_\+rule\+\_\+public} (\textbf{ jsgf\+\_\+rule\+\_\+t} $\ast$rule)
\begin{DoxyCompactList}\small\item\em Test if a rule is public or not. \end{DoxyCompactList}\item 
\mbox{\label{jsgf_8c_a2a3f3ade56bed13c58059a6ede5fd1dc}} 
\textbf{ fsg\+\_\+model\+\_\+t} $\ast$ \textbf{ jsgf\+\_\+build\+\_\+fsg} (\textbf{ jsgf\+\_\+t} $\ast$grammar, \textbf{ jsgf\+\_\+rule\+\_\+t} $\ast$rule, \textbf{ logmath\+\_\+t} $\ast$lmath, float32 lw)
\begin{DoxyCompactList}\small\item\em Build a Sphinx F\+SG object from a J\+S\+GF rule. \end{DoxyCompactList}\item 
\textbf{ fsg\+\_\+model\+\_\+t} $\ast$ \textbf{ jsgf\+\_\+build\+\_\+fsg\+\_\+raw} (\textbf{ jsgf\+\_\+t} $\ast$grammar, \textbf{ jsgf\+\_\+rule\+\_\+t} $\ast$rule, \textbf{ logmath\+\_\+t} $\ast$lmath, float32 lw)
\begin{DoxyCompactList}\small\item\em Build a Sphinx F\+SG object from a J\+S\+GF rule. \end{DoxyCompactList}\item 
\textbf{ fsg\+\_\+model\+\_\+t} $\ast$ \textbf{ jsgf\+\_\+read\+\_\+file} (const char $\ast$file, \textbf{ logmath\+\_\+t} $\ast$lmath, float32 lw)
\begin{DoxyCompactList}\small\item\em Read J\+S\+GF from file and return F\+SG object from it. \end{DoxyCompactList}\item 
int \textbf{ jsgf\+\_\+write\+\_\+fsg} (\textbf{ jsgf\+\_\+t} $\ast$grammar, \textbf{ jsgf\+\_\+rule\+\_\+t} $\ast$rule, F\+I\+LE $\ast$outfh)
\begin{DoxyCompactList}\small\item\em Convert a J\+S\+GF rule to Sphinx F\+SG text form. \end{DoxyCompactList}\item 
\mbox{\label{jsgf_8c_a91539f6bf30f85af4b90b5a85598f53f}} 
\textbf{ jsgf\+\_\+rule\+\_\+t} $\ast$ {\bfseries jsgf\+\_\+define\+\_\+rule} (\textbf{ jsgf\+\_\+t} $\ast$jsgf, char $\ast$name, \textbf{ jsgf\+\_\+rhs\+\_\+t} $\ast$rhs, int public)
\item 
\mbox{\label{jsgf_8c_ad152a23eb0f0d9af8417919fc93074fd}} 
\textbf{ jsgf\+\_\+rule\+\_\+t} $\ast$ {\bfseries jsgf\+\_\+rule\+\_\+retain} (\textbf{ jsgf\+\_\+rule\+\_\+t} $\ast$rule)
\item 
\mbox{\label{jsgf_8c_af2faf6fb74ad1e4d43cf990c1bcec672}} 
int {\bfseries jsgf\+\_\+rule\+\_\+free} (\textbf{ jsgf\+\_\+rule\+\_\+t} $\ast$rule)
\item 
\mbox{\label{jsgf_8c_afe1e47584e4dd80929552ffed77373f2}} 
\textbf{ jsgf\+\_\+rule\+\_\+t} $\ast$ {\bfseries jsgf\+\_\+import\+\_\+rule} (\textbf{ jsgf\+\_\+t} $\ast$jsgf, char $\ast$name)
\item 
\textbf{ jsgf\+\_\+t} $\ast$ \textbf{ jsgf\+\_\+parse\+\_\+file} (const char $\ast$filename, \textbf{ jsgf\+\_\+t} $\ast$parent)
\begin{DoxyCompactList}\small\item\em Parse a J\+S\+GF grammar from a file. \end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{Detailed Description}
This file implements the data structures for parsing J\+S\+GF grammars into Sphinx finite-\/state grammars. 



\subsection{Function Documentation}
\mbox{\label{jsgf_8c_a68e2c50712a3d1da5d9d6cb25a7198ee}} 
\index{jsgf.\+c@{jsgf.\+c}!jsgf\+\_\+build\+\_\+fsg\+\_\+raw@{jsgf\+\_\+build\+\_\+fsg\+\_\+raw}}
\index{jsgf\+\_\+build\+\_\+fsg\+\_\+raw@{jsgf\+\_\+build\+\_\+fsg\+\_\+raw}!jsgf.\+c@{jsgf.\+c}}
\subsubsection{jsgf\+\_\+build\+\_\+fsg\+\_\+raw()}
{\footnotesize\ttfamily \textbf{ fsg\+\_\+model\+\_\+t}$\ast$ jsgf\+\_\+build\+\_\+fsg\+\_\+raw (\begin{DoxyParamCaption}\item[{\textbf{ jsgf\+\_\+t} $\ast$}]{grammar,  }\item[{\textbf{ jsgf\+\_\+rule\+\_\+t} $\ast$}]{rule,  }\item[{\textbf{ logmath\+\_\+t} $\ast$}]{lmath,  }\item[{float32}]{lw }\end{DoxyParamCaption})}



Build a Sphinx F\+SG object from a J\+S\+GF rule. 

This differs from \doxyref{jsgf\+\_\+build\+\_\+fsg()}{p.}{jsgf_8h_ac355893a25aa071eba6874fae1d51ad2} in that it does not do closure on epsilon transitions or any other postprocessing. For the time being this is necessary in order to write it to a file -\/ the F\+SG code will be fixed soon. 

Definition at line 504 of file jsgf.\+c.



Referenced by jsgf\+\_\+write\+\_\+fsg().

\mbox{\label{jsgf_8c_a2f4a4ef97090210de6c73361de1d9561}} 
\index{jsgf.\+c@{jsgf.\+c}!jsgf\+\_\+grammar\+\_\+new@{jsgf\+\_\+grammar\+\_\+new}}
\index{jsgf\+\_\+grammar\+\_\+new@{jsgf\+\_\+grammar\+\_\+new}!jsgf.\+c@{jsgf.\+c}}
\subsubsection{jsgf\+\_\+grammar\+\_\+new()}
{\footnotesize\ttfamily \textbf{ jsgf\+\_\+t}$\ast$ jsgf\+\_\+grammar\+\_\+new (\begin{DoxyParamCaption}\item[{\textbf{ jsgf\+\_\+t} $\ast$}]{parent }\end{DoxyParamCaption})}



Create a new J\+S\+GF grammar. 


\begin{DoxyParams}{Parameters}
{\em parent} & optional parent grammar for this one (N\+U\+LL, usually). \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
new J\+S\+GF grammar object, or N\+U\+LL on failure. 
\end{DoxyReturn}


Definition at line 79 of file jsgf.\+c.



References ckd\+\_\+calloc, hash\+\_\+table\+\_\+new(), jsgf\+\_\+s\+::imports, jsgf\+\_\+s\+::parent, jsgf\+\_\+s\+::rules, and jsgf\+\_\+s\+::searchpath.

\mbox{\label{jsgf_8c_a322da18b1c115f1b3a1b7c766cf9e778}} 
\index{jsgf.\+c@{jsgf.\+c}!jsgf\+\_\+parse\+\_\+file@{jsgf\+\_\+parse\+\_\+file}}
\index{jsgf\+\_\+parse\+\_\+file@{jsgf\+\_\+parse\+\_\+file}!jsgf.\+c@{jsgf.\+c}}
\subsubsection{jsgf\+\_\+parse\+\_\+file()}
{\footnotesize\ttfamily \textbf{ jsgf\+\_\+t}$\ast$ jsgf\+\_\+parse\+\_\+file (\begin{DoxyParamCaption}\item[{const char $\ast$}]{filename,  }\item[{\textbf{ jsgf\+\_\+t} $\ast$}]{parent }\end{DoxyParamCaption})}



Parse a J\+S\+GF grammar from a file. 


\begin{DoxyParams}{Parameters}
{\em filename} & the name of the file to parse. \\
\hline
{\em parent} & optional parent grammar for this one (N\+U\+LL, usually). \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
new J\+S\+GF grammar object, or N\+U\+LL on failure. 
\end{DoxyReturn}


Definition at line 734 of file jsgf.\+c.



Referenced by jsgf\+\_\+read\+\_\+file().

\mbox{\label{jsgf_8c_adfb0b4d697fcd8760d051c38689c85e2}} 
\index{jsgf.\+c@{jsgf.\+c}!jsgf\+\_\+read\+\_\+file@{jsgf\+\_\+read\+\_\+file}}
\index{jsgf\+\_\+read\+\_\+file@{jsgf\+\_\+read\+\_\+file}!jsgf.\+c@{jsgf.\+c}}
\subsubsection{jsgf\+\_\+read\+\_\+file()}
{\footnotesize\ttfamily \textbf{ fsg\+\_\+model\+\_\+t}$\ast$ jsgf\+\_\+read\+\_\+file (\begin{DoxyParamCaption}\item[{const char $\ast$}]{file,  }\item[{\textbf{ logmath\+\_\+t} $\ast$}]{lmath,  }\item[{float32}]{lw }\end{DoxyParamCaption})}



Read J\+S\+GF from file and return F\+SG object from it. 

This function looks for a first public rule in jsgf and constructs J\+S\+GF from it. 

Definition at line 511 of file jsgf.\+c.



References jsgf\+\_\+parse\+\_\+file().

\mbox{\label{jsgf_8c_ab9454daf3cdf0c857b0f4780b600632c}} 
\index{jsgf.\+c@{jsgf.\+c}!jsgf\+\_\+write\+\_\+fsg@{jsgf\+\_\+write\+\_\+fsg}}
\index{jsgf\+\_\+write\+\_\+fsg@{jsgf\+\_\+write\+\_\+fsg}!jsgf.\+c@{jsgf.\+c}}
\subsubsection{jsgf\+\_\+write\+\_\+fsg()}
{\footnotesize\ttfamily int jsgf\+\_\+write\+\_\+fsg (\begin{DoxyParamCaption}\item[{\textbf{ jsgf\+\_\+t} $\ast$}]{grammar,  }\item[{\textbf{ jsgf\+\_\+rule\+\_\+t} $\ast$}]{rule,  }\item[{F\+I\+LE $\ast$}]{outfh }\end{DoxyParamCaption})}



Convert a J\+S\+GF rule to Sphinx F\+SG text form. 

This does a direct conversion without doing transitive closure on null transitions and so forth. 

Definition at line 542 of file jsgf.\+c.



References jsgf\+\_\+build\+\_\+fsg\+\_\+raw(), and logmath\+\_\+init().

