9.25 Summary and combing grammar and language

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

 This chapter is mainly about the type and basic grammar definition language, we learn grammar by chapter intuitive concept, in the form of symbols and strings, grammar and language definitions, grammar, context-free grammar and syntax tree, sentence analysis these and other content. It focuses on the definition of formal grammar, context-free grammars, regular grammar, derivation, phrases, parse tree, ambiguity.

1,  alphabet and symbol train

alphabet: Ⅰ . Nonempty finite set of symbols, for example:. AabbccⅡ Typical symbols are letters, numbers, punctuation and various other operators.

symbol string: Ⅰ . Definition Ⅱ on a finite symbol alphabet sequence of the alphabet by the Ⅲ symbols synonym: sentences, words. Including the connection string, set operations, exponentiation symbol string, a set of exponentiation A + positive closure with closure A *.

2,   grammar

Definitions: grammar is the definition and description of the structure of language. I.e., from the form and a predetermined language for describing the structure is referred to as "grammar" (or "grammar")

   Any grammar can be represented as a four-tuple = G (V N , V T , P, S), where V N is non-terminal symbols, V T  termination nonempty finite set of symbols, V N ∩V T = [Phi] , P is of formula (rules) set, S, or an identifier called the start symbol.

       

For example, an integer generated grammar:

G(N):N->D|ND

D->0|1|2|3…|9

Can be written as V T = V {0,1,2 ...} N = {N, D} P = {S = N-N> D, N-> ND, D-> 0, D->. 1, ...}

3, language

Formal definition: determining a set of alphabet on a particular symbol string.

4, is derived: including the leftmost derivation and rightmost derivation.

Leftmost derivation : if a => β, and in each "step derivation", the leftmost α are replaced nonterminal, such derived as claimed leftmost derivation.

Rightmost derivation : if a => β, and in each "step derivation", the rightmost α are replaced nonterminal, such derived as claimed rightmost derivation. Also known as canonical derivation

E.g:

 

5, phrases, and phrases directly handle:

The phrase: For grammar = G (V T , V N , S, J), assume that a sentence is grammar G abd, if there is: S = *> αAδ and A = +> β, abd sentence is called beta] About nonterminal a phrase; corresponding sub-tree concept in the syntax tree.

Direct phrase: if there is: S = *> αAδ and A => β, is called beta] abd sentence phrases directly on the nonterminal A. The corresponding sub-tree simple syntax tree. Each phrase is a direct right part of a rule.

Handle: a sentence leftmost direct phrases, simple sub-tree corresponding to the most left one.

E.g:

 

6,  ambiguity: grammatical ambiguity and ambiguous language

Is defined : If a grammar corresponding to a sentence exists between two different syntax tree or comprise two or more of the rightmost (leftmost) derivation (statute), the grammar is ambiguous, can use grammar equivalence to eliminate ambiguity.

Solution : ① If two language grammar to generate the same, 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, such a language called ambiguous language.

Ambiguity problem is undecidable

The absence of an algorithm that can be determined in a limited step exactly whether a grammar is ambiguous.

You can find some sufficient conditions (not necessarily a prerequisite), grammar when these conditions are met, you can be sure that the grammar is unambiguous.

7,  grammar classification

①0 type grammar / unrestricted grammar: α-> β, where α∈ (Vn∪Vt) * and containing at least one non-terminator, β∈ (Vn∪Vt) *.

②1 type grammar / context-sensitive grammar: αAβ-> αuβ, wherein A∈Vn, α, β∈ (Vn∪Vt) *, u∈ (Vn∪Vt) +.

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

④3 type 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 *.

This chapter focuses on understanding the syntax, semantics, grammar, generates a syntax tree, ambiguous sentence / grammar / language, learn how to solve ① given grammar, language production; ② seeking phrases, direct phrases and handles; ③ definition of grammar, description language.

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

Integer n- : <integer> :: = [-] <number> {<number>}

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

Identifier of the I : <Identifier i> :: = <letter> {<letters> | <number>}

<Letters> :: = a | b | ... | X | Y | Z

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

Expression E : <Expression e> :: = [+ | - ] < item> {<subtraction operator> <term>}

      <Addition operator> :: = + | -

      <Item> :: = <factor> {<multiplication operator> <factor>}

<Factor> :: = <identifier> | <unsigned integer> | '(' <expression> ')'

<Multiplication operator> :: = * | /

<Unsigned integer> :: = <number> {<number>}

<Identifier i> :: = <letter> {<letters> | <number>}

<Letters> :: = a | b | ... | X | Y | Z

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

Conditional statements: <conditional statement> :: = if <condition> the then <statement>

<Condition> :: = <expression> <relational operator> <expression> | the ODD <expression>

<Statement> :: = <assignment statement> | <conditional statements> | <When the loop type> | <procedure call statement> | <statement read> | <Write statement> | <compound statement> | <empty statement>

<Relational operators> :: = = | # | <| <= |> |> =

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

<Conditional statement> :: = if <condition> the then <statement>

<When the loop type> :: = WHILE <condition> the DO <statement>

<Procedure call statement> :: = CALL <identifier>

<Read statement> :: = READ '(' <identifier> {, <identifier>} '')

<Write statement> :: = WRITE '(' <expression> {, <expression>} '')

<Compound statement> :: = begin <statement> {; <statement>} end

Assignment statement: <assignment statement> :: = <identifier id>: = <expression>

<Identifier id> :: = <letter> {<letters> | <number>}

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

Compound statement: <compound statement> :: = begin <statement> {; <statement>} end

      <Statement> :: = <assignment statement> | <conditional statements> | <When the loop type> | <procedure call statement> | <statement read> | <Write statement> | <compound statement> | <empty statement>

Function: <function> :: = <Description Type> <function name> <compound statement>

      <Compound statement> :: = begin <statement> {; <statement>} end

<Statement> :: = <assignment statement> | <conditional statements> | <When the loop type> | <procedure call statement> | <statement read> | <Write statement> | <compound statement> | <empty statement>

Program: <program> :: = <Block>

<Block> :: = [<Constant Description section>] [<Variable Description section>] [<Process Description section>] <statement>

<Constant Description part> :: = CONST <constant definitions> {, <constant definitions>};

<Constant defines> :: = <identifier> = <unsigned integer>

<Unsigned integer> :: = <number> {<number>}

<Variable Description part> :: = VAR <identifier> {, <identifier>};

<Identifier> :: = <letter> {<letters> | <number>}

<Process Description section> :: = <process header> <Block> {; <Process Description section>};

<Process header> :: = PROCEDURE <identifier>;

Guess you like

Origin www.cnblogs.com/Azan1999/p/11593141.html