ABNF syntax analyzer based on predictive parsing (3) -- basic framework of ABNF syntax parser

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.