04- grammar and language summary and comb

1. carding second chapter, understand and write a summary.

Chapter grammar and language

 

2.1 The concept of grammar

Grammar definition : grammar is a definition and description of the structure of language, from formal description language structure and rules, also known as syntax.

2.2 symbols and strings

Alphabet: The alphabet is a nonempty finite set of elements, referred to as a symbol alphabet elements, also referred to as alphabet symbol set. Traditional symbol alphabet letters, numbers, punctuation and various other operators.

String of symbols: the symbol alphabet composed of any symbol string and poor sequence. The symbol string of the alphabet A = {a, b, c } alphabet by the composition comprising: {ab, ac, bc, abc, a, b, c}. If there is a symbol string of m symbols x, is called the length m, it is expressed as | x | = m. Allow null symbol strings, i.e., does not include any symbol string of symbols, represented by [epsilon], a length of 0, i.e. | ε | = 0.

Symbol string operation: ① connected symbol string: εx = xε = x;

       ② set of product: AB = {xy | x∈A, y∈B}; {ε} A = A {ε} = A;

       ③ symbol string exponentiation: x = abc, x ^ 2 = abcabc;

       ④ exponentiation set: x ^ 0 = e; x ^ 1 = x; .....; (Powers and elementary mathematical consistency)

       ⑤ A + positive closure with closure A *: A * = {ε} ∪A +

2.3 formal grammar and language definitions

Grammar : grammar is described in the form of so-called rules of the grammatical structure of the language. Any grammar can be represented as a four-tuple G = (, P, S) . It is a nonempty finite set, each of its elements is called nonterminal; is a nonempty finite set, each of its elements is called terminal symbols; S is a special nonterminal, called grammar start symbol; P rule (α → β) is set.

2.4 grammar type

Type 0 grammar / unrestricted grammar: α-> β, where α∈ (Vn∪Vt) * and containing at least one non-terminator, β∈ (Vn∪Vt) *. Type 1 grammar / context-sensitive grammar: αAβ-> αuβ, wherein A∈Vn, α, β∈ (Vn∪Vt) *, u∈ (Vn∪Vt) +.

Type 2 grammar / context-free grammar: A-> β, where A∈Vn, β∈ (Vn∪Vt) *. Commonly used in syntactic analysis.

Type 3 grammar / regular grammar: lexical analysis commonly used in   

    . I Right Linear Grammar: Only the right push-expanded, A-> αB | α, A, B∈Vn, α∈Vt *.   

    . Ii left linear grammar: Only the left push-out deployment, A-> Bα | α, A, B∈Vn, α∈Vt *.

2.5 context-free grammar and syntax tree

For context-free grammar, the syntax tree is a geometric representation of the derivation of the sentence . When a given symbol string, attempts to follow the rules of a grammar or a symbol string configuration syntax tree for the derivation, it is thus recognized that a sentence grammar; symbol sequence when the termination of all the symbols, it is to identify It is not a grammatical sentence. Therefore, it was also the syntax tree called a parse tree or tree analysis.

2.6 Analysis of sentence

The sentence pattern analysis means to construct an algorithm for determining whether a given symbol string is a sentence grammar (or sentence) . It is divided into two methods: ① top down analysis (rightmost derivation): derived from the start symbol sentences or sentence; ② bottom-up analysis (left-most reduction): reduction from the sentence or sentence start character.

2.6.1 phrases and handles

The phrase: S = *> αAδ and A = +> β, called beta] with respect nonterminal A sentence phrase αAδ; corresponding sub-tree concept in the syntax tree.

Direct phrases: where A => β is derived directly; subtree corresponding to the simple syntax tree. Each phrase is a direct right part of a rule.

Handle: directly the phrase (i.e., the right portion of a rule), and having a leftmost of; simple subtree corresponding to the leftmost one.

Ambiguous grammar: If a sentence corresponding to a grammar exists two different syntax tree, comprising two or more of the rightmost (leftmost) derivation (statute), the grammar is ambiguous may be used; equivalence between grammar to eliminate ambiguity. It does not change any original syntax grammar rules, to increase the intake of non-form definition syntax, such as priority; a configuration equivalent unambiguous grammar.

Ambiguous ambiguous grammar and language: the same as if the two language grammar generated, i.e., L (G) = L (G ), both said to be equivalent grammar. Sometimes an ambiguous grammar can be converted into an equivalent, unambiguous grammar. Some languages, does not exist unambiguous grammar, this language is called the language of ambiguity.

 

2. Try to write grammar PL / 0 language. (Or do you think the better language rules)

Integer n

 <Number> => 0 | 1 | 2 ... 7 | 8 | 9

I identifier

 <Identifier> → <letter> {<letters> | <number>}

Expression e

 <Expression> → [+ | -] <item> {<subtraction operator> <term>}

Conditional statements

 <Conditional statement> → if <condition> the then <statement>

Assignment

<Assignment statement> → <identifier>: = <expression>

Compound statement

<Compound statement> → begin <statement> {; <statement>} end

program

<Program> → <Block>.

<Block> → [<Constant Description section>] [<Variable Description section>] [<Process Description section>] <statement>

Statement

 <Statement> → <assignment> | <conditional statements> | <When the loop type> | <procedure call statement> | <statement read> | <write statements> | <compound statement> | <empty>

Assignment

<Assignment statement> → <identifier>: = <expression>

Compound statement

 <Compound statement> → begin <statement> {; <statement>} end

Statement read 

 <Read statement> → read (<identifier> {, <Identifier>})

Written statements

<Write statement> → write (<expression> {, <expression>})

Guess you like

Origin www.cnblogs.com/HvYan/p/11588191.html