jsgf.c File Reference

This file implements the data structures for parsing JSGF grammars into Sphinx finite-state grammars. More...

`#include <string.h>`
`#include <assert.h>`
`#include "sphinxbase/ckd_alloc.h"`
`#include "sphinxbase/strfuncs.h"`
`#include "sphinxbase/hash_table.h"`
`#include "sphinxbase/filename.h"`
`#include "sphinxbase/err.h"`
`#include "sphinxbase/jsgf.h"`
`#include "jsgf_internal.h"`
`#include "jsgf_parser.h"`
`#include "jsgf_scanner.h"`

Go to the source code of this file.

## Macros

#define | NO_NODE -1
#define | RECURSIVE_NODE -2

## Functions

int | yyparse (void *scanner, jsgf_t *jsgf)
jsgf_atom_t * | jsgf_atom_new (char *name, float weight)
int | jsgf_atom_free (jsgf_atom_t *atom)
jsgf_t * | jsgf_grammar_new (jsgf_t *parent)
| Create a new JSGF grammar.
void | jsgf_grammar_free (jsgf_t *jsgf)
| Free a JSGF grammar.
jsgf_atom_t * | jsgf_kleene_new (jsgf_t *jsgf, jsgf_atom_t *atom, int plus)
jsgf_rule_t * | jsgf_optional_new (jsgf_t *jsgf, jsgf_rhs_t *exp)
void | jsgf_add_link (jsgf_t *grammar, jsgf_atom_t *atom, int from, int to)
char const * | jsgf_grammar_name (jsgf_t *jsgf)
| Get the grammar name from the file.
jsgf_rule_iter_t * | jsgf_rule_iter (jsgf_t *grammar)
| Get an iterator over all rules in a grammar.
jsgf_rule_t * | jsgf_get_rule (jsgf_t *grammar, char const *name)
| Get a rule by name from a grammar.
jsgf_rule_t * | jsgf_get_public_rule (jsgf_t *grammar)
| Returns the first public rule of the grammar.
char const * | jsgf_rule_name (jsgf_rule_t *rule)
| Get the rule name from a rule.
int | jsgf_rule_public (jsgf_rule_t *rule)
| Test if a rule is public or not.
fsg_model_t * | jsgf_build_fsg (jsgf_t *grammar, jsgf_rule_t *rule, logmath_t *lmath, float32 lw)
| Build a Sphinx FSG object from a JSGF rule.
fsg_model_t * | jsgf_build_fsg_raw (jsgf_t *grammar, jsgf_rule_t *rule, logmath_t *lmath, float32 lw)
| Build a Sphinx FSG object from a JSGF rule.
fsg_model_t * | jsgf_read_file (const char *file, logmath_t *lmath, float32 lw)
| Read JSGF from file and return FSG object from it.
fsg_model_t * | jsgf_read_string (const char *string, logmath_t *lmath, float32 lw)
| Read JSGF from string and return FSG object from it.
int | jsgf_write_fsg (jsgf_t *grammar, jsgf_rule_t *rule, FILE *outfh)
| Convert a JSGF rule to Sphinx FSG text form.
jsgf_rule_t * | jsgf_define_rule (jsgf_t *jsgf, char *name, jsgf_rhs_t *rhs, int is_public)
jsgf_rule_t * | jsgf_rule_retain (jsgf_rule_t *rule)
int | jsgf_rule_free (jsgf_rule_t *rule)
jsgf_rule_t * | jsgf_import_rule (jsgf_t *jsgf, char *name)
jsgf_t * | jsgf_parse_file (const char *filename, jsgf_t *parent)
| Parse a JSGF grammar from a file.
jsgf_t * | jsgf_parse_string (const char *string, jsgf_t *parent)
| Parse a JSGF grammar from a string.

## Detailed Description

This file implements the data structures for parsing JSGF grammars into Sphinx finite-state grammars.

Definition in file jsgf.c.

## Macro Definition Documentation

## NO_NODE

#define NO_NODE -1

Definition at line 283 of file jsgf.c.

## RECURSIVE_NODE

#define RECURSIVE_NODE -2

Definition at line 284 of file jsgf.c.

## Function Documentation

## jsgf_add_link()

void jsgf_add_link | ( | jsgf_t * | grammar,
|  | jsgf_atom_t * | atom,
|  | int | from,
|  | int | to )

