You are on page 1of 21

. , , .

. , . .

I
.

if condition then statement . while condition do statement for each item in collection do statement . . , ,

(dynamic typing)

for each .

. , , . . . , , . . 2 . SOV . . if

, . , . (0, 0) (0, 0) . (1, 1) (1, 1)

, , 45% SOV . , . SOV SOV [3]. . Tomlin

. , II . . III IV , ,V . , .

II

I . , , .

II.1

,
,

. ( )

IEEE 754

binary64 . 52

. bi32

nary64

253 . ,

91015 ISO/IEC 10646 . 4 (collection)

(associative array) W3C . .

[2]

EBNF(Extended Backus-Naur Form)

number_literal ::= [0-9]+ (.[0-9]+)? (_[0-9]+)? () . string_literal ::= #x22 (#x5C#x22|[^#x00-#x1F#x22#x80-#x9F]) #x22 | #x29 (#x5C#x29|[^#x00-#x1F#x29#x80-#x9F]) #x29 . (terminal symbol) . .

table_constructor ::= "[" [((expr ":")? expr ",")* (expr ":")? expr] "]" , expr . function call . primary_expr ::= identifier | literal | table_element_reference | "(" expr ")" II.3 , primary expr function call , I primary expr

literal ::= " " | "

" | "

" | number_literal | string_literal

| table_constructor 5

+ 1 * 2 3 / % + < 4 <= > >= 5 6 7 = <>

( ( (

) ) ) < > = = (repetition) (split) (concatenation)

I.

table_element_reference ::= primary_expr "." (identifier | literal | "(" expr ")")

(.) , . " " -> [ : 1,

(record)

: 2] -> . . 6 1, .( ) 2 .

(:) (assignment) , , -> , , ,

expr

(->)

2 . identifier L, M, N, Co . W3C EBNF . identifier ::= ((L | M | Co) (L | M | N | Co)*) - (" | " " | " " | " " | " " | " " | " " | " " | " ") " ,

II.2
II.3 (for each 1 ( ) ) . (if-else . while )

2 (

) (

.)

.)

. . 7

else

/ continue

} break . , while ,

. -> , , . , -> ,

(->)

, .

->

->

->

, i = j = 0 .

II.3
. word . function_call ::= function_call? primary_expr* identifier "!" (!) " , !" ! . . , , . . Forth .

. [ : " , . function_definition ::= identifier ":" (identifier+("...")?)? compound_statement . : . ->! ->! + " (!) . " + . + " !" ->! (->) . ", : 21] ! . !

(object-oriented) . . : ! [ : " ! ", ! : 16] ->

(object-based) II.4 .) 9

