Signatures and grammars Signatures and grammars Why manual - - PowerPoint PPT Presentation

signatures and grammars signatures and grammars
SMART_READER_LITE
LIVE PREVIEW

Signatures and grammars Signatures and grammars Why manual - - PowerPoint PPT Presentation

Signatures and grammars Signatures and grammars Why manual disambiguation in SDF? Associativity of binary operators y g y y Fully declarative definition of syntax Priorities between binary operators Implicit disambiguation


slide-1
SLIDE 1

Signatures and grammars

  • Why manual disambiguation in SDF?

y g

  • Fully declarative definition of syntax
  • Implicit disambiguation can be wrong
  • More programming languages can be parsed
  • More programming languages can be parsed
  • Modularity
  • Fewer non-terminals/sorts
  • Separation of concerns: form of rules is independent of

disambiguation

  • Costs?
  • Intellectual effort
  • False safety, grammar may still be ambiguous

/ Faculteit Wiskunde en Informatica

PAGE 0 20-9-2011

Signatures and grammars

  • Associativity of binary operators

y y

  • Priorities between binary operators
  • Transitivity of priorities
  • Prefix expressions and binary expressions
  • Postfix expression with binary operator (slide 3+4)
  • Overloaded comma’s in expression languages (slide

Overloaded comma s in expression languages (slide 5+6)

/ Faculteit Wiskunde en Informatica

PAGE 1 20-9-2011

Signatures and grammars

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} Input sentence: e + e [ e + e ]

/ Faculteit Wiskunde en Informatica

PAGE 2 20-9-2011

Signatures and grammars

module Expressions t

  • The "< >" notation is used to

exports sorts E lexical syntax [\ \t\n] -> LAYOUT

restrict the filtering behavior

  • f priorities to certain

arguments.

[\ \t\n] > LAYOUT context-free start-symbols E context-free syntax "e" -> E

  • In this case, if a "+" is a direct

child of the first "E" in the "[ ]" production, it is filtered.

context-free priorities E "[" E "]" -> E <0> > E "+" E > E {l ft}

  • No other direct children are

filtered but the ones listed between the angular brackets.

E "+" E -> E {left}

/ Faculteit Wiskunde en Informatica

PAGE 3 20-9-2011

slide-2
SLIDE 2

Signatures and grammars

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

/ Faculteit Wiskunde en Informatica

PAGE 4 20-9-2011

Signatures and grammars

/ Faculteit Wiskunde en Informatica

PAGE 5 20-9-2011

Signatures and grammars

module Expressions exports

  • The priority definition reveals

exports sorts E lexical syntax [\ \t\n] -> LAYOUT t t f t t b l E

some of the implementation details of SDF, by showing

  • ne of the productions that

are automatically generated

context-free start-symbols E context-free syntax "e" -> E "f" "(" {E ","}+ ")" -> E

are automatically generated for you.

  • There is no way around this,

unless you would like to

context-free priorities { non-assoc: E -> {E “,”}+ E "," E -> E {left} }

unless you would like to remove the comma separated argument list and use the binary comma operator binary comma operator instead for parsing your commas.

/ Faculteit Wiskunde en Informatica

PAGE 6 20-9-2011

Signatures and grammars

  • Longest match

g

  • Runaway whitespace and comments
  • Syntactic overloading between identifiers and

k d keywords

  • Useless cloning of identifier classes
  • Dynamically reserved types
  • Dynamically reserved types
  • Dangling else and related ambiguities

/ Faculteit Wiskunde en Informatica

PAGE 7 20-9-2011

slide-3
SLIDE 3

Signatures and grammars

  • Runaway whitespace and comments

y

module Layout exports exports sorts P S context-free start-symbols P lexical syntax lexical syntax [\ \t\n] -> LAYOUT context-free syntax context-free syntax "[" S* "]" -> P "s" -> S

/ Faculteit Wiskunde en Informatica

PAGE 8 20-9-2011

Signatures and grammars

/ Faculteit Wiskunde en Informatica

PAGE 9 20-9-2011

Signatures and grammars

  • These trees have typical symptoms:

y y

  • a nullable symbol (for example a star list), which

recognizes the empty language

  • two layout nodes surrounding the nullable non terminal
  • two layout nodes surrounding the nullable non-terminal

take turns in accepting the layout.

/ Faculteit Wiskunde en Informatica

PAGE 10 20-9-2011

Signatures and grammars

module Layout exports sorts P S context-free start-symbols P lexical syntax [\ \t\n] -> LAYOUT "%" ~[\%]* "%" -> LAYOUT context-free restrictions LAYOUT? -/- [\ \t\n] LAYOUT? -/- [\%] context-free syntax "[" S* "]" -> P "s" -> S

