基于Predictive Parsing的ABNF语法分析器(八)——AbnfParser文法解析器之带星号的情形(如char

编程入门 行业动态 更新时间:2024-10-11 05:21:41

基于Predictive Parsing的ABNF语法分析器(八)——AbnfParser文法解析器之带<a href=https://www.elefans.com/category/jswz/34/1709131.html style=星号的情形(如char"/>

基于Predictive Parsing的ABNF语法分析器(八)——AbnfParser文法解析器之带星号的情形(如char

带星号*表示重复的次数,例如*A表示A可以重复0至任意多次,*3A表示A可以重复0次、1次、2次或3次,4*A表示A至少要重复4次。我们先来看最简单的情形*A,ABNF的语法定义中char-val和prose-val都属于这种。

/*This file is one of the component a Context-free Grammar Parser Generator,which accept a piece of text as the input, and generates a parserfor the inputted context-free grammar.Copyright (C) 2013, Junbiao Pan (Email: panjunbiao@gmail)This program is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, orany later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program.  If not, see </>.*///		        char-val       =  DQUOTE *(%x20-21 / %x23-7E) DQUOTE
//  DQUOTE         =  %x22protected CharVal char_val() throws IOException, MatchException {String char_val = "";
//      char-val是双引号开始的assertMatch(is.peek(), 0x22);
//      把这个双引号消化掉 :)is.read();
//      双引号后面跟着的0x20-21、0x23-7E都属于合法的字符,读入之while (match(is.peek(), 0x20, 0x21) || match(is.peek(), 0x23, 0x7E)) {char_val += (char)is.read();}
//      如果不是跟着0x20-21、0x23-7E,则必须是双引号,否则异常assertMatch(is.peek(), 0x22);is.read();
//      返回这个字符串return new CharVal(char_val);}//		        char-val       =  DQUOTE *(%x20-21 / %x23-7E) DQUOTE
//  DQUOTE         =  %x22@Testpublic void testChar_val() throws Exception {Tester<CharVal> tester = new Tester<CharVal>() {@Overridepublic CharVal test(AbnfParser parser) throws MatchException, IOException {return parser.char_val();}};String input;input = "";
//      用来虐待char_var()方法的各种case,自己看吧Assert.assertEquals(input, AbnfParserFactory.newInstance("\"" + input + "\"").char_val().toString());input = String.valueOf((char)0x20);Assert.assertEquals(input, AbnfParserFactory.newInstance("\"" + input + "\"").char_val().toString());input = String.valueOf((char)0x21);Assert.assertEquals(input, AbnfParserFactory.newInstance("\"" + input + "\"").char_val().toString());input = String.valueOf((char)0x23);Assert.assertEquals(input, AbnfParserFactory.newInstance("\"" + input + "\"").char_val().toString());input = String.valueOf((char)0x7E);Assert.assertEquals(input, AbnfParserFactory.newInstance("\"" + input + "\"").char_val().toString());input = String.valueOf((char)0x20) + String.valueOf((char)0x20);Assert.assertEquals(input, AbnfParserFactory.newInstance("\"" + input + "\"").char_val().toString());input = String.valueOf((char)0x21) + String.valueOf((char)0x21);Assert.assertEquals(input, AbnfParserFactory.newInstance("\"" + input + "\"").char_val().toString());input = String.valueOf((char)0x23) + String.valueOf((char)0x23);Assert.assertEquals(input, AbnfParserFactory.newInstance("\"" + input + "\"").char_val().toString());input = String.valueOf((char)0x7E) + String.valueOf((char)0x7E);Assert.assertEquals(input, AbnfParserFactory.newInstance("\"" + input + "\"").char_val().toString());input = "AbCd1234#$%^~!@#$%^&*()`-=_+[]\\{}|,./<>?;:'";Assert.assertEquals(input, AbnfParserFactory.newInstance("\"" + input + "\"").char_val().toString());Assert.assertEquals(input, AbnfParserFactory.newInstance("\"" + input + "\"A").char_val().toString());Assert.assertEquals(input, AbnfParserFactory.newInstance("\"" + input + "\"\"").char_val().toString());//      Assertion.assertMatchException("", tester, 1, 1);Assertion.assertMatchException("" + (char)0x19, tester, 1, 1);
//      这个case可以发现我们这个解析器的缺陷,
//      例如当一个输入流是只有一个双引号开头,没有双引号结尾的情况下,
//      char_var()方法会抛出异常(这一点是可以接受的),
//      但读指针却无法到双引号之前了(也就是说这个算法不支持回溯)Assertion.assertMatchException("\"", tester, 2, 1);Assertion.assertMatchException("\"a", tester, 3, 1);Assertion.assertMatchException("B", tester, 1, 1);}//		        prose-val      =  "<" *(%x20-3D / %x3F-7E) ">"
//      prose_val()方法和char_val()方法很类似,请自行阅读protected ProseVal prose_val() throws IOException, MatchException {String proseval = "";assertMatch(is.peek(), '<');is.read();while (match(is.peek(), 0x20, 0x3D) || match(is.peek(), 0x3F, 0x7E)) {proseval += (char)is.read();}assertMatch(is.peek(), '>');is.read();return new ProseVal(proseval);}//		        prose-val      =  "<" *(%x20-3D / %x3F-7E) ">"@Testpublic void testProse_val() throws Exception {Tester<String> tester = new Tester<String>() {@Overridepublic String test(AbnfParser parser) throws MatchException, IOException {return parser.prose_val().toString();}};String input;input = String.valueOf(new char[] {'<', '>'});Assert.assertEquals(input, AbnfParserFactory.newInstance(input).prose_val().toString());input = String.valueOf(new char[] {'<', 0x20, '>'});Assert.assertEquals(input, AbnfParserFactory.newInstance(input).prose_val().toString());input = String.valueOf(new char[] {'<', 0x3D, '>'});Assert.assertEquals(input, AbnfParserFactory.newInstance(input).prose_val().toString());input = String.valueOf(new char[] {'<', 0x3F, '>'});Assert.assertEquals(input, AbnfParserFactory.newInstance(input).prose_val().toString());input = String.valueOf(new char[] {'<', 0x7E, '>'});Assert.assertEquals(input, AbnfParserFactory.newInstance(input).prose_val().toString());input = String.valueOf(new char[] {'<', 0x20, 0x3D, 0x3F, 0x7E, '>'});Assert.assertEquals(input, AbnfParserFactory.newInstance(input).prose_val().toString());Assert.assertEquals("<>", AbnfParserFactory.newInstance("<>>").prose_val().toString());Assertion.assertMatchException("<" + (char) 0x19 + ">", tester, 2, 1);Assertion.assertMatchException("<" + (char) 0x7F + ">", tester, 2, 1);Assertion.assertMatchException("<" + (char)0x20 + (char)0x19 + ">", tester, 3, 1);}



本系列文章索引:基于预测的ABNF文法分析器

更多推荐

基于Predictive Parsing的ABNF语法分析器(八)——AbnfParser文法解析器之带星号的情形(如char

本文发布于:2024-02-27 15:32:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1707139.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:星号   分析器   文法   语法   情形

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!