Cup 和 Jflex 中的 JSON 验证语法



我为数组编写了以下cup文件:

    terminal COMMA, LSQBRACKET, RSQBRACKET;
    terminal Integer NUMBER;
    non terminal array, value_list, value;
    non terminal Integer expr;
    array ::= LSQBRACKET value_list RSQBRACKET;
    value_list ::= value_list COMMA value | value;
    value::= expr:e {: System.out.println(" value: "+e+""); :} ;
    expr ::= NUMBER:n
    {: RESULT=n; :}

如何为 Jflex 和 Cup 编写 Json 验证的整个语法?

来自简单的 json 库

package org.json.simple.parser;
%%
%{
private StringBuffer sb=new StringBuffer();
int getPosition(){
    return yychar;
}
%}
%table
%unicode
%state STRING_BEGIN
%yylexthrow ParseException
%char
HEX_D = [a-fA-F0-9]
INT = [-]?[0-9]+
DOUBLE = {INT}((.[0-9]+)?([eE][-+]?[0-9]+)?)
WS = [ trn]
UNESCAPED_CH = [^"\]
FALLBACK_CH = .
%%
<STRING_BEGIN> "               { yybegin(YYINITIAL);return new Yytoken(Yytoken.TYPE_VALUE, sb.toString());}
<STRING_BEGIN> {UNESCAPED_CH}+  { sb.append(yytext());}
<STRING_BEGIN> \"             {sb.append('"');}
<STRING_BEGIN> \\             {sb.append('\');}
<STRING_BEGIN> \/             {sb.append('/');}
<STRING_BEGIN> \b              {sb.append('b');}
<STRING_BEGIN> \f              {sb.append('f');}
<STRING_BEGIN> \n              {sb.append('n');}
<STRING_BEGIN> \r              {sb.append('r');}
<STRING_BEGIN> \t              {sb.append('t');}
<STRING_BEGIN> \u{HEX_D}{HEX_D}{HEX_D}{HEX_D}  {   try{
                                                        int ch=Integer.parseInt(yytext().substring(2),16);
                                                        sb.append((char)ch);
                                                    }
                                                    catch(Exception e){
                                                        throw new ParseException(yychar, ParseException.ERROR_UNEXPECTED_EXCEPTION, e);
                                                    }
                                                }
<STRING_BEGIN> \               {sb.append('\');}
<YYINITIAL> "                  { sb = null; sb = new StringBuffer(); yybegin(STRING_BEGIN);}
<YYINITIAL> {INT}               { Long val=Long.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE, val);}
<YYINITIAL> {DOUBLE}            { Double val=Double.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE, val);}
<YYINITIAL> "true"|"false"      { Boolean val=Boolean.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE, val);}
<YYINITIAL> "null"              { return new Yytoken(Yytoken.TYPE_VALUE, null);}
<YYINITIAL> "{"                 { return new Yytoken(Yytoken.TYPE_LEFT_BRACE,null);}
<YYINITIAL> "}"                 { return new Yytoken(Yytoken.TYPE_RIGHT_BRACE,null);}
<YYINITIAL> "["                 { return new Yytoken(Yytoken.TYPE_LEFT_SQUARE,null);}
<YYINITIAL> "]"                 { return new Yytoken(Yytoken.TYPE_RIGHT_SQUARE,null);}
<YYINITIAL> ","                 { return new Yytoken(Yytoken.TYPE_COMMA,null);}
<YYINITIAL> ":"                 { return new Yytoken(Yytoken.TYPE_COLON,null);}
<YYINITIAL> {WS}+               {}
<YYINITIAL> {FALLBACK_CH}       { throw new ParseException(yychar, ParseException.ERROR_UNEXPECTED_CHAR, new Character(yycharat(0)));}

相关内容

  • 没有找到相关文章

最新更新