/ Faculteit Wiskunde en Informatica

PAGE 11 20-9-2011

slide-4
SLIDE 4

Signatures and grammars

  • Syntactic overloading between identifiers and keywords

module Keywords exports sorts S E context-free start-symbols S lexical syntax [A-Za-z][A-Za-z0-9]* -> E [\ \t\n] -> LAYOUT context-free syntax E "(" {E ","}* ")" ";" -> S "return" E ";" -> S "(" E ")" -> E {bracket}

/ Faculteit Wiskunde en Informatica

PAGE 12 20-9-2011

Signatures and grammars

/ Faculteit Wiskunde en Informatica

PAGE 13 20-9-2011

Signatures and grammars

  • The symptoms of this ambiguity are:

y g y

  • The top two nodes of the ambiguity differ
  • There is more than one instance of syntactic
  • verloading:
  • verloading:

− "return" keyword, and the bracket syntax "( )" infer with other parts of the syntax − ";" is allocated to a different production.

  • Characters that are grouped under a keyword node

("return") in one alternative, end up under an identifier ( return ) in one alternative, end up under an identifier node in another ("E").

/ Faculteit Wiskunde en Informatica

PAGE 14 20-9-2011

Signatures and grammars

module Keywords exports sorts S E context-free start-symbols S lexical syntax [A-Za-z][A-Za-z0-9]* -> E [\ \t\n] -> LAYOUT "return" -> E {reject} context-free syntax E "(" {E ","}* ")" ";" -> S "return" E ";" -> S "(" E ")" -> E {bracket}

/ Faculteit Wiskunde en Informatica

PAGE 15 20-9-2011

slide-5
SLIDE 5

Signatures and grammars

  • Useless cloning of identifier classes

module Clone exports sorts Identifier ClassName TypeName Declaration context-free start-symbols Declaration lexical syntax [\ \t\n] -> LAYOUT [a-z]+ -> Identifier [a-z]+ -> ClassName context-free syntax Identifier -> TypeName ClassName -> TypeName TypeName Identifier ";" -> Declaration

/ Faculteit Wiskunde en Informatica

PAGE 16 20-9-2011

Signatures and grammars

/ Faculteit Wiskunde en Informatica

PAGE 17 20-9-2011

Signatures and grammars

  • Symptoms are very clear:

y y

  • The two top productions of the ambiguity are different
  • The two top productions of the ambiguity are injections

(chain rules) (chain rules)

  • Each chain rule introduces a lexical (identifier) that has

the same definition, but a different non-terminal name

  • Each alternative recognizes exactly the same

characters in exactly the same way, modulo non- terminal names

/ Faculteit Wiskunde en Informatica

PAGE 18 20-9-2011

Signatures and grammars

  • Useless cloning of identifier classes

module Clone exports sorts Identifier Declaration context-free start-symbols Declaration lexical syntax [\ \t\n] -> LAYOUT [a-z]+ -> Identifier context-free syntax Identifier Identifier ";" -> Declaration

/ Faculteit Wiskunde en Informatica

PAGE 19 20-9-2011

slide-6
SLIDE 6

Signatures and grammars

  • Dangling else and related ambiguities

module DanglingElse exports sorts S E lexical syntax [\ \t\n] -> LAYOUT context-free restrictions LAYOUT? -/- [\t\n\ ] context-free start-symbols S context-free syntax "expr" -> E "if" E "then" S+ -> S "if" E "then" S+ "else" S+ -> S "other" -> S

/ Faculteit Wiskunde en Informatica

PAGE 20 20-9-2011

Signatures and grammars

/ Faculteit Wiskunde en Informatica

PAGE 21 20-9-2011

Signatures and grammars

  • Symptoms in these two trees:

y

  • Top nodes of the derivations have different productions

(this is not always the case in this kind of ambiguity)

  • Both trees exercise the same production rules but in
  • Both trees exercise the same production rules, but in

different vertical order

  • One of the productions is a prefix of the other,
  • and, these two productions are the ones that have

swapped vertical order between the two derivations

  • In both derivations, the deeply nested statement is the

In both derivations, the deeply nested statement is the

  • nly, or the last, statement of a list of statements

/ Faculteit Wiskunde en Informatica

PAGE 22 20-9-2011

Signatures and grammars

  • Dangling else and related ambiguities

module DanglingElse exports sorts S E lexical syntax [\ \t\n] -> LAYOUT context-free restrictions LAYOUT? -/- [\t\n\ ] context-free start-symbols S context-free syntax "expr" -> E "if" E "then" S+ -> S {avoid} "if" E "then" S+ "else" S+ -> S "other" -> S

/ Faculteit Wiskunde en Informatica

PAGE 23 20-9-2011