+ All Categories
Home > Documents > Syntaxation - GOTO...

Syntaxation - GOTO...

Date post: 21-Mar-2020
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
65
Syntaxation
Transcript
Page 1: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Syntaxation

Page 2: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

JavaScript: The Universal Virtual Machine

Page 3: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

JSLint.

The Good Parts.

http://www.JSLint.com/

Page 4: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

WARNING! JSLint will hurt your feelings.

Page 5: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Syntax is the least important aspect of programming language

design.

Page 6: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Syntax is the least important aspect of programming language

design.

Fashion is the least important aspect of clothing design.

Page 7: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Programming Languages:

An Interpreter-Based Approach

Samuel N. Kamin [1990] Lisp

APL

Scheme

SASL

Clu

Smalltalk

Prolog

Page 8: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Minimal Syntax

Page 9: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Lisp

(fname arg1 arg2)

Page 10: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Smalltalk 80

object name1: arg name2: arg2

object operator arg

[ var | block ]

Page 11: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

IF Statement

And yet don't look too good, nor talk too wise

Page 12: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

C FORTRAN

IF(A-B)20,20,10

10 A=B

20 CONTINUE

Page 13: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

C FORTRAN IV

IF(A.LE.B)GO TO 30

A=B

30 CONTINUE

Page 14: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

comment ALGOL 60;

if a>b then begin

a:=b

end;

Page 15: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

// BCPL

IF A > B {

A := B

}

Page 16: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

/* B */

if (a > b) {

a = b;

}

Page 17: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

-- Ada

if a > b then

a := b;

end if;

Page 18: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

¢ Algol 68 ¢

if a > b then

a := b

fi

Page 19: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Emotional Style

Fashionable Tolerance of Syntaxtic Ambiguity

Page 20: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

a ☁ b ♥ c

((a ☁ b) ♥ c) (a ☁ (b ♥ c))

Page 21: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Binding Power 10 = += -= 20 ?

40 && ||

50 === < > <= >= !==

60 + -

70 * /

80 unary

