BNF and ABNF expand understanding of the bacos paradigm

Source: Internet
Author: User
Tags format definition repetition

BNF

The BNF (abbreviated as Backus-Naur Form) paradigm is a set of symbols first introduced by John Backus and Peter Naur to describe computer language syntax. Now, almost every author of a new programming language book uses the bacos paradigm to define the syntax rules of the programming language.
In BNF, the double quotation marks ("word") represent the characters themselves. Double_quote represents double quotation marks.
Words outside double quotes (which may contain underscores) represent the syntax section.
<>: Mandatory.
[]: The included items are optional.
{}: Contains items that can be repeated 0 to countless times.
|: Indicates either of the left and right sides, which is equivalent to "or.
::=: It means "defined"
"...": Glossary
[...]: Option: it can appear at most once.
{...}: Repeated items: any number of times, including 0
(...): Group
|: Parallel option. You can select only one
Italics: parameters, which are explained elsewhere

The following is an example of the for Statement defined by BNF in Java:
For_statement: =
"For" "(" (variable_declaration |
(Expression ";") | ";")
[Expression] ";"
[Expression] ";"
")" Statement

ABNF

Rfc2234 defines the extended bacos Paradigm (ABNF ). ABNF has been widely used in Internet definition in recent years. ABNF has made more improvements. ABNF is based on BNF, But it consists of its own syntax and derivation rules. The initiation principle of this metalanguage is to describe the formal system as a communication protocol (two-way Specification) language. It is created in RFC 4234 and is generally used as the definition language of the IETF communication protocol.

ABNF is a set of deduction rules, written

Rule = definition; Comment CR LF

The rule here is a case-sensitive non-Terminator. It defines a sequence of symbols defining the rule, which is composed of a document comment and ends with a carriage return line break.

The rule name is case-insensitive:<Rulename>,<Rulename>,<Rulename>And<Rulename>The same rule is mentioned. The rule name consists of letters, numbers, and hyphens starting with a letter. Angle brackets ("<",">") (Such as BNF) Name of the surrounding rule. However, they can be used to define rule names, for example, when identifying rule names in redundant texts. ABNF uses 7-bit ASCII encoding, and the high position is zero in the 8-bit domain.

The Terminator is specified by one or more numeric characters. The numeric character can be specified as a percent sign with the base number (B = binary, D = decimal, x = hexadecimal)"%", Followed by the numerical value, or a series of numerical values (using".). For example, if you press enter, you can specify it as decimal.? 3Or hexadecimal% X0d. Enter a line feed? 3.10.

Text text is enclosed in quotation marks (". These strings are case-insensitive and use the US-ASCII character set. Therefore, the string "ABC" matches "ABC", and "ABC ". For case-sensitive matching, explicit characters must be defined: To match "ABC", the definition will be? 7? 6? 9.

 

Operator

A blank space is used to separate the elements of a definition. To recognize a space as a separator, it must be explicitly included.

 

Series

Rule 1 Rule 2

Rules can be defined by listing a sequence of Rule names.

You can use the following rules to match the string "ABA:

    1. Fu = % x61;
    2. Bar = % x62; B
    3. Mumble = Fu bar Fu

Select

Rule 1/Rule 2

You can use a backslash ("/.

To accept rules <Fu> or rules <bar>, You can construct the following rules:

    1. Fubar = Fu/Bar

 

Incremental Selection

Rule 1 =/Rule 2

You can use the"=/To add a supplementary choice to a rule.

Rules

    1. Ruleset = alt1/alt2/alt3/alt4/alt5

Equivalent

    1. Ruleset = alt1/alt2
    2. Ruleset =/alt3
    3. Ruleset =/alt4/alt5

 

Value Range

% C ##-##

You can use a hyphen ("-.

Rules

    1. Octal = "0"/"1"/"2"/"3"/"4"/"5"/"6"/"7"

Equivalent

    1. Octal = % x30-37

Sequential grouping

(Rule 1 rule 2)

Elements can be placed in parentheses to combine the rules in the definition.

To match "ELEM fubar snfu" or "ELEM tarfu snfu", you can construct the following rules:

    1. Group = ELEM (fubar/tarfu) snfu

To match "elem fubar" or "tarfu snfu", you can construct the following rules:

    1. Group = ELEM fubar/tarfu snfu
    2. Group = (ELEM fubar)/(tarfu snfu)

 

Variable repetition

N * n rules

It indicates the repeat form of an element.<A> * <B> element. Optional<A>The minimum number of elements to be included. The default value is 0. Optional<B>The maximum number of elements to be included. The default value is infinite.

Use zero or multiple elements* ElementTo use one or more elements.1 * Element, Used for two or three elements2*3 elements.

 

Specific repetition

N rules

Indicates the usable form of a specific number of elements<A> element, Which is equivalent<A> * <A> element.

Use2 digitGet two numbers, use3 digitGet three numbers. (Digit is defined in the following core rules ).

 

Optional Sequence

[Rules]

It indicates that the following structures of the optional elements are equivalent:

    1. [Fubar snfu]
    2. * 1 (fubar snfu)
    3. 0*1 (fubar snfu)

 

Note

; Comment

Semicolon (";) Starts a comment and continues until the end of this line.

 

Operator priority

The preceding operators have a given priority from binding to loose binding:

    1. String, Formation)
    2. Note
    3. Value Range
    4. Repeated
    5. Group. Optional.
    6. Series
    7. Select

The use of the selection operator with concatenation can cause confusion. We recommend that you use grouping for explicit concatenation grouping.

 

Core Rules

Core rules are defined in the ABNF standard.

Rules Format definition Meaning
Alpha % X41-5A/% x61-7A Uppercase and lowercase ASCII letters (A-Z A-Z)
Digit % X30-39 Number (0-9)
Hexdig Digit/"A"/"B"/"C"/"D"/"E"/"F" Hexadecimal number (0-9 A-F a-f)
Dquote % X22 Double quotation marks
SP % X20 Space
Htab % X09 Horizontal Tab
WSP SP/htab Space and horizontal Tab
Lwsp * (WSP/crlf wsp) Linear blank (later than line feed)
Vchar % X21-7E Visible (print) characters
Char % X01-7F Any 7-bit US-ASCII character, not including NUL
Octet % X00-FF 8-Bit Data
CTL % X00-1F/% x7f Control characters
Cr % X0d Enter
Lf % X0a Line feed
CRLF CR LF Standard Internet line feed
Bit "0"/"1"  

 

Example

The example of the postal address in the BNF entry can be specified:

postal-address = name-part street zip-part name-part = *(personal-part SP) last-name [SP suffix] CRLF name-part =/ personal-part CRLF personal-part = first-name / (initial ".") first-name = *ALPHA initial = ALPHA last-name = *ALPHA suffix = ("Jr." / "Sr." / 1*("I" / "V" / "X")) street = [apt SP] house-num SP street-name CRLF apt = 1*4DIGIT house-num = 1*8(DIGIT / ALPHA) street-name = 1*VCHAR zip-part = town-name "," SP state 1*2SP zip-code CRLF town-name = 1*(ALPHA / SP) state = 2ALPHA zip-code = 5DIGIT ["-" 4DIGIT]

BNF and ABNF expand understanding of the bacos paradigm

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.