As mentioned above, a generator (Parser Generator) that can recognize ABNF grammar and automatically construct ABNF grammar parser must first be able to recognize ABNF grammar, that is, after reading ABNF into the memory and structuring it, to generate a parser. The module I read into the ABNF grammar is called the abnfparser class. Next, let's take a look at the basic structure of this class:
/* 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 parser for the inputted context-free grammar. copyright (c) 2013, junbiao Pan (Email: panjunbiao@gmail.com) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the license, or (at your option) any later version. this program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a participant purpose. see the GNU General Public License for more details. you shoshould have your ed a copy of the GNU General Public License along with this p Rogram. if not, see
In this way, when we need to parse the input ABNF syntax, we only need to call it like this:
Abnfparser = new abnfparser (prefix, system. In); List <rule> rulelist = abnfparser. parse ();
The peekableinputstream class is copied from the Internet. If you are interested, click peekable inputstream. On this basis, I added a location-related function, this parameter is used to indicate the location where an error occurs when a matching error occurs. Other content is not moved. Next let's take a look at the peekableinputstream class definition.
/* 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 parser for the inputted context-free grammar. copyright (c) 2013, junbiao Pan (Email: panjunbiao@gmail.com) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the license, or (at your option) any later version. this program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a participant purpose. see the GNU General Public License for more details. you shoshould have your ed a copy of the GNU General Public License along with this p Rogram. if not, see
Next let's take a look at the exceptions that may be thrown during the two parsing syntaxes.
Matchexception matching exception:
Package Org. sip4x. ABNF;/* 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 parser for the inputted context-free grammar. copyright (c) 2013, junbiao Pan (Email: panjunbiao@gmail.com) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the license, or (at your option) any later version. this program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a participant purpose. see the GNU General Public License for more details. you shoshould have your ed a copy of the GNU General Public License along with this program. if not, see
Nothing special. Let's look at the conflict exceptions:
/* 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 parser for the inputted context-free grammar. copyright (c) 2013, junbiao Pan (Email: panjunbiao@gmail.com) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the license, or (at your option) any later version. this program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a participant purpose. see the GNU General Public License for more details. you shoshould have your ed a copy of the GNU General Public License along with this program. if not, see
A conflict exception is used to find two rules with the same name in the input stream, and an exception is thrown when it is not a progressive definition. That is, the rule cannot be renamed, unless "=/" is used to add definitions based on existing rules.
At this point, the basic architecture of our ABNF syntax analyzer has come out. Next we will insert some unit test content, and then begin to write the specific ABNF Syntax Parsing.Code.