Definition at line 191 of file jsgf.c.

## jsgf_atom_free()

int jsgf_atom_free | ( | jsgf_atom_t * | atom | ) |

Definition at line 76 of file jsgf.c.

## jsgf_atom_new()

jsgf_atom_t * jsgf_atom_new | ( | char * | name,
|  | float | weight )

Definition at line 65 of file jsgf.c.

## jsgf_build_fsg()

fsg_model_t * jsgf_build_fsg | ( | jsgf_t * | grammar,
|  | jsgf_rule_t * | rule,
|  | logmath_t * | lmath,
|  | float32 | lw )

Build a Sphinx FSG object from a JSGF rule.

Definition at line 585 of file jsgf.c.

Referenced by jsgf_read_file(), and jsgf_read_string().

## jsgf_build_fsg_raw()

fsg_model_t * jsgf_build_fsg_raw | ( | jsgf_t * | grammar,
|  | jsgf_rule_t * | rule,
|  | logmath_t * | lmath,
|  | float32 | lw )

Build a Sphinx FSG object from a JSGF rule.

This differs from jsgf_build_fsg() 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 FSG code will be fixed soon.

Definition at line 592 of file jsgf.c.

Referenced by jsgf_write_fsg().

## jsgf_define_rule()

jsgf_rule_t * jsgf_define_rule | ( | jsgf_t * | jsgf,
|  | char * | name,
|  | jsgf_rhs_t * | rhs,
|  | int | is_public )

Definition at line 681 of file jsgf.c.

## jsgf_get_public_rule()

jsgf_rule_t * jsgf_get_public_rule | ( | jsgf_t * | grammar | ) |

Returns the first public rule of the grammar.

Definition at line 484 of file jsgf.c.

References jsgf_grammar_name(), jsgf_rule_iter(), jsgf_rule_iter_free, jsgf_rule_iter_next, jsgf_rule_iter_rule, jsgf_rule_name(), and jsgf_rule_public().

## jsgf_get_rule()

jsgf_rule_t * jsgf_get_rule | ( | jsgf_t * | grammar,
|  | const char * | name )

Get a rule by name from a grammar.

Name should not contain brackets.

Definition at line 469 of file jsgf.c.

References ckd_free(), hash_table_lookup(), jsgf_s::rules, and string_join().

## jsgf_grammar_free()

void jsgf_grammar_free | ( | jsgf_t * | jsgf | ) |

Free a JSGF grammar.

Definition at line 108 of file jsgf.c.

References jsgf_s::charset, ckd_free(), hash_iter_s::ent, glist_free(), gnode_ptr, hash_table_free(), hash_table_iter(), hash_table_iter_next(), jsgf_s::imports, jsgf_grammar_free(), jsgf_s::links, jsgf_s::locale, jsgf_s::name, jsgf_s::parent, jsgf_s::rules, jsgf_s::searchpath, hash_entry_s::val, and jsgf_s::version.

Referenced by jsgf_grammar_free(), jsgf_parse_file(), jsgf_parse_string(), jsgf_read_file(), and jsgf_read_string().

## jsgf_grammar_name()

char const * jsgf_grammar_name | ( | jsgf_t * | jsgf | ) |

Get the grammar name from the file.

Definition at line 216 of file jsgf.c.

References jsgf_s::name.

Referenced by jsgf_get_public_rule().

## jsgf_grammar_new()

jsgf_t * jsgf_grammar_new | ( | jsgf_t * | parent | ) |

Create a new JSGF grammar.

Parameters

  parent | optional parent grammar for this one (NULL, usually).

Returns
  new JSGF grammar object, or NULL on failure.

Definition at line 86 of file jsgf.c.

References ckd_calloc, hash_table_new(), jsgf_s::imports, jsgf_s::parent, jsgf_s::rules, and jsgf_s::searchpath.

Referenced by jsgf_parse_file(), and jsgf_parse_string().

## jsgf_import_rule()

jsgf_rule_t * jsgf_import_rule | ( | jsgf_t * | jsgf,
|  | char * | name )

Definition at line 759 of file jsgf.c.

## jsgf_kleene_new()

