Debian Codegraph @

src/libsphinxbase/lm/jsgf_parser.y

raw
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 #define YYERROR_VERBOSE
39
40 #include <stdio.h>
41 #include <string.h>
42
43 #include <sphinxbase/hash_table.h>
44 #include <sphinxbase/ckd_alloc.h>
45 #include <sphinxbase/err.h>
46
47 #include "jsgf_internal.h"
48 #include "jsgf_parser.h"
49 #include "jsgf_scanner.h"
50
51 /* Suppress warnings from generated code */
52 #if defined _MSC_VER
53 #pragma warning(disable: 4273)
54 #endif
55
56 void yyerror(yyscan_t lex, jsgf_t *jsgf, const char *s);
57
58 %}
59
60 %pure-parser
61 %lex-param { void* yyscanner }
62 %parse-param { void* yyscanner }
63 %parse-param { jsgf_t *jsgf }
64
65 %union {
66 char *name;
67 float weight;
68 jsgf_rule_t *rule;
69 jsgf_rhs_t *rhs;
70 jsgf_atom_t *atom;
71 }
72
73 %token HEADER GRAMMAR IMPORT PUBLIC
74 %token <name> TOKEN RULENAME TAG
75 %token <weight> WEIGHT
76 %type <atom> rule_atom rule_item tagged_rule_item
77 %type <rhs> rule_expansion alternate_list
78 %type <name> grammar_header
79 %type <rule> rule_group rule_optional
80 %%
81
82 grammar: header
83 | header rule_list
84 | header import_header rule_list
85 ;
86
87 header: jsgf_header grammar_header { jsgf->name = $2; }
88 ;
89
90 jsgf_header: HEADER ';'
91 | HEADER TOKEN ';' { jsgf->version = $2; }
92 | HEADER TOKEN TOKEN ';' { jsgf->version = $2; jsgf->charset = $3; }
93 | HEADER TOKEN TOKEN TOKEN ';' { jsgf->version = $2; jsgf->charset = $3;
94 jsgf->locale = $4; }
95 ;
96
97 grammar_header: GRAMMAR TOKEN ';' { $$ = $2; }
98 ;
99
100 import_header: import_statement
101 | import_header import_statement
102 ;
103
104 import_statement: IMPORT RULENAME ';' { jsgf_import_rule(jsgf, $2); ckd_free($2); }
105 ;
106
107 rule_list: rule
108 | rule_list rule
109 ;
110
111 rule: RULENAME '=' alternate_list ';' { jsgf_define_rule(jsgf, $1, $3, 0); ckd_free($1); }
112 | PUBLIC RULENAME '=' alternate_list ';' { jsgf_define_rule(jsgf, $2, $4, 1); ckd_free($2); }
113 ;
114
115 alternate_list: rule_expansion { $$ = $1; $$->atoms = glist_reverse($$->atoms); }
116 | alternate_list '|' rule_expansion { $$ = $3;
117 $$->atoms = glist_reverse($$->atoms);
118 $$->alt = $1; }
119 ;
120
121 rule_expansion: tagged_rule_item { $$ = ckd_calloc(1, sizeof(*$$));
122 $$->atoms = glist_add_ptr($$->atoms, $1); }
123 | rule_expansion tagged_rule_item { $$ = $1;
124 $$->atoms = glist_add_ptr($$->atoms, $2); }
125 ;
126
127 tagged_rule_item: rule_item
128 | tagged_rule_item TAG { $$ = $1;
129 $$->tags = glist_add_ptr($$->tags, $2); }
130 ;
131
132 rule_item: rule_atom
133 | WEIGHT rule_atom { $$ = $2; $$->weight = $1; }
134 ;
135
136 rule_group: '(' alternate_list ')' { $$ = jsgf_define_rule(jsgf, NULL, $2, 0); }
137 ;
138
139 rule_optional: '[' alternate_list ']' { $$ = jsgf_optional_new(jsgf, $2); }
140 ;
141
142 rule_atom: TOKEN { $$ = jsgf_atom_new($1, 1.0); ckd_free($1); }
143 | RULENAME { $$ = jsgf_atom_new($1, 1.0); ckd_free($1); }
144 | rule_group { $$ = jsgf_atom_new($1->name, 1.0); }
145 | rule_optional { $$ = jsgf_atom_new($1->name, 1.0); }
146 | rule_atom '*' { $$ = jsgf_kleene_new(jsgf, $1, 0); }
147 | rule_atom '+' { $$ = jsgf_kleene_new(jsgf, $1, 1); }
148 ;
149
150 %%
151
152 void
153 yyerror(yyscan_t lex, jsgf_t *jsgf, const char *s)
154 {
155 E_ERROR("%s at line %d current token '%s'\n", s, yyget_lineno(lex), yyget_text(lex));
156 }