Use examples to differentiate four types of grammar

Source: Internet
Author: User

Compilation principles: This part of the Chinese method is very important. It is also involved in the soft test. At the beginning, this part of the content is quite messy. I can't even understand the definition, so let alone differentiate them. This is a question, analyze the questions, contact the definitions, and discuss them with the students. The relationship between them is shown in the figure below.




1. Grammar

Grammar G is defined as a four-tuple (VN, VT, P, S)


  • Vn: Non-terminator set, usually expressed in uppercase letters
  • Vt: The Terminator set, which is usually expressed by lowercase letters.
  • P: generative set (Rule Set)
  • S: Start symbol (recognition symbol)



Tip: In the following example, uppercase letters indicate non-Terminator, while lowercase letters indicate Terminator.


2.0 grammar (phrase)

Set G = (VN, VT, P, S). If each of its generative α → β is in this structure: α (VN 1_vt) * And contains at least one non-Terminator, while β( VN 1_vt) *, G is a 0-type syntax. Type 0 grammar is the least restrictive of these types of grammar, so we can see at least 0 grammar in the questions.

Type 0 grammar is equivalent to a Turing machine.


3.1 grammar (contextual grammar)

It is based on the 0-type grammar. Each α → β has | β |> = | α |. | β | indicates the length of Beta.

The 1-type syntax is equivalent to a Linear Bounded automatic machine.


Example: If a-> BA exists, | β | = 2, | α | = 1 meets the requirements of type 1 grammar. Otherwise, AA-> A does not conform to the 1-type syntax.

Special case: α → ε also meets the 1 grammar.


4.2 grammar (context-independent grammar)

The type-2 Grammar is based on the type-1 grammar and satisfies that every α → β has α as a non-Terminator. For example, a-> BA meets the requirements of Type 2 Grammar.

The Type 2 syntax is equivalent to the push-down machine.


Example: Although AB-> Bab meets the requirements of type 1 grammar, it does not meet the requirements of Type 2 Grammar because its α = AB, while AB is not a non-Terminator. A-> Bab is right.


5.3 grammar (regular grammar)

It satisfies the following requirements on the basis of Type 2 Syntax: A → α | α B (right linear) or a → α | B α (left linear ).

The Type-3 syntax is equivalent to finite state automation.

Note: Type 3 grammar can only satisfy the right or left linearity.

Example: A-> A, A-> AB, B-> A, B-> CB, which meets the requirements of Type 3 grammar. However, if the derivation is a-> AB, a-> AB, B-> A, B-> CB or derivation

Is: A-> A, A-> BA, B-> A, B-> CB does not meet the requirements of Type 3 method.


I believe you can understand the differences between them through examples.

Use examples to differentiate four types of grammar

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: 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.