Escolar Documentos
Profissional Documentos
Cultura Documentos
. , . .
I
.
if condition then statement . while condition do statement for each item in collection do statement . . , ,
(dynamic typing)
for each .
. , , . . . , , . . 2 . SOV . . if
. , II . . III IV , ,V . , .
II
I . , , .
II.1
,
,
. ( )
IEEE 754
binary64 . 52
. bi32
nary64
253 . ,
[2]
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
" | "
| table_constructor 5
( ( (
I.
(record)
: 2] -> . . 6 1, .( ) 2 .
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-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
, , analysis)
(code generator)
, .
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
syntax-directed translation
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
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;
inequality_expr: additive_expr | inequality_expr inequality_operator additive_expr; inequality_operator: < | LT_EQUAL | > | GT_EQUAL; /* <=, >= */
15
function_definition: IDENTIFIER : block | IDENTIFIER : parameter_list block | IDENTIFIER : parameter_list ELLIPSIS block; /* ... */
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 /* */ */ */ */
16
source_list: expr | source_list , expr; destination_list: IDENTIFIER | table_element_reference | destination_list , IDENTIFIER | destination_list , table_element_reference;
*/ */
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
->!
:[ , ], :[ , ], :[ , ], :[ ], :[ ]] -> [] ! ! ! -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