90 . [ (

Page 22: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

word

variable?

statement keyword?

operator?

special form?

Page 23: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

( )

Function definition and invocation

Grouping

Separation

Page 24: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Parsing

Theory of Formal Languages

Page 25: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Tokens are objects

prototype ← symbol ← token

advance() advance(id)

Page 26: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

a = b + c;

Weave a stream of tokens into a tree

a =

b

+

c

Page 27: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Top Down Operator Precedence

•  Vaughan Pratt [POPL 1973]

•  simple to understand

•  trivial to implement

•  easy to use

•  extremely efficient

•  very flexible

•  beautiful

Page 28: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Why have you never heard of this?

•  Preoccupation with BNF grammars and their various offspring, along with their related automata and theorems.

•  Requires a functional programming language.

•  LISP community did not want syntax.

•  JavaScript is a functional language with a community that likes syntax.

Page 29: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

What do we expect to see to the left of the token?

left denotation led null denotation nud

Page 30: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

•  only nud ! ~ typeof { prefix

•  only led * . = === infix, suffix

•  nud & led + - ( [

Page 31: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

var prototype_token = { nud: function () { this.error("Undefined."); }, led: function (left) { this.error("Missing operator."); }, error: function(message) { ... }, lbp: 0 // left binding power };

Page 32: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

var symbol_table = {}; function symbol(id, bp) { var s = symbol_table[id]; bp = bp || 0; if (s) { if (bp >= s.lbp) { s.lbp = bp; } } else { s = Object.create(prototype_token); s.id = s.value = id; s.lbp = bp; symbol_table[id] = s; } return s; }

Page 33: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

symbol(":"); symbol(";"); symbol(","); symbol(")"); symbol("]"); symbol("}"); symbol("else"); symbol("(end)"); symbol("(word)");

Page 34: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

symbol("+", 60).led = function (left) {

this.first = left;

this.second = expression(60);

this.arity = "binary";

return this;

};

Page 35: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

symbol("*", 70).led = function (left) {

this.first = left;

this.second = expression(70);

this.arity = "binary";

return this;

};

Page 36: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

function infix(id, bp, led) { var s = symbol(id, bp); s.led = led || function (left) { this.first = left; this.second = expression(bp); this.arity = "binary"; return this; }; return s; }

Page 37: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

infix("+", 60); infix("-", 60); infix("*", 70); infix("/", 70); infix("===", 50); infix("!==", 50); infix("<", 50); infix("<=", 50); infix(">", 50); infix(">=", 50);

Page 38: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

infix("?", 20, function led(left) { this.first = left; this.second = expression(0); advance(":"); this.third = expression(0); this.arity = "ternary"; return this; });

Page 39: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

function infixr(id, bp, led) { var s = symbol(id, bp); s.led = led || function (left) { this.first = left; this.second = expression(bp - 1); this.arity = "binary"; return this; }; return s; }

Page 40: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

function assignment(id) { return infixr(id, 10, function (left) { if (left.arity !== "name" && left.id !== "." && left.id !== "[") { left.error("Bad lvalue."); } this.first = left; this.second = expression(9); this.assignment = true; this.arity = "binary"; return this; }); } assignment("="); assignment("+="); assignment("-=");

Page 41: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

function prefix(id, nud) { var s = symbol(id); s.nud = nud || function () { this.first = expression(80); this.arity = "unary"; }; return s; } prefix("+"); prefix("-"); prefix("!"); prefix("typeof");

Page 42: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

prefix("(", function () { var e = expression(0); advance(")"); return e; });

Page 43: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Statement denotation

first null denotation

fud

Page 44: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

function statement() { var exp, tok = token; if (tok.fud) { advance(); return tok.fud(); } exp = expression(0); if (!exp.assignment && exp.id !== "(") { exp.error("Bad expression statement."); } advance(";"); return exp; }

Page 45: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

function statements() { var array = []; while (token.nud || token.fud) { a.push(statement()); } return array; }

Page 46: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

function block() { advance("{"); var a = statements(); advance("}"); return a; }

Page 47: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

function stmt(id, f) { var s = symbol(id); s.fud = f; return s; }

Page 48: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

stmt("if", function () { advance("("); this.first = expression(0); advance(")"); this.second = block(); if (token.id === "else") { advance("else"); this.third = token.id === "if" ? statement() : block(); } this.arity = "statement"; return this; });

Page 49: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

stmt("if", function () { this.first = expression(0); this.second = block(); if token.id === "else" { advance("else"); this.third = token.id === "if" ? statement() : block(); } // BCPL this.arity = "statement"; return this; });

Page 50: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

stmt("if", function () { this.first = expression(0); advance("then"); this.second = statements(); if token.id === "else" then advance("else"); this.third = statements(); fi // Algol 68 advance("fi"); this.arity = "statement"; return this; });

Page 51: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

function expression(rbp) { var left, tok = token; advance(); left = tok.nud(); while (rbp < token.lbp) { tok = token; advance(); left = tok.led(left); } return left; }

Page 52: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

a = b + c;

Weave a stream of tokens into a tree

a =

b

+

c

Page 53: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

a = b + c; { id: "=", arity: "binary", first: {id: "a", arity: "word"}, second: { id: "+", arity: "binary", first: {id: "b", arity: "word"}, second: {id: "c", arity: "word"} } }

Page 54: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

a = b + c; statements() statement() expression(0) a.nud() while 0 < =.lbp =.led(a) expression(10) b.nud() while 10 < +.lbp +.led(b) expression(60) c.nud()

Page 55: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

a.b = c;

Weave a stream of tokens into a tree

a

=

b

. c

Page 56: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

a.b = c; { id: "=", arity: "binary", first: { id: ".", arity: "binary", first: {id: "a", arity: "word"}, second: {id: "b", arity: "word"} }, second: {id: "c", arity: "word"} }

Page 57: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

a.b = c; statements() statement() expression(0) a.nud() while 0 < ..lbp ..led(a) expression(90) b.nud() while 90 < =.lbp while 0 < =.lbp =.led(a.b) expression(60) c.nud()

Page 58: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Top Down Operator Precedence

•  It is easy to build parsers with it.

•  It is really fast because it does almost nothing.

•  It is fast enough to use as an interpreter.

•  Dynamic: Build DSLs with it.

•  Extensible languages.

•  No more reserved words.

Page 59: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Advice for language designers

Page 60: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Minimalism

•  Conceptual

•  Notational   Don't be cryptic

•  Error resistant   Confusion free

•  Readable   Can be easily and correctly understood by a

reader

Page 61: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Innovate

•  We already have many Java-like languages. CokeBottle cokeBottle = new CokeBottle();

•  Select your features carefully.

•  Beware of Sometimes Useful.

•  Avoid universality.

•  Manage complexity.

•  Promote quality.

Page 62: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Innovate •  Make new mistakes. •  Let the language teach you. •  Embrace Unicode. •  Leap forward. •  Forgotten treasure:

State machines, constraint engines. •  Exploit parallelism. •  Distributed programming: clouds & cores. •  Have fun.

Page 63: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

;

Page 64: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

https://github.com/douglascrockford/TDOP

https://github.com/douglascrockford/JSLint

Beautiful Code: Leading Programmers Explain How They Think [Chapter 9] Oram & Wilson O'Reilly

Page 65: Syntaxation - GOTO Conferencegotocon.com/dl/goto-aar-2013/slides/DouglasCrockford_Syntaxation.pdf · Top Down Operator Precedence • It is easy to build parsers with it. • It is

Thank you and good night.


Recommended