Pla an The Toy Lan nguage Pico Pico has two types: natu ural - - PowerPoint PPT Presentation

pla an the toy lan nguage pico
SMART_READER_LITE
LIVE PREVIEW

Pla an The Toy Lan nguage Pico Pico has two types: natu ural - - PowerPoint PPT Presentation

Pla an The Toy Lan nguage Pico Pico has two types: natu ural number and string Booleans Variables have to be dec Variables have to be dec clared clared Steps towards a Pico en S d Pi nvironment i Statements: assign,


slide-1
SLIDE 1

Pla

  • Booleans

S d Pi

  • Steps towards a Pico en

– Step 1: define syntax – Step 2: define a typechec – Step 3: define an evaluat

Step 3: define an evaluat

– Step 4: define a compile

T l f ti

  • Traversal functions
  • Methodology

Introduction

an

i nvironment

cker tor tor er

to ASF+SDF 1

The Toy Lan

  • Pico has two types: natu

Variables have to be dec

  • Variables have to be dec
  • Statements: assign, if-th
  • Expressions: natural, str

+ and ha e nat ral ope

  • + and - have natural ope
  • || has string operands a

|| g p

  • Tests (if, while) should

Introduction

nguage Pico

ural number and string clared clared hen-else, while-do ring, +, - and || erands; the res lt is nat ral erands; the result is natural and the result is string be of type natural

to ASF+SDF 2

A Pico P

begin declare input : natural begin declare input : natural,

  • utput : natural,

repnr: natural, rep: natural; rep: natural; input := 14;

  • utput := 1;

while input 1 do while input - 1 do rep := output; repnr := input; hil 1 d while repnr - 1 do

  • utput := output + rep;

repnr := repnr - 1 d

  • d;

input := input - 1

  • d

d

Introduction

end

Program

input value input value

  • utput value

What does this program compute?

to ASF+SDF 3

A Pico P

begin declare input : natural begin declare input : natural,

  • utput : natural,

repnr: natural, rep: natural; rep: natural; input := 14;

  • utput := 1;

while input 1 do while input - 1 do rep := output; repnr := input; hil 1 d while repnr - 1 do

  • utput := output + rep;

repnr := repnr - 1 d

  • d;

input := input - 1

  • d

d

Introduction

end

Program

input value input value

  • utput value

14! 14 * 13 * * 1 What does this program compute? 14! = 14 * 13 * ... * 1 Why is it written in this l l (a) Pico has no input/output clumsy style? ( ) p p statements (b) Pico has no multiplication

  • perator

to ASF+SDF 4

  • perator
slide-2
SLIDE 2

Pla

  • Booleans

S d Pi

  • Steps towards a Pico e

– Step 1: define syntax – Step 2: define a typechec – Step 3: define an evaluat

Step 3: define an evaluat

– Step 4: define a compile

T l f ti

  • Traversal functions
  • Methodology

Introduction

an

i nvironment

cker tor tor er

to ASF+SDF 5

Step 1: Define

basic/NatCon basic/Str languages/pico/ languages/pico/ basic/Whitespace

Introduction

syntax for Pico

languages/pico/syntax/Types rCon syntax/Pico syntax/Pico languages/pico/syntax/Identifiers

to ASF+SDF 6

Pico-sy

module languages/pico/syntax/Pico m g g /p / y / imports languages/pico/syntax/Identifiers languages/pico/syntax/Types languages/pico/syntax/Types basic/NatCon basic/StrCon exports exports sorts PROGRAM DECLS ID-TYPE STATEMENT context-free start-symbols context free start symbols PROGRAM context-free syntax "begin" DECLS {STATEMENT ";"}* "end" begin DECLS {STATEMENT ; } end "declare" {ID-TYPE ","}* ";" PICO-ID ":" TYPE

Introduction

yntax, 1

Sorts and syntax rules Sorts and syntax rules for program and declarations

T EXP

List of zero or more statements

  • > PROGRAM

separated by ; * zero or more +