jsgf_atom_t * jsgf_kleene_new | ( | jsgf_t * | jsgf,
|  | jsgf_atom_t * | atom,
|  | int | plus )

Definition at line 157 of file jsgf.c.

## jsgf_optional_new()

jsgf_rule_t * jsgf_optional_new | ( | jsgf_t * | jsgf,
|  | jsgf_rhs_t * | exp )

Definition at line 181 of file jsgf.c.

## jsgf_parse_file()

jsgf_t * jsgf_parse_file | ( | const char * | filename,
|  | jsgf_t * | parent )

Parse a JSGF grammar from a file.

Parameters

  filename | the name of the file to parse.
  parent | optional parent grammar for this one (NULL, usually).

Returns
  new JSGF grammar object, or NULL on failure.

Definition at line 896 of file jsgf.c.

References E_ERROR, E_ERROR_SYSTEM, jsgf_grammar_free(), and jsgf_grammar_new().

Referenced by jsgf_read_file().

## jsgf_parse_string()

jsgf_t * jsgf_parse_string | ( | const char * | string,
|  | jsgf_t * | parent )

Parse a JSGF grammar from a string.

Parameters

  0-terminated | string with grammar.
  parent | optional parent grammar for this one (NULL, usually).

Returns
  new JSGF grammar object, or NULL on failure.

Definition at line 937 of file jsgf.c.

References E_ERROR, jsgf_grammar_free(), and jsgf_grammar_new().

Referenced by jsgf_read_string().

## jsgf_read_file()

fsg_model_t * jsgf_read_file | ( | const char * | file,
|  | logmath_t * | lmath,
|  | float32 | lw )

Read JSGF from file and return FSG object from it.

This function looks for a first public rule in jsgf and constructs JSGF from it.

Definition at line 599 of file jsgf.c.

References E_ERROR, jsgf_build_fsg(), jsgf_grammar_free(), jsgf_parse_file(), jsgf_rule_iter(), jsgf_rule_iter_free, jsgf_rule_iter_next, jsgf_rule_iter_rule, and jsgf_rule_public().

## jsgf_read_string()

fsg_model_t * jsgf_read_string | ( | const char * | string,
|  | logmath_t * | lmath,
|  | float32 | lw )

Read JSGF from string and return FSG object from it.

This function looks for a first public rule in jsgf and constructs JSGF from it.

Definition at line 630 of file jsgf.c.

References E_ERROR, jsgf_build_fsg(), jsgf_grammar_free(), jsgf_parse_string(), jsgf_rule_iter(), jsgf_rule_iter_free, jsgf_rule_iter_next, jsgf_rule_iter_rule, and jsgf_rule_public().

## jsgf_rule_free()

int jsgf_rule_free | ( | jsgf_rule_t * | rule | ) |

Definition at line 722 of file jsgf.c.

## jsgf_rule_iter()

jsgf_rule_iter_t * jsgf_rule_iter | ( | jsgf_t * | grammar | ) |

Get an iterator over all rules in a grammar.

Definition at line 463 of file jsgf.c.

References hash_table_iter(), and jsgf_s::rules.

Referenced by jsgf_get_public_rule(), jsgf_read_file(), and jsgf_read_string().

## jsgf_rule_name()

char const * jsgf_rule_name | ( | jsgf_rule_t * | rule | ) |

Get the rule name from a rule.

Definition at line 513 of file jsgf.c.

References jsgf_rule_s::name.

Referenced by jsgf_get_public_rule().

## jsgf_rule_public()

int jsgf_rule_public | ( | jsgf_rule_t * | rule | ) |

Test if a rule is public or not.

Definition at line 519 of file jsgf.c.

References jsgf_rule_s::is_public.

Referenced by jsgf_get_public_rule(), jsgf_read_file(), and jsgf_read_string().

## jsgf_rule_retain()

jsgf_rule_t * jsgf_rule_retain | ( | jsgf_rule_t * | rule | ) |

Definition at line 715 of file jsgf.c.

## jsgf_write_fsg()

int jsgf_write_fsg | ( | jsgf_t * | grammar,
|  | jsgf_rule_t * | rule,
|  | FILE * | outfh )

Convert a JSGF rule to Sphinx FSG text form.

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

Definition at line 663 of file jsgf.c.

References jsgf_build_fsg_raw(), logmath_free(), and logmath_init().