. (--

II.4
, . end , (block) . . . -(sig dashes) -. . , -, (comment) -, . , begin,

III
. (reference implementation) (stack virtual machine) , . (compiler) ) III.4 (machine language) ) , ( [1]. (source code; , JIT(just-in-time)

10

(instruction set) . , . (intermediate representation) (source language) , ,

, . (front-end) (lexical analyzer), (semantic analyzer) . (back-end) , [1]. ([1]

, , analysis)

(syntax analyzer) ([1] . , , synthesis)

(code generator)

. Compiler) GNU bison ,

yacc (Yet Another CompilerLALR (look-ahead left-

to-right rightmost derivation) . Lexical Analyzer Generator)

GLR (generalized left-to-right rightmost derivation) lex . . GNU ex (Fast

, .

11

III.1
. . , II . let stack = empty; let indent = number of blank characters at the beginning of the line; if indent > stack top: push indent to stack; send token BEGIN to parser; else if indent < stack top: send token LINE_BREAK to parser; while indent < stack top: pop stack; send token END to parser; send token LINE_BREAK to parser; else send token LINE_BREAK to parser; . ,

III.2
(parsing) . 12 ,

LR(1)

lookahead LALR(1)

LR(1)

reduce/reduce

. LALR(1) , shift/reduce reduce/reduce yacc mid-rule . ,

syntax-directed translation . , primary_expr

function_call LALR(1) mid-rule 24 LR(1) . ,

syntax-directed translation

statement statement statement statement

expr IF . compound_statement expr IF . compound_statement . or_if_unless expr IF . compound_statement . UNLESS compound_statement expr IF . compound_statement . or_if_unless UNLESS compound_statement

statement statement statement statement

expr UNLESS . compound_statement expr UNLESS . compound_statement . or_if_unless expr UNLESS . compound_statement . UNLESS compound_statement expr UNLESS . compound_statement . or_if_unless UNLESS compound_statement

13

reduce/reduce . GNU bison %start program %% primary_expr: IDENTIFIER | LITERAL | table_constructor | table_element_reference | ( expr ); .

table_constructor: [ ] | [ table_element_list ]; table_element_list: table_element | table_element_list , table_element; table_element: expr | expr : expr;

table_element_reference: primary_expr . subscript; subscript: IDENTIFIER | LITERAL | table_constructor | ( expr ); function_call: IDENTIFIER ! | argument_list IDENTIFIER ! | function_call IDENTIFIER ! | function_call argument_list !; argument_list: primary_expr | argument_list primary_expr;

expr: function_call | logical_and_expr | expr OR logical_and_expr; /* 14 */

logical_and_expr: bitwise_or_expr | logical_and_expr AND equality_expr; /* */

equality_expr: inequality_expr | equality_expr equality_operator inequality_expr; equality_operator: = | NOT_EQUAL; /* <> */

inequality_expr: additive_expr | inequality_expr inequality_operator additive_expr; inequality_operator: < | LT_EQUAL | > | GT_EQUAL; /* <=, >= */

additive_expr: multiplicative_expr | additive_expr additive_operator multiplicative_expr; +additive_operator: + | -;

multiplicative_expr: unary_expr | multiplicative_expr multiplicative_operator unary_expr; multiplicative_operator : * | / | %;

unary_expr: primary_expr | unary_operator primary_expr; unary_operator: + | - | ~;

program: function_definition | statement | program function_definition | program statement;

15

function_definition: IDENTIFIER : block | IDENTIFIER : parameter_list block | IDENTIFIER : parameter_list ELLIPSIS block; /* ... */

block: BEGIN statement_list END LINE_BREAK;

statement_list: statement | statement_list statement;

statement: LINE_BREAK | function_call LINE_BREAK | assign_statement LINE_BREAK | BEGIN statement_list END LINE_BREAK | expr IF_THEN block /* | expr UNLESS_THEN block /* | expr IF_THEN block or_then | expr UNLESS_THEN block or_then | expr IF_THEN block UNLESS_THEN block | expr UNLESS_THEN block UNLESS_THEN block | expr IF_THEN block or_then UNLESS_THEN block | expr UNLESS_THEN block or_then UNLESS_THEN block | CONTINUE LINE_BREAK /* */ */ */ */

| expr EACH IDENTIFIER REPEAT block /* | NEXT LINE_BREAK /* | BREAK LINE_BREAK /* */ */

| expr RETURN LINE_BREAK; /* ->! */

assign_statement: source_list ASSIGN destination_list /* -> */ | assign_statement ASSIGN destination_list;

16

source_list: expr | source_list , expr; destination_list: IDENTIFIER | table_element_reference | destination_list , IDENTIFIER | destination_list , table_element_reference;

or_then: OR expr IF_THEN block /* | OR expr UNLESS_THEN block /*

*/ */

| or_then OR expr IF_THEN block | or_then OR expr UNLESS_THEN block;

parameter_list: IDENTIFIER | parameter_list IDENTIFIER; %%

III.3
parse tree syntax-directed translation , . parse tree (type checking) (symbol table) . 1

III.4
. ,

17

1. Syntax-directed translation

. . . Python

IV
(depth-rst search) . .

18

: +[ .( ] -> ) ! ! -> ->! : [ ] -> !=0 0 ! -> ! +[ .( ] -> )+ ->

->! : [ ] -> !=0 0 ! -> ! +[ + ] -> .( ) ->

->!

:[ , ], :[ , ], :[ , ], :[ ], :[ ]] -> [] ! ! ! -19

[] []

! !

! !

! !

, , , .

, . SOV . .

SOV .

[1] A. Aho, M. Lam, R. Sethi, and J. Ullman. Compilers: Principles, Techniques, and Tools (2nd ed.). AddisonWesley, 2006.

20

[2] T. Bray et al. Extensible Markup Language (XML) 1.0 (5th ed.). World Wide Web Consortium, 2008. [3] R. Tomlin. Basic Word Order: Functional Principles. Croom Helm, 1986.

21

You might also like