⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsgfparser.java

📁 It is the Speech recognition software. It is platform independent. To execute the source code,
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* Generated By:JavaCC: Do not edit this line. JSGFParser.java */package com.sun.speech.engine.recognition;import java.io.BufferedInputStream;import java.io.ByteArrayInputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;import java.io.StringReader;import java.net.MalformedURLException;import java.net.URL;import java.util.StringTokenizer;import java.util.Vector;import javax.speech.recognition.Grammar;import javax.speech.recognition.GrammarException;import javax.speech.recognition.GrammarSyntaxDetail;import javax.speech.recognition.Recognizer;import javax.speech.recognition.Rule;import javax.speech.recognition.RuleAlternatives;import javax.speech.recognition.RuleCount;import javax.speech.recognition.RuleGrammar;import javax.speech.recognition.RuleName;import javax.speech.recognition.RuleSequence;import javax.speech.recognition.RuleTag;import javax.speech.recognition.RuleToken;public class JSGFParser implements JSGFParserConstants {    // For now we create one global parser, if needed JavaCC can be set    // to allow the creation of multiple parser instances    //    static final String version = "1.0";    static JSGFParser parser = null;    // This main method simplly checks the syntax of a jsgf Grammar    //    public static void main(String args[]) {        if (args.length == 0) {            System.out.println("JSGF Parser Version " + version                               + ":  Reading from standard input . . .");            parser = new JSGFParser(System.in);        } else if (args.length > 0) {            System.out.println("JSGF Parser Version " + version                               +" :  Reading from file " + args[0] + " . . .");            try {               URL codeBase = null;               File f = new File(".");               String path = f.getAbsolutePath() + "/" + args[0];               try {                     codeBase = new URL("file:" + path);               } catch (MalformedURLException e) {                   System.out.println(                    "Could not get URL for current directory " + e);                     return;               }               BufferedInputStream i = new BufferedInputStream(codeBase.openStream(),256);               Object a[] = getJSGFEncoding(i);               Reader rdr;               if ((a != null) && (a[1] != null)) {                 System.out.println("Grammar Character Encoding \""+a[1]+"\"");                 rdr = new InputStreamReader(i,(String)a[1]);               }               else {                 if (a==null) System.out.println("WARNING: Grammar missing self identifying header");                 rdr = new InputStreamReader(i);               }               parser = new JSGFParser(rdr);            } catch (Exception e) {                System.out.println("JSGF Parser Version " + version                                   + ":  File " + args[0] + " not found.");                return;            }        } else {            System.out.println("JSGF Parser Version " + version                               + ":  Usage is one of:");            System.out.println("         java JSGFParser < inputfile");            System.out.println("OR");            System.out.println("         java JSGFParser inputfile");            return;        }        try {            parser.GrammarUnit(null);            System.out.println("JSGF Parser Version " + version                               + ":  JSGF Grammar parsed successfully.");        } catch (ParseException e) {            System.out.println("JSGF Parser Version " + version                               + ":  Encountered errors during parse."                               + e.getMessage());        }    }    /**     * newGrammarFromJSGF - Once JavaCC supports Readers we will change this     */    public static RuleGrammar newGrammarFromJSGF(InputStream i, Recognizer R)        throws GrammarException {        RuleGrammar G = null;        if (parser == null) {            parser = new JSGFParser(i);        } else {            parser.ReInit(i);        }        try {            G = parser.GrammarUnit(R);            return G;        } catch (ParseException e) {            Token etoken = e.currentToken;            GrammarSyntaxDetail gsd = new GrammarSyntaxDetail(                null,                null,                null,                null,                etoken.beginLine,                etoken.beginColumn,                e.getMessage());            GrammarSyntaxDetail gsda[] = new GrammarSyntaxDetail[1];            gsda[0] = gsd;            GrammarException ge = new GrammarException("Grammar Error",gsda);            throw ge;        }    }    /**     * newGrammarFromJSGF - Once JavaCC supports Readers we will change this     */    public static RuleGrammar newGrammarFromJSGF(Reader i, Recognizer R)        throws GrammarException {        RuleGrammar G = null;        if (parser == null) {            parser = new JSGFParser(i);        } else {            parser.ReInit(i);        }        try {            G = parser.GrammarUnit(R);            return G;        } catch (ParseException e) {            Token etoken = e.currentToken;            GrammarSyntaxDetail gsd = new GrammarSyntaxDetail(                null,                null,                null,                null,                etoken.beginLine,                etoken.beginColumn,                e.getMessage());            GrammarSyntaxDetail gsda[] = new GrammarSyntaxDetail[1];            gsda[0] = gsd;            GrammarException ge = new GrammarException("Grammar Error",gsda);            throw ge;        }    }  // returns object array containing the following  // three strings:  //    arry[0] = JSGF Version string  //    arry[1] = ISO Char encoding  //    arry[2] = Locale  //   private static Object[] getJSGFEncoding(BufferedInputStream is) {    int i=0;    byte b[] = new byte[2];    byte c[] = new byte[80];    is.mark(256);    /* read 2 bytes */    try {      if (is.read(b,0,2) != 2) {        is.reset();        return null;      }      // UTF-8      if ((b[0] == 0x23) && (b[1] == 0x4A)) {        i=0;        c[i++]=b[0];        c[i++]=b[1];        while (i<80) {          if (is.read(b,0,1) != 1) { is.reset(); return null; }          if ((b[0] == 0x0A)||(b[0]==0x0D)) break;          c[i++]=b[0];        }      } else    // UTF-16 BE        if ((b[0] == 0x23) && (b[1] == 0x00)) {          i=0;          c[i++]=b[0];          while (i<80) {            if (is.read(b,0,2) != 2) { is.reset(); return null; }            if (b[1] != 0) return null;            if ((b[0] == 0x0A)||(b[0]==0x0D)) break;            c[i++]=b[0];          }        } else {          // UTF-16 LE          if ((b[0] == 0x00) && (b[1] == 0x23)) {            i=0;            c[i++]=b[1];            while (i<80) {              if (is.read(b,0,2) != 2) { is.reset(); return null; }              if (b[0] != 0) return null;              if ((b[1] == 0x0A)||(b[1]==0x0D)) break;              c[i++]=b[1];            }          }        }    } catch (IOException ioe) {      try { is.reset(); } catch (IOException ioe2) { }      return null;    }    if (i==0) {      try { is.reset(); } catch (IOException ioe2) { }      return null;    }    //    // Now c[] should have first line of text in UTF-8 format    //    String estr = new String(c,0,i);    StringTokenizer st = new StringTokenizer(estr," \t\n\r\f;");    String id = null;    String ver = null;    String enc = null;    String loc = null;    if (st.hasMoreTokens()) id = st.nextToken();    if (!id.equals("#JSGF")) {      try { is.reset(); } catch (IOException ioe2) { }      return null;    }    if (st.hasMoreTokens()) ver = st.nextToken();    if (st.hasMoreTokens()) enc = st.nextToken();    if (st.hasMoreTokens()) loc = st.nextToken();    Object rv[] = new Object[3];    rv[0] = ver;    rv[1] = enc;    rv[2] = loc;    return rv;  }    /**     * newGrammarFromURL     */    public static RuleGrammar newGrammarFromJSGF(URL u, Recognizer R)        throws GrammarException, IOException {        BufferedInputStream i = new BufferedInputStream(u.openStream(),256);        Object a[] = getJSGFEncoding(i);        Reader rdr;        if ((a != null) && (a[1] != null)) {                System.out.println("Grammar Character Encoding \""+a[1]+"\"");                rdr = new InputStreamReader(i,(String)a[1]);        }        else {           if (a==null) System.out.println("WARNING: Grammar missing self identifying header");           rdr = new InputStreamReader(i);        }        return newGrammarFromJSGF(rdr,R);    }    /**     * ruleForJSGF     */    public static Rule ruleForJSGF(String text) {        Rule r = null;        try {            StringReader sread = new StringReader(text);            if (parser == null) parser = new JSGFParser(sread);            else parser.ReInit(sread);            r = parser.alternatives();            // System.out.println("JSGF Parser Version " + version            //                    + ":  JSGF RHS parsed successfully.");        } catch (ParseException e) {            System.out.println("JSGF Parser Version " + version                               + ":  Encountered errors during parse.");        }        return r;    }    /**     * Parse an apparent rulename reference.     */    public static RuleName parseRuleName(String text)      throws GrammarException    {       RuleName r = null;       try {         ByteArrayInputStream stream = new ByteArrayInputStream(text.getBytes());         if (parser == null) parser = new JSGFParser(stream);         else parser.ReInit(stream);         r = parser.ruleRef();       } catch (ParseException e) {         throw new GrammarException("JSGF Parser Version " + version                                    + " error", null);       }       return r;    }    /**     * Parse and apparent import declaration     */    public static RuleName parseImport(String text)      throws GrammarException    {      RuleName r = null;      try {        ByteArrayInputStream stream = new ByteArrayInputStream(text.getBytes());        if (parser == null) parser = new JSGFParser(stream);        else parser.ReInit(stream);        r = parser.importRef();      } catch (ParseException e) {        throw new GrammarException("JSGF Parser Version 0.1 error", null);      }      return r;    }    /**     * extract @xxxx keywords from documention comments     */    static void extractKeywords(Grammar G,String rname,String comment) {        if (!(G instanceof BaseRuleGrammar)) {            return;        }        String sample;        BaseRuleGrammar JG = (BaseRuleGrammar) G;        int i = comment.indexOf("@example ");        while (i > 0) {            int j = comment.indexOf('\n',i);            if (j < 0) {                sample = comment.substring(i+8);                i = -1;            } else {                sample = comment.substring(i+8,j);                i = j;            }            i = comment.indexOf("@example ",i);            JG.addSampleSentence(rname,sample);        }    }  final public RuleGrammar GrammarUnit(Recognizer R) throws ParseException {  RuleGrammar G = null;    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {    case IDENTIFIER:      IdentHeader();      break;    default:      jj_la1[0] = jj_gen;      ;    }    G = GrammarDeclaration(R);    label_1:    while (true) {      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {      case IMPORT:        ;        break;      default:        jj_la1[1] = jj_gen;        break label_1;      }      ImportDeclaration(G);    }    label_2:    while (true) {      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {      case PUBLIC:      case 28:        ;        break;      default:        jj_la1[2] = jj_gen;        break label_2;      }      RuleDeclaration(G);    }    jj_consume_token(0);    {if (true) return G;}    throw new Error("Missing return statement in function");  }  final public RuleGrammar GrammarDeclaration(Recognizer R) throws ParseException { String s; RuleGrammar G = null; Token t = null;    t = jj_consume_token(GRAMMAR);    s = Name();    jj_consume_token(26);    if (R != null) try { G=R.newRuleGrammar(s); } catch (IllegalArgumentException ge) {          System.out.println("ERROR " + ge);        };    if (G != null && G instanceof BaseRuleGrammar && t != null &&

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -