Job four - grammar and language summary and comb

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

1, the computer language with English, as has its own unique syntax and rules, certain rules can distinguish legitimate or sentence structure, sentence structure described in these rules, such language is grammar. Using finite set (grammar) to characterize the infinite set (generation language sentences) of a tool.

  As Chinese sentence constituting the subject + = + verb phrase predicate phrase, or other characters

  Programming language sentence + = own symbols symbol string

  Finite alphabet set is not empty, so the alphabet elements are also referred to as symbols, any finite alphabet sequences of symbols called a string, symbol string order is important, to allow an empty symbol string represented by ε. Symbol string operation must pay attention to the order.

2, Rule (rewriting rules, production, formula) is shaped like A → B (A :: = B) of (A, B) ordered pair.

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

  V N : non-terminators (syntax entity, or a variable) set capital letters used;

  V T : as the terminator, small letters used;

  P: is the set of rules, the rules belonging to the left V and comprising at least one non-terminal symbol;

  S: or an identifier for the start symbol, a nonterminal left portion appears at least as a rule.

  Derivation is to derive forward, reverse reduction is deduced.

3, grammar type:

Grammar divided into four types, type 0, type 1, type 2, type 3.

Grammar type

Also known as

Structure or definition

 

example

0 type grammar

The phrase grammar

ae (V N ∪V T ) *, and contains at least a VN, and β∈ (V N ∪V T ) *

There are no restrictions on the production

S->A

Type 1 grammar

Context-sensitive grammar

If any one of the production α → β, there | β | ≥ | α |, excluding S → ε only, in the form of production is described: α1Aα2 → α1βα2, A must be present in a context allowed replaced by β.

Production of the left more than one symbol, but there must be at least a nonterminal

aSb->aaSbb

S->ab

Type 2 grammar

Context-free grammar

For any production has α∈V N , β∈ (V N ∪V T ) *

Each production is only one left terminator

G[S]:

S→01

S→0S1

Type 3 grammar

Regular grammar

Production is in the form A → aB or A → a

Left only a terminator, the right to a non-terminal or a non-terminal plus a terminator

G[S]:

S→a

S→aB

4, the syntax tree

  Given grammar = G (V N , V T , P, S), the root of the tree grammar is S, each node has a flag, a sign of V.

  [Alpha] at any step derivation => β, where α, β is a sentence, of [alpha] is the leftmost nonterminal replaced, for the most left; if [alpha] on the rightmost nonterminal replaced, for the most the right derivation, also called canonical derivation .

  If there is a different grammar syntax of a sentence corresponding to the number two, then the grammar is ambiguous in.

  Sentence: a syntax tree sequence from left to right;

  Sentence: syntax tree terminator sequence from left to right, is in a sentence;

  Language: the set of all sentences generated by the grammar;

  The phrase: sub-tree of a terminal node according to the order from left to right sentence strings of symbols, the symbol string for the sentence with respect to the sub-tree of phrases;

  Simple phrases: leaf node subtree simple syntax tree from left to right to form a simple phrase;

  Handle: leftmost simple phrase.

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

Integer n

  n->0|1|2|3|……|8|9

I identifier

  i->a|b|c|……|y|z|A|B|C|……|Y|Z

Expression e

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

  <Item> -> <Factor> {<multiplication and division operator> <factor>}

  <Factor> -> i | n | '(' e ')'

  <加减运算符>->+|-

  <乘除运算符>*|/

条件语句

  stmt->if<条件>then<语句>

  <条件>->e<关系运算符>e|odd e

  <语句>-><赋值语句>|<条件语句>|<当型循环语句>|<过程调用语句>|<读语句>|<写语句>|<复合语句>|<空语句>

  <关系运算符>->==|#|<|<=|>|>=

赋值语句

  i=e

复合语句

  stmt->begin <语句>{;<语句>} end

  <语句>-><赋值语句>|<条件语句>|<当型循环语句>|<过程调用语句>|<读语句>|<写语句>|<复合语句>|<空语句>

函数

  <函数>-><主函数><其他函数>|<主函数>

程序

  <程序>-><分程序>

  <分程序>-><常量说明部分><语句>|<变量说明部分><语句>|<过程说明部分><语句>

  <常量说明部分>->const<常量定义>{,<常量定义>};

  <变量说明部分>->var i{,i};

  <过程说明部分>-><过程首部><分程序>{;<过程说明部分>};

  <过程首部>->procedure i;

  <语句>-><赋值语句>|<条件语句>|<当型循环语句>|<过程调用语句>|<读语句>|<写语句>|<复合语句>|<空语句>

  <过程循环语句>call i

  <当型调用语句>->while<条件>do<语句>

  <读语句>->write’(‘e{,e}’)’

  <写语句>->read’(‘i{,i}’)’

  <空语句>->ε

Guess you like

Origin www.cnblogs.com/cyxxixi/p/11588652.html