> PROGRAM

  • > DECLS
  • > ID-TYPE

+ one or more

to ASF+SDF 7

Pico sy Pico-sy

PICO-ID ":=" EXP "if" EXP "then" {STATEMENT ";"}* "else" {STATEMENT ";"}* "fi" "while" EXP "do" {STATEMENT ";"}* "od“

Introduction

yntax 2 yntax, 2

Syntax rules for statements

  • > STATEMENT
  • > STATEMENT

  • > STATEMENT

to ASF+SDF 8

slide-3
SLIDE 3

Pico-sy

PICO-ID -> EXP NatCon -> EXP StrCon -> EXP StrCon -> EXP EXP "+" EXP -> EXP {left} EXP "-" EXP -> EXP {left} EXP "||" EXP -> EXP {left} "(" EXP ")" > EXP {b k t} "(" EXP ")" -> EXP {bracket} context-free priorities EXP "||" EXP > EXP > EXP "||" EXP -> EXP > EXP "-" EXP -> EXP > EXP "+" EXP -> EXP

Introduction

yntax, 3

Syntax rules for expressions Syntax rules for expressions The sort NatCon is imported from basic/NatCon from basic/NatCon The sort StrCon is imported from basic/StrCon from basic/StrCon The three operators are l ft i ti left-associative The priorities of the three p

  • perators, a disambiguation

construct: 1 - (2 + 3) , or (1 - 2) + 3 ??

to ASF+SDF 9

(1 - 2) + 3 ??

Ident

d l l / i / t /Id tif module languages/pico/syntax/Identif exports sorts PICO-ID lexical syntax [a-z] [a-z0-9]* -> PICO-ID [a z] [a z0 9] > PICO ID lexical restrictions PICO ID / [ 0 9] PICO-ID -/- [a-z0-9]

A l i l t i ti A lexical restricti

  • /- can be used to

Introduction

tifiers

fi fiers

Repeat zero (*) or one (+) or more times ( ) A character class: PICO-ID starts with a lowercase letter i i th t id tifi ? ion: is aaa three, two or one identifier?

  • define

longest match

to ASF+SDF 10

Pico-T

module languages/pico/syntax/Types exports exports sorts TYPE context-free syntax "natural" -> TYPE "string" -> TYPE "nil-type" -> TYPE yp

Introduction

Types

The sort of possible types in a Pico program program The constants natural and string represent types as can be declared in represent types as can be declared in Pico program The constant nil-type is used for handling error cases

to ASF+SDF 11

Pico: factor

begin declare input : n begin declare input : n

  • utput

repnr: n rep: nat rep: nat input := 14;

  • utput := 1;

while input 1 d while input - 1 d rep := output repnr := inpu while repnr while repnr -

  • utput := o

repnr := re d;

  • d;

input := input

  • d

d

Introduction

end

rial program

natural natural, : natural, natural, ural; ural; do do t; ut; 1 do

  • 1 do
  • utput + rep;

epnr - 1 t - 1

to ASF+SDF 12

slide-4
SLIDE 4

Syntax for Pi

  • The modules languages

defines (together with th defines (together with th syntax for the Pico langu Thi t b d

  • This syntax can be used

– Generate a parser that ca – Generate a syntax-direct – Generate a parser that ca

p fragments of Pico progra

Introduction

ico: summary

s/pico/syntax/Pico he imported modules) the he imported modules) the uage d t d to

an parse Pico programs ted editor for Pico programs an parse equations containing p q g ams

to ASF+SDF 13

Intermezzo:

An elementary symbol is:

Li l “ b ”

– Literal: “abc” – Sort (non-terminal) nam – Character classes: [a-z]:

  • ~: complement of charac

p

  • /: difference of two char

/\: intersection of two ch

  • /\: intersection of two ch
  • \/: union of two charact

Introduction

Symbols (1)

mes: INT : one of a, b, ..., z

cter class. racter classes. haracter classes haracter classes. er classes.

to ASF+SDF 14

Intermezzo:

A complex symbol is: A complex symbol is:

– Repetition:

  • S* zero or more times S
  • {S1 S2}* zero or more
  • {S1 S2}

zero or more

  • {S1 S2}+ one or more t

O ti l S?

– Optional: S? zero or one – Alternative: S | T an S o

– Tuple: <S,T> shorthand f

– Parameterized sorts: S[[

Introduction

Symbols (2)

S; S+ one or more times S

times S1 separated by S2 times S1 separated by S2 times S1 separated by S2

f S e occurrences of S

  • r a T

for “<” S “,” T “>”

[ P1, P2 ]]

to ASF+SDF 15

Intermezzo: produ

  • General form of a produ

1 2

– S1 S2 ... Sn -> S0 Attr

  • Lexical syntax and context-

– Between the symbols in

symbols may occur in th

– A context-free productio

  • S1 LAYOUT? S2 LAY
  • S1 LAYOUT? S2 LAY

Introduction

uctions (functions)

uction (function):

b ributes

  • free syntax are similar, but

a production optional layout he input text.

  • n is equivalent with:

YOUT? LAYOUT? Sn -> S0 YOUT? ... LAYOUT? Sn S0

to ASF+SDF 16

slide-5
SLIDE 5

Example: floatin

sorts UnsignedInt SignedInt Unsig lexical syntax [0] | ([1-9][0-9]*) [\+\-]? UnsignedInt [\ \ ]? UnsignedInt UnsignedInt "." [0-9]+ ([eE] Sig Unsi n dInt [ E] Si n dInt UnsignedInt [eE] SignedInt UnsignedInt | UnsignedReal 0 1 14 0.1 3e4 3.014e-7

Introduction

ng point numbers

gnedReal Number

  • > UnsignedInt
  • > SignedInt

SignedInt gnedInt)? -> UnsignedReal > Unsi n dR l

  • > UnsignedReal
  • > Number

00 01 04.1 3e04 3.14e-07

to ASF+SDF 17

Intermezzo: list

A “ ”

A+ a a a a

Assume: “a”

{A “;”}+

a ; a

a a (A “;”)+ ( “ ) a ; a ;

a ; a;

(A “;”?)+ a a a a a a

Introduction

ts, lists, lists, ...

A

  • > A

a ; a; a a ; a; a;

;

a ; a; a;

a ; a; a a ; a; a a ; a; a ; a

to ASF+SDF 18

Intermezzo: disa Intermezzo: disa

Wh l di bi ti

  • Why manual disambiguation

– Fully declarative definition of syntax

I li i di bi i b

– Implicit disambiguation can be wron – More programming languages can b

Modularity

– Modularity – Fewer non-terminals/sorts – Separation of concerns: form of rule

p

  • Costs?

– Intellectual effort

Intellectual effort

– False safety, grammar may still be am Introduction

ambiguation (1) ambiguation (1)

i SDF? in SDF?

x ng e parsed es is independent of disambiguation p g mbiguous

to ASF+SDF

Intermezzo: disa Intermezzo: disa

A i ti it f bi

  • Associativity of binary op
  • Priorities between binary

y

– Transitivity of priorities

Prefix expressions and bin

– Prefix expressions and bin – Postfix expression with bi

O l d d ’ i

– Overloaded comma’s in ex

Introduction

ambiguation (2) ambiguation (2)

t perators

  • perators

p

nary expressions nary expressions inary operator (slide 3+4) i l ( lid 5 6) xpression languages (slide 5+6)

to ASF+SDF

slide-6
SLIDE 6

Intermezzo: disa Intermezzo: disa

module Expressions module Expressions exports sorts E lexical syntax [\ \t\n] -> LAYOUT context-free start-symbols E context-free syntax "e" -> E E "[" E "]" -> E E "+" E -> E {left} I t t + [ + ] Input sentence: e + e [ e + e ]

Introduction

ambiguation (3) ambiguation (3)

to ASF+SDF