1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* ==================================================================== 3 * Copyright (c) 2007 Carnegie Mellon University. All rights 4 * reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 18 * This work was supported in part by funding from the Defense Advanced 19 * Research Projects Agency and the National Science Foundation of the 20 * United States of America, and the CMU Sphinx Speech Consortium. 21 * 22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * ==================================================================== 35 * 36 */ 37 38 #ifndef __JSGF_H__ 39 #define __JSGF_H__ 40 41 /** 42 * @file jsgf.h JSGF grammar compiler 43 * 44 * This file defines the data structures for parsing JSGF grammars 45 * into Sphinx finite-state grammars. 46 **/ 47 48 #include <stdio.h> 49 50 /* Win32/WinCE DLL gunk */ 51 #include <sphinxbase/sphinxbase_export.h> 52 #include <sphinxbase/hash_table.h> 53 #include <sphinxbase/fsg_model.h> 54 #include <sphinxbase/logmath.h> 55 56 #ifdef __cplusplus 57 extern "C" { 58 #endif 59 #if 0 60 /* Fool Emacs. */ 61 } 62 #endif 63 64 typedef struct jsgf_s jsgf_t; 65 typedef struct jsgf_rule_s jsgf_rule_t; 66 67 /** 68 * Create a new JSGF grammar. 69 * 70 * @param parent optional parent grammar for this one (NULL, usually). 71 * @return new JSGF grammar object, or NULL on failure. 72 */ 73 SPHINXBASE_EXPORT 74 jsgf_t *jsgf_grammar_new(jsgf_t *parent); 75 76 /** 77 * Parse a JSGF grammar from a file. 78 * 79 * @param filename the name of the file to parse. 80 * @param parent optional parent grammar for this one (NULL, usually). 81 * @return new JSGF grammar object, or NULL on failure. 82 */ 83 SPHINXBASE_EXPORT 84 jsgf_t *jsgf_parse_file(const char *filename, jsgf_t *parent); 85 86 /** 87 * Get the grammar name from the file. 88 */ 89 SPHINXBASE_EXPORT 90 char const *jsgf_grammar_name(jsgf_t *jsgf); 91 92 /** 93 * Free a JSGF grammar. 94 */ 95 SPHINXBASE_EXPORT 96 void jsgf_grammar_free(jsgf_t *jsgf); 97 98 /** 99 * Iterator over rules in a grammar. 100 */ 101 typedef hash_iter_t jsgf_rule_iter_t; 102 103 /** 104 * Get an iterator over all rules in a grammar. 105 */ 106 SPHINXBASE_EXPORT 107 jsgf_rule_iter_t *jsgf_rule_iter(jsgf_t *grammar); 108 109 /** 110 * Advance an iterator to the next rule in the grammar. 111 */ 112 #define jsgf_rule_iter_next(itor) hash_table_iter_next(itor) 113 114 /** 115 * Get the current rule in a rule iterator. 116 */ 117 #define jsgf_rule_iter_rule(itor) ((jsgf_rule_t *)(itor)->ent->val) 118 119 /** 120 * Free a rule iterator (if the end hasn't been reached). 121 */ 122 #define jsgf_rule_iter_free(itor) hash_table_iter_free(itor) 123 124 /** 125 * Get a rule by name from a grammar. 126 */ 127 SPHINXBASE_EXPORT 128 jsgf_rule_t *jsgf_get_rule(jsgf_t *grammar, char const *name); 129 130 /** 131 * Get the rule name from a rule. 132 */ 133 SPHINXBASE_EXPORT 134 char const *jsgf_rule_name(jsgf_rule_t *rule); 135 136 /** 137 * Test if a rule is public or not. 138 */ 139 SPHINXBASE_EXPORT 140 int jsgf_rule_public(jsgf_rule_t *rule); 141 142 /** 143 * Build a Sphinx FSG object from a JSGF rule. 144 */ 145 SPHINXBASE_EXPORT 146 fsg_model_t *jsgf_build_fsg(jsgf_t *grammar, jsgf_rule_t *rule, 147 logmath_t *lmath, float32 lw); 148 149 /** 150 * Build a Sphinx FSG object from a JSGF rule. 151 * 152 * This differs from jsgf_build_fsg() in that it does not do closure 153 * on epsilon transitions or any other postprocessing. For the time 154 * being this is necessary in order to write it to a file - the FSG 155 * code will be fixed soon. 156 */ 157 SPHINXBASE_EXPORT 158 fsg_model_t *jsgf_build_fsg_raw(jsgf_t *grammar, jsgf_rule_t *rule, 159 logmath_t *lmath, float32 lw); 160 161 162 /** 163 * Read JSGF from file and return FSG object from it. 164 * 165 * This function looks for a first public rule in jsgf and constructs JSGF from it. 166 */ 167 SPHINXBASE_EXPORT 168 fsg_model_t *jsgf_read_file(const char *file, logmath_t * lmath, float32 lw); 169 170 /** 171 * Convert a JSGF rule to Sphinx FSG text form. 172 * 173 * This does a direct conversion without doing transitive closure on 174 * null transitions and so forth. 175 */ 176 SPHINXBASE_EXPORT 177 int jsgf_write_fsg(jsgf_t *grammar, jsgf_rule_t *rule, FILE *outfh); 178 179 #ifdef __cplusplus 180 } 181 #endif 182 183 184 #endif /* __JSGF_H__ */ 185