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_INTERNAL_H__
39 #define __JSGF_INTERNAL_H__
40 
41 /**
42  * @file jsgf_internal.h Internal definitions for JSGF grammar compiler
43  */
44 
45 #include <stdio.h>
46 
47 #include <sphinxbase/hash_table.h>
48 #include <sphinxbase/glist.h>
49 #include <sphinxbase/fsg_model.h>
50 #include <sphinxbase/logmath.h>
51 #include <sphinxbase/strfuncs.h>
52 #include <sphinxbase/jsgf.h>
53 
54 
55 /* Flex uses strdup which is missing on WinCE */
56 #if defined(_WIN32) || defined(_WIN32_WCE)
57 #define strdup _strdup
58 #endif
59 
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 #if 0
64 /* Fool Emacs. */
65 }
66 #endif
67 
68 #define YY_NO_INPUT /* Silence a compiler warning. */
69 
70 typedef struct jsgf_rhs_s jsgf_rhs_t;
71 typedef struct jsgf_atom_s jsgf_atom_t;
72 typedef struct jsgf_link_s jsgf_link_t;
73 typedef struct jsgf_rule_stack_s jsgf_rule_stack_t;
74 
75 struct jsgf_s {
76     char *version;  /**< JSGF version (from header) */
77     char *charset;  /**< JSGF charset (default UTF-8) */
78     char *locale;   /**< JSGF locale (default C) */
79     char *name;     /**< Grammar name */
80 
81     hash_table_t *rules;   /**< Defined or imported rules in this grammar. */
82     hash_table_t *imports; /**< Pointers to imported grammars. */
83     jsgf_t *parent;        /**< Parent grammar (if this is an imported one) */
84     glist_t searchpath;    /**< List of directories to search for grammars. */
85 
86     /* Scratch variables for FSG conversion. */
87     int nstate;            /**< Number of generated states. */
88     glist_t links;	   /**< Generated FSG links. */
89     glist_t rulestack;     /**< Stack of currently expanded rules. */
90 };
91 
92 /* A type to keep track of the stack of rules currently being expanded. */
93 struct jsgf_rule_stack_s {
94     jsgf_rule_t *rule;  /**< The rule being expanded */
95     int entry;          /**< The entry-state for this expansion */
96 };
97 
98 struct jsgf_rule_s {
99     int refcnt;      /**< Reference count. */
100     char *name;      /**< Rule name (NULL for an alternation/grouping) */
101     int is_public;   /**< Is this rule marked 'public'? */
102     jsgf_rhs_t *rhs; /**< Expansion */
103 };
104 
105 struct jsgf_rhs_s {
106     glist_t atoms;   /**< Sequence of items */
107     jsgf_rhs_t *alt; /**< Linked list of alternates */
108 };
109 
110 struct jsgf_atom_s {
111     char *name;        /**< Rule or token name */
112     glist_t tags;      /**< Tags, if any (glist_t of char *) */
113     float weight;      /**< Weight (default 1) */
114 };
115 
116 struct jsgf_link_s {
117     jsgf_atom_t *atom; /**< Name, tags, weight */
118     int from;          /**< From state */
119     int to;            /**< To state */
120 };
121 
122 #define jsgf_atom_is_rule(atom) ((atom)->name[0] == '<')
123 
124 void jsgf_add_link(jsgf_t *grammar, jsgf_atom_t *atom, int from, int to);
125 jsgf_atom_t *jsgf_atom_new(char *name, float weight);
126 jsgf_atom_t *jsgf_kleene_new(jsgf_t *jsgf, jsgf_atom_t *atom, int plus);
127 jsgf_rule_t *jsgf_optional_new(jsgf_t *jsgf, jsgf_rhs_t *exp);
128 jsgf_rule_t *jsgf_define_rule(jsgf_t *jsgf, char *name, jsgf_rhs_t *rhs, int is_public);
129 jsgf_rule_t *jsgf_import_rule(jsgf_t *jsgf, char *name);
130 
131 int jsgf_atom_free(jsgf_atom_t *atom);
132 int jsgf_rule_free(jsgf_rule_t *rule);
133 jsgf_rule_t *jsgf_rule_retain(jsgf_rule_t *rule);
134 
135 #ifdef __cplusplus
136 }
137 #endif
138 
139 
140 #endif /* __JSGF_H__ */
141