Declarative Syntax Definitionpretty printingGuido Wachsmuth       Delft                                Course IN4303 2012/...
Assessmentlast lecture               Pretty Printing   2
Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.                                      ...
Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.Discuss additional description means p...
Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.Discuss additional description means p...
Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.Discuss additional description means p...
Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.Discuss additional description means p...
Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.Discuss additional description means p...
Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.Discuss additional description means p...
Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.Discuss additional description means p...
Overviewtoday’s lecture                  Lexical Analysis   3
Overviewtoday’s lectureplagues of traditional parsing algorithms  •   paradise lost  •   paradise regained                ...
Overviewtoday’s lectureplagues of traditional parsing algorithms  •    paradise lost  •    paradise regainedpretty-printin...
Overviewtoday’s lectureplagues of traditional parsing algorithms  •    paradise lost  •    paradise regainedpretty-printin...
Iparadise lost & regained                           Pretty Printing   4
Pure and Declarative       Syntax Definition: Paradise Lost and RegainedLennart Kats       Eelco Visser       Guido Wachsmu...
PARADISE
PARADISE LOST
PARADISEREGAINED
PARADISE DENIED
PARADISE
WORDS
TREES
GRAMMARS
LANGUAGEENGINEERS
LANGUAGES
GRAMMARS
NATURAL
PURE
BEAUTIFUL
SOFTWAREENGINEERS
LANGUAGESOFTWARE
NOTNATURAL
NOTPURE
NOTBEAUTIFUL
SYNTAXDEFINITIONS
NATURAL
PURE
BEAUTIFUL
THE FALL
PARSERDEFINITIONS
PARADISE LOST
PAIN
SWEAT
NOTNATURAL
NOTPURE
NOTBEAUTIFUL
THE PLAGUES
context-free grammars      LL(k)        LR(k)      LL(1)        LR(1)                 LALR(1)                    SLR      ...
DISAMBIGUATION
LEXICAL SYNTAX
TREECONSTRUCTION
EVOLUTION
COMPOSITION
RESTRICTION  TO PARSERS
PARADISE
WORDS WERE MADE THROUGH   GRAMMARS
GRAMMARS
Num ➝ Digit NumNum ➝ DigitDigit ➝ “0”Digit ➝ “1”
Num ➝ Digit NumNum ➝ DigitDigit ➝ “0”Digit ➝ “1”                  production rules
Num ➝ Digit NumNum ➝ DigitDigit ➝ “0”Digit ➝ “1”                  terminal symbols                   production rules
Num ➝ Digit NumNum ➝ DigitDigit ➝ “0”Digit ➝ “1”                  nonterminal symbols                    terminal symbols ...
Num ➝ Digit NumNum ➝ DigitDigit ➝ “0”Digit ➝ “1”                  nonterminal symbols                    terminal symbols ...
WORDS
Num
Num         Num ➝ Digit NumDigit Num
Num           Num ➝ Digit NumDigit Num     Num ➝ DigitDigit Digit
Num           Num ➝ Digit NumDigit Num     Num ➝ DigitDigit Digit   Digit ➝ “0”Digit 0
Num           Num ➝ Digit NumDigit Num     Num ➝ DigitDigit Digit   Digit ➝ “0”Digit 0       Digit ➝ “1”10
SENTENCES
Exp ➝ Exp “+” ExpExp ➝ Exp “*” ExpExp ➝ Num                    production rules
Exp ➝ Exp “+” ExpExp ➝ Exp “*” ExpExp ➝ Num                    terminal symbols                     production rules
Exp ➝ Exp “+” ExpExp ➝ Exp “*” ExpExp ➝ Num                    nonterminal symbols                      terminal symbols  ...
Exp ➝ Exp “+” ExpExp ➝ Exp “*” ExpExp ➝ Num                    nonterminal symbols                      terminal symbols  ...
Exp
Exp         Exp ➝ Exp “+” ExpExp + Exp
Exp               Exp ➝ Exp “+” ExpExp + Exp         Exp ➝ Exp “*” ExpExp * Exp + Exp
Exp               Exp ➝ Exp “+” ExpExp + Exp         Exp ➝ Exp “*” ExpExp * Exp + Exp   Exp ➝ Num3 * Exp + Exp
Exp               Exp ➝ Exp “+” ExpExp + Exp         Exp ➝ Exp “*” ExpExp * Exp + Exp   Exp ➝ Num3 * Exp + Exp     Exp ➝ N...
Exp               Exp ➝ Exp “+” ExpExp + Exp         Exp ➝ Exp “*” ExpExp * Exp + Exp   Exp ➝ Num3 * Exp + Exp     Exp ➝ N...
THEY MADE LANGUAGES     BY MAKING GRAMMARS
GRAMMAR
LANGUAGE
TRUTH
Exp ➝ Exp “+” Exp   Exp “+” Exp ➝ ExpExp ➝ Exp “*” Exp   Exp “*” Exp ➝ ExpExp ➝ Num                 Num ➝ Expproductive   ...
3 * 7 + 21
3 * 7 + 21    Num ➝ Exp3 * 7 + Exp
3 * 7 + 21      Num ➝ Exp3 * 7 + Exp     Num ➝ Exp3 * Exp + Exp
3 * 7 + 21        Num ➝ Exp3 * 7 + Exp       Num ➝ Exp3 * Exp + Exp     Num ➝ ExpExp * Exp + Exp
3 * 7 + 21              Num ➝ Exp3 * 7 + Exp             Num ➝ Exp3 * Exp + Exp           Num ➝ ExpExp * Exp + Exp   Exp “...
3 * 7 + 21              Num ➝ Exp3 * 7 + Exp             Num ➝ Exp3 * Exp + Exp           Num ➝ ExpExp * Exp + Exp   Exp “...
THEY TURNED WORDS INTO      TREES
SENTENCES
STRUCTURE
Exp ➝ Exp “+” Exp   Exp “+” Exp ➝ ExpExp ➝ Exp “*” Exp   Exp “*” Exp ➝ ExpExp ➝ Num                 Num ➝ Expproductive   ...
Exp          Exp            ExpNum        Exp + Exp      Exp * Exp      tree construction
3 * 7 + 21
ExpNum   Exp   Exp   Exp      3 * 7 + 21
Exp         ExpExp + Exp    *       Exp   Exp   Exp            3 * 7 + 21
Exp  Exp         ExpExp + Exp   Exp   Exp   Exp            3 * 7 + 21
ONE FORMALISM
THREE READINGS
PURE
DECLARATIVE
BEAUTIFUL
PARADISE LOST
EFFICIENCY
THE FIRST PLAGUE WEREGRAMMAR CLASSES
context-free grammars
context-free grammars    LL(0)
context-free grammars    LL(1)    LL(0)
context-free grammars    LL(k)    LL(1)    LL(0)
context-free grammars    LL(k)    LL(1)    LL(0)        LR(0)
context-free grammars    LL(k)    LL(1)        LR(1)    LL(0)        LR(0)
context-free grammars    LL(k)        LR(k)    LL(1)        LR(1)    LL(0)        LR(0)
context-free grammars    LL(k)        LR(k)    LL(1)        LR(1)                  SLR    LL(0)        LR(0)
context-free grammars    LL(k)        LR(k)    LL(1)        LR(1)               LALR(1)                  SLR    LL(0)     ...
Exp “+” Exp ➝ Exp   Term (“+” Term)* ➝ ExpExp “*” Exp ➝ Exp    Fact (“*” Fact)* ➝ Term      Num ➝ Exp                  Num...
Exp                             *      Exp                         *  Exp               Term Term TermExp   Exp   Exp     ...
THE SECOND PLAGUE WASDISAMBIGUATION
Exp               Exp  Exp                     ExpExp   Exp   Exp   Exp   Exp   Exp3 * 7 + 21        3 * 7 + 21
precedence         operators               associativity    1                 ( ), [ ]            non-associative    2    ...
context-free grammars    LL(k)        LR(k)    LL(1)        LR(1)               LALR(1)                  SLR    LL(0)     ...
context-free grammars      unambigous    LL(k)          LR(k)    LL(1)          LR(1)               LALR(1)               ...
Exp “+” Term ➝ Exp          Term ➝ Exp Term “*” Fact ➝ Term          Fact ➝ Term          Num ➝ Factgrammar classes
FALSE PROPHETS
L = {ab, a}   L = {ab, a}“a” “b” ➝ A   “a” “b” /    “a” ➝ A   “a”     ➝Aparadise        PEGs
L = {ab, a}     L = {a}    “a” ➝ A   “a”     /“a” “b” ➝ A   “a” “b” ➝ Aparadise        PEGs
if c1 then if c2 then s1 else s2      dangling else
“if ” E “then” S “else” S /    “if ” E “then” S          /“if ” E “then” S          ➝S   “if ” E “then” S “else” S ➝ S    ...
THE THIRD PLAGUE WASLEXICAL SYNTAX
morphology & syntax
LL(k)    LR(k)      LL(1)    LR(1)              LALR(1)               SLR      LL(0)    LR(0)limited look-ahead
scanners
3 * 7 + 21
3 * 7 + 21
parsers
3 * 7 + 21
Exp  ExpExp   Exp   Exp3 * 7 + 21
x = 1. * .10y   :   array [ 1 .. 10 ] of integer
x = 1. * .10y   :   array [ 1 .. 10 ] of integer
x = 1. * .10y   :   array [ 1. .10 ]   of integer
THE FOURTH PLAGUE WASTREE CONSTRUCTION
Exp                            Add  Exp                              AddExp   Exp   Exp              Const Const Const3 + ...
Exp         *                       Add              *                AddTerm Term Term           Const Const ConstFact   ...
expr:  INTEGER       { $$ = con($1); }| expr + expr { $$ = opr(+, 2, $1, $3);}| expr * expr { $$ = opr(*, 2, $1, $3);};   ...
THE FIFTH PLAGUE WAS EVOLUTION
Exp “+” Term ➝ ExpExp “+” Exp ➝ Exp               Term ➝ ExpExp “*” Exp ➝ Exp      Term “*” Fact ➝ Term      Num ➝ Exp    ...
CExp “+” Term ➝ CExp                             Term ➝ CExpExp “+” Exp ➝ Exp      Term “*” Fact ➝ TermExp “*” Exp ➝ Exp  ...
THE SIXTH PLAGUE WASCOMPOSITION
parsers
context-free grammars    LL(k)        LR(k)    LL(1)        LR(1)               LALR(1)                  SLR    LL(0)     ...
context-free grammars    LL(k)        LR(k)    LL(1)        LR(1)               LALR(1)                  SLR    LL(0)     ...
context-free grammars    LL(k)        LR(k)    LL(1)        LR(1)               LALR(1)                  SLR    LL(0)     ...
context-free grammars    LL(k)        LR(k)    LL(1)        LR(1)               LALR(1)                  SLR    LL(0)     ...
scanners
public boolean  authenticate(String user, String pw) {    SQL stm = <| SELECT id FROM Users                 WHERE name = $...
THE SEVENTH PLAGUE WASRESTRICTION         TO    PARSERS
PRETTY PRINTERS
SENTENCEGENERATORS
AST ACCESS
IDE SUPPORT
PARADISEREGAINED
GENERALISEDPARSING
context-free grammars    LL(k)        LR(k)    LL(1)        LR(1)               LALR(1)                  SLR    LL(0)     ...
context-free grammars
context-free syntaxExp “+” Exp ➝ ExpExp “*” Exp ➝ Exp     Exp "+" Exp -> Exp      Num ➝ Exp       Exp "*" Exp -> Exp      ...
DECLARATIVEDISAMBIGUATION
context-free grammars      unambigous    LL(k)          LR(k)    LL(1)          LR(1)               LALR(1)               ...
context-free grammars      unambigous
context-free grammars
precedence         operators               associativity    1                 ( ), [ ]            non-associative    2    ...
context-free priorities  Exp "*" Exp -> Exp {left}> Exp "+" Exp -> Exp {left}
context-free syntax    	  "if" E "then" S          -> S {prefer}  "if" E "then" S "else" S -> S
SCANNERLESSPARSING
morphology & syntax
lexical syntax    	  [0-9]+ -> NUM  [ tn]         -> LAYOUT  "//" ~[n]* [n] -> LAYOUT
parser
parser
DECLARATIVETREE CONSTRUCTION
Exp                            Add  Exp                              AddExp   Exp   Exp              Const Const Const3 + ...
context-free syntax    	  Exp "+" Exp -> Exp {cons("Add")}  Exp "*" Exp -> Exp {cons("Mul")}  NUM         -> Exp {cons("Co...
Add          Add(                       Add(      Add                 Const("3"),                          Const("7")Const...
SEAMLESSEVOLUTION
context-free syntax    	  Exp "+" Exp -> Exp {cons("Add")}  Exp "*" Exp -> Exp {cons("Mul")}  NUM         -> Exp {cons("Co...
MODULARCOMPOSITION
context-free grammars    LL(k)        LR(k)    LL(1)        LR(1)               LALR(1)                  SLR    LL(0)     ...
context-free grammars
public boolean  authenticate(String user, String pw) {    SQL stm = <| SELECT id FROM Users                 WHERE name = $...
module Java-SQL  imports    Java SQL  exports    context-free syntax    "<|" Query "|>" -> Exp {cons("ToSQL")}    "${" Exp...
BEYONDPARSERS
PRETTY PRINTERS
SENTENCEGENERATORS
AST ACCESS
IDE SUPPORT
PARADISE DENIED
still around
still around
still have to use it
still have to learnLL, LR, SLR, LALR
still think usingparser generators is hard
modern parser generator
modern parser generator
PARADISE OPEN
coffee break               Pretty Printing 181
IIpretty-printing                  Pretty Printing 182
Recap: Parsing & AST constructionlet      function fact(n : int): int = if n < 1 then 1 else n * fact(n - 1)in      printi...
Pretty Printingfrom ASTs to text   •   keywords   •   layout: spaces, line breaks, indentationspecification   •   partiall...
Box Layoutbasic boxes                 “foo”              KW [ “foo” ]                  _1                             Pret...
Box Layouthorizontal boxes            H hs=x [ B     B       B ]                   B   B       B                          ...
Box Layoutvertical boxes          V vs=y is=i [ B            hs=x            B   B ]                    B                 ...
Pretty Print Tablesgeneration"if" Exp "then" Exp              -> Exp {"IfThen"}"let" Dec* "in" {Exp ";"}* "end" -> Exp {"L...
Pretty Print Tablesmanual override"if" Exp "then" Exp              -> Exp {"IfThen"}"let" Dec* "in" {Exp ";"}* "end" -> Ex...
IIIsyntax templates                   Pretty Printing 190
Recap: SDFsorts Exp	context-free syntax    LValue ":=" Exp                        -> Exp	    	    "let" Dec* "in" {Exp ";"...
Pretty Print Tablesmanual overridesExp.Let -- V vs=1 is=0 [             V vs=1 is=2 [KW["let"] _1]             V vs=1 is=2...
Recap: Code Completionmodule Tiger-Completionsimports Tiger-Completions.generatedcompletions    completion keyword   : "fu...
Templatesexplicit layout in syntax definitionscontext-free syntax   Exp "+"   Exp -> Exp {left, "Plus"}   Exp "-"   Exp ->...
Templatesexplicit layout in syntax definitionscontext-free syntax   "let" Dec* "in" {Exp ";"}* "end" "   Exp {"Let"}contex...
IVsummary          Pretty Printing 196
Summarylessons learned                  Pretty Printing 197
Summarylessons learnedWhy are efficient parsing algorithms problematic?  •   not longer pure, declarative, beautiful  •   ...
Summarylessons learnedWhy are efficient parsing algorithms problematic?  •   not longer pure, declarative, beautiful  •   ...
Summarylessons learnedWhy are efficient parsing algorithms problematic?  •   not longer pure, declarative, beautiful  •   ...
Literaturelearn more             Pretty Printing 198
Literaturelearn moredeclarative syntax definition    Lennart C. L. Kats, Eelco Visser, Guido Wachsmuth: Pure and Declarati...
Literaturelearn moredeclarative syntax definition    Lennart C. L. Kats, Eelco Visser, Guido Wachsmuth: Pure and Declarati...
Literaturelearn moredeclarative syntax definition    Lennart C. L. Kats, Eelco Visser, Guido Wachsmuth: Pure and Declarati...
Outlookbeyond IN4303Model-driven Software Development  •   domain-specific languages  •   build your own language with Spo...
Outlookcoming nextdeclarative semantics definition  •    Lecture 4: Term Rewriting  •    Lecture 5: Static Analysis and Er...
copyrights             Pretty Printing 201
Pretty Printing 202
Picturesattribution & copyrightsSlide 1:   Latin Bible by Gerard Brils (photo by Adrian Pingstone), public domainSlide 188...
Declarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty Printing
Upcoming SlideShare
Loading in …5
×

Declarative Syntax Definition - Pretty Printing

1,130 views

Published on

Presentation slides for lecture 5 of course IN4303 on Compiler Construction at TU Delft.

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,130
On SlideShare
0
From Embeds
0
Number of Embeds
146
Actions
Shares
0
Downloads
31
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • My name is Guido Wachsmuth, \n
  • and I will tell you a sad story.\n
  • A story about the paradise of pure and declarative syntax definition,\n
  • about how this paradise once was lost,\n
  • and about how it was regained later,\n
  • but also about how this paradise is denied until these days. \n
  • In the beginning\n
  • were the words, and the words\n
  • were trees, and the trees were words. \n
  • All words were made through grammars, and without grammars was not any word made that was made. \n
  • Those were the days of the garden of Eden. \nAnd there where language engineers strolling through the garden. \n
  • They made languages which were sets of words by making grammars full of beauty. \n
  • And with these grammars, they turned words into trees and trees into words. \n
  • And the trees were natural, \n
  • and pure, \n
  • and beautiful, as were the grammars.\n
  • Among them were software engineers who made software as the language engineers made languages. And they dwelt with them and they were one people. The language engineers were software engineers and the software engineers were language engineers. \n
  • And the language engineers made language software. They made recognisers to know words, and generators to make words, and parsers to turn words into trees, and formatters to turn trees into words.\n
  • But the software they made was not as natural, \n
  • and pure, \n
  • and beautiful as the grammars they made. \n
  • So they made software to make language software and began to make language software by making syntax definitions. And the syntax definitions were grammars and grammars were syntax definitions. With their software, they turned syntax definitions into language software. And the syntax definitions were language software and language software were syntax definitions.\n
  • And the syntax definitions were natural, \n
  • and pure, \n
  • and beautiful, as were the grammars.\n
  • Now the serpent was more crafty than any other beast of the field. He said to the language engineers\n\nDid you actually decide not to build any parsers?\n\nAnd the language engineers said to the serpent,\n\nWe build parsers, but we decided not to build others than general parsers, nor shall we try it, lest we lose our syntax definitions to be natural, and pure, and beautiful.\n\nBut the serpent said to the language engineers,\n\nYou will not surely lose your syntax definitions to be natural, and pure, and beautiful. For you know that when you build particular parsers your benchmarks will be improved, and your parsers will be the best, running fast and efficient.\n
  • So when the language engineers saw that restricted parsers were good for efficiency, and that they were a delight to the benchmarks, they made software to make efficient parsers and began to make efficient parsers by making parser definitions.\n
  • Those days, the language engineers went out from the garden of Eden. \n
  • In pain they made parser definitions all the days of their life. But the parser definitions were not grammars and grammars were not parser definitions.\n
  • And by the sweat of their faces they turned parser definitions into efficient parsers. \n
  • But the parser definitions were not natural, \n
  • nor pure, \n
  • nor beautiful, as the grammars had been before.\n
  • Their software was full of plagues. \n
  • The first plague were grammar classes. Only few grammars could be turned directly into parser definitions. And language engineers massaged their grammars all the days of their life to make them fit into a grammar class. And the parser definitions became unnatural, and impure, and ugly. And there was weeping and mourning.\n
  • The second plague was disambiguation. Their new parsers were deterministic. So the language engineers encoded precedence in parser definitions. And the parser definitions became unnatural, and impure, and ugly.\n\n
  • The third plague was lexical syntax. The new software could not handle lexical syntax definitions. So the language engineers made another software to turn lexical syntax definitions into scanners. But lexical syntax definitions were less expressive than the grammars they used before. And they were separated from parser definitions, as were scan- ners from parsers. And there was weeping and wailing.\n\n
  • The fourth plague was tree construction. The language engineers wanted the efficient parsers to turn words into trees, as their old parsers did. So they added code to their parser definitions. And the parser definitions became unnatural, and impure, and ugly. And those who were oblivious to the working of the efficient parsers made parsers that turn the right words into the wrong trees.\n\n
  • The fifth and sixth plague were evolution\n
  • and composition. Once the language engineers added a new rule to their parser definitions, those tended to break. And they massaged them by the sweat of their faces to make them fit again into the grammar class. And they were not able to compose two parser definitions to a single parser definition because of grammar classes and separate scanners. And there was weeping and groaning.\n
  • The seventh plague was the restriction to parsers. The language engineers turned parser definitions into recognizers and into parsers. But they could not turn them into generators or formatters. That was because parser definitions were not grammars.\n
  • Many have undertaken to compile a narrative of the things that have been accomplished among us. It seemed good to us also, having followed all things closely for some time past, to tell an orderly account for you that you may have certainty concerning the things you have been taught.\n
  • It all starts with the beauty of grammars and trees\n
  • and it all starts with Noam Chomsky.\n\nLike other linguists, he was thinking about the infinite productivity of language.\nWe can produce sentences that no-one produced before.\n\n
  • But how can we put this productivity into finite models?\nChomsky&amp;#x2019;s came up with grammars as generative devices.\nFinite models, declaring how words are made from letters and sentences are made from words.\n\n
  • This is a grammar for generating binary numbers.\n
  • It consists of 4 production rules\n
  • At the RHS of these rules we see terminal symbols.\nThese are the symbols binary numbers are made of.\n
  • At the LHS of each rule we see a nonterminal symbol.\nNonterminal symbols are variables in the generation.\nThey can also occur on the right-hand side of a production rule.\nWe say: The LHS symbol produces the symbols on the RHS.\n
  • The start symbol is a particular nonterminal symbol.\n
  • When we generate words through grammars,\n
  • we start with this start symbol.\n
  • and apply production rules by replacing nonterminal symbols.\nWe replace a nonterminal symbol from the LHS of a rule with the symbols of the RHS.\n
  • We do this as long as there are nonterminal symbols for which we can find production rules.\nWe are free to choose any nonterminal symbol and apply any rule.\nSo let&amp;#x2019;s take another rule for NUM this time.\n
  • Now we can replace one of the both symbols for digits.\nLet&amp;#x2019;s apply the first rule.\n
  • There is still one nonterminal symbol left.\nLet&amp;#x2019;s take the other rule for digits this time.\nWe are done. \nWe generated the word 10.\n
  • But with grammars we can not only generate words but also sentences.\n
  • Here is a grammar for generating expressions.\nAgain we have production rules\n
  • with terminal symbols occuring on the RHS of these rules.\nHere, we have two different kinds of terminal symbols:\n+ and * each representing a particular morphem,\nand NUM, representing a whole class of morphems, let&amp;#x2019;s assume in this case decimal numbers.\n
  • We have only a single nonterminal symbol\n
  • which is also the start symbol.\n
  • We can now generate an expression.\n
  • We replace the start symbol by applying the first rule for EXP.\n
  • Now let&amp;#x2019;s replace the first EXP and let&amp;#x2019;s use the second rule this time.\n
  • Let&amp;#x2019;s take again the first EXP and replace it by a morphem from the morphem class NUM.\n
  • When we do this two more times\n
  • we have generated an expression.\n
  • We can use grammars to describe languages by making a grammar that generates all the words of a given language.\n
  • For example, we can come up with a grammar for Latin.\n
  • Which should generate all these Latin sentences.\nAnd all other correct Latin sentences.\n
  • But a grammar can also define a language. \nIt defines the language of sentences that it generates.\nAnd we can make new languages my making grammars.\nAnd we can ask the grammar if a sentence is in the language or not.\nThe grammar is the only truth.\n
  • For this, we read the production rules of a grammar as reduction rules.\nThe symbols of the RHS can be reduced to the nonterminal symbol of the LHS.\nTo emphasise this reading, we can write the rules in a reductive way, switching LHS and RHS.\n
  • Now let&amp;#x2019;s see if this is a valid expression according to our grammar we&amp;#x2019;ve seen before.\n
  • We apply reduction rules where ever we can, finding the LHS of a rule and replacing it with the nonterminal symbol from the RHS.\nHere we can reduce 21 to EXP.\n
  • The same for 7\n
  • and 3.\n
  • Now we can replace EXP*EXP by EXP\n
  • and finally EXP+EXP by EXP.\nIt remains the start symbol of our grammar, telling us the truth:\n3*7+21 is a valid expression.\n
  • We can use grammars also to turn words into trees.\n
  • We do so because we are not only interested if a sentence is an element of a language,\n
  • but also its structure. \nAs we know from primary school, \nthis structure can typically be represented as trees.\n
  • we saw already two readings of grammars\n
  • now, here we have a third one: rules as tree construction rules. \nnonterminal symbol as the root, symbols constituting the root as leaves\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • non-intuitive classification\nnon-intuitive error messages\ncomplex parsing algorithms\n
  • \n
  • \n
  • \n
  • declarative disambiguation\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • interleaved with evaluation of the parser\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • not closed under composition\n
  • not closed under composition\n
  • not closed under composition\n
  • \n
  • overlapping morphem classes\nglobally reserved words\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • what did before the scanner, now does the parser\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • closed under composition\n
  • closed under composition\n
  • closed under composition\n
  • closed under composition\n
  • closed under composition\n
  • closed under composition\n
  • closed under composition\n
  • closed under composition\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Fear not! Stand firm! See the naturalness, and the pureness, and the beauty of declarative syntax definitions, which will work for you. \nFor the parser definitions that you see today, you shall never see again.\n
  • Go out to the promised land! \nMake new software to make parsers and begin to make parsers by making syntax definitions again. \nLet the syntax definitions be grammars again and grammars be syntax definitions again. \nAnd the syntax definitions will be natural, and pure, and beautiful again, as will the grammars.\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • WebDSL &amp; mobl: Flash to HTML5, query optimisation, data synchronisation, partitioning, e-learning platform\nSpoofax: debugging, integration, testing, visualisation, incremental compilation, DSLs for robotics\n
  • \n
  • round-up on every lecture\n\nwhat to take with you\n\ncheck yourself, pre- and post-paration\n
  • \n
  • \n
  • Declarative Syntax Definition - Pretty Printing

    1. 1. Declarative Syntax Definitionpretty printingGuido Wachsmuth Delft Course IN4303 2012/13 University of Technology Compiler Construction Challenge the future
    2. 2. Assessmentlast lecture Pretty Printing 2
    3. 3. Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars. Pretty Printing 2
    4. 4. Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.Discuss additional description means provided by SDF and whythey are needed in compiler construction. Pretty Printing 2
    5. 5. Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.Discuss additional description means provided by SDF and whythey are needed in compiler construction. • regular expressions Pretty Printing 2
    6. 6. Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.Discuss additional description means provided by SDF and whythey are needed in compiler construction. • regular expressions • layout Pretty Printing 2
    7. 7. Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.Discuss additional description means provided by SDF and whythey are needed in compiler construction. • regular expressions • layout • AST constructors Pretty Printing 2
    8. 8. Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.Discuss additional description means provided by SDF and whythey are needed in compiler construction. • regular expressions • layout • AST constructors • follow restrictions Pretty Printing 2
    9. 9. Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.Discuss additional description means provided by SDF and whythey are needed in compiler construction. • regular expressions • layout • AST constructors • follow restrictions • annotations for associativity Pretty Printing 2
    10. 10. Assessmentlast lectureCompare SDF with the plain formalism of context-free grammars.Discuss additional description means provided by SDF and whythey are needed in compiler construction. • regular expressions • layout • AST constructors • follow restrictions • annotations for associativity • priorities Pretty Printing 2
    11. 11. Overviewtoday’s lecture Lexical Analysis 3
    12. 12. Overviewtoday’s lectureplagues of traditional parsing algorithms • paradise lost • paradise regained Lexical Analysis 3
    13. 13. Overviewtoday’s lectureplagues of traditional parsing algorithms • paradise lost • paradise regainedpretty-printing • from trees to text • box layout in pretty-print tables Lexical Analysis 3
    14. 14. Overviewtoday’s lectureplagues of traditional parsing algorithms • paradise lost • paradise regainedpretty-printing • from trees to text • box layout in pretty-print tablestemplate language • alternative to SDF • generation of pretty-printing strategies • generation of completion templates Lexical Analysis 3
    15. 15. Iparadise lost & regained Pretty Printing 4
    16. 16. Pure and Declarative Syntax Definition: Paradise Lost and RegainedLennart Kats Eelco Visser Guido Wachsmuth Delft University of Technology
    17. 17. PARADISE
    18. 18. PARADISE LOST
    19. 19. PARADISEREGAINED
    20. 20. PARADISE DENIED
    21. 21. PARADISE
    22. 22. WORDS
    23. 23. TREES
    24. 24. GRAMMARS
    25. 25. LANGUAGEENGINEERS
    26. 26. LANGUAGES
    27. 27. GRAMMARS
    28. 28. NATURAL
    29. 29. PURE
    30. 30. BEAUTIFUL
    31. 31. SOFTWAREENGINEERS
    32. 32. LANGUAGESOFTWARE
    33. 33. NOTNATURAL
    34. 34. NOTPURE
    35. 35. NOTBEAUTIFUL
    36. 36. SYNTAXDEFINITIONS
    37. 37. NATURAL
    38. 38. PURE
    39. 39. BEAUTIFUL
    40. 40. THE FALL
    41. 41. PARSERDEFINITIONS
    42. 42. PARADISE LOST
    43. 43. PAIN
    44. 44. SWEAT
    45. 45. NOTNATURAL
    46. 46. NOTPURE
    47. 47. NOTBEAUTIFUL
    48. 48. THE PLAGUES
    49. 49. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)GRAMMAR CLASSES
    50. 50. DISAMBIGUATION
    51. 51. LEXICAL SYNTAX
    52. 52. TREECONSTRUCTION
    53. 53. EVOLUTION
    54. 54. COMPOSITION
    55. 55. RESTRICTION TO PARSERS
    56. 56. PARADISE
    57. 57. WORDS WERE MADE THROUGH GRAMMARS
    58. 58. GRAMMARS
    59. 59. Num ➝ Digit NumNum ➝ DigitDigit ➝ “0”Digit ➝ “1”
    60. 60. Num ➝ Digit NumNum ➝ DigitDigit ➝ “0”Digit ➝ “1” production rules
    61. 61. Num ➝ Digit NumNum ➝ DigitDigit ➝ “0”Digit ➝ “1” terminal symbols production rules
    62. 62. Num ➝ Digit NumNum ➝ DigitDigit ➝ “0”Digit ➝ “1” nonterminal symbols terminal symbols production rules
    63. 63. Num ➝ Digit NumNum ➝ DigitDigit ➝ “0”Digit ➝ “1” nonterminal symbols terminal symbols production rules start symbol
    64. 64. WORDS
    65. 65. Num
    66. 66. Num Num ➝ Digit NumDigit Num
    67. 67. Num Num ➝ Digit NumDigit Num Num ➝ DigitDigit Digit
    68. 68. Num Num ➝ Digit NumDigit Num Num ➝ DigitDigit Digit Digit ➝ “0”Digit 0
    69. 69. Num Num ➝ Digit NumDigit Num Num ➝ DigitDigit Digit Digit ➝ “0”Digit 0 Digit ➝ “1”10
    70. 70. SENTENCES
    71. 71. Exp ➝ Exp “+” ExpExp ➝ Exp “*” ExpExp ➝ Num production rules
    72. 72. Exp ➝ Exp “+” ExpExp ➝ Exp “*” ExpExp ➝ Num terminal symbols production rules
    73. 73. Exp ➝ Exp “+” ExpExp ➝ Exp “*” ExpExp ➝ Num nonterminal symbols terminal symbols production rules
    74. 74. Exp ➝ Exp “+” ExpExp ➝ Exp “*” ExpExp ➝ Num nonterminal symbols terminal symbols production rules start symbol
    75. 75. Exp
    76. 76. Exp Exp ➝ Exp “+” ExpExp + Exp
    77. 77. Exp Exp ➝ Exp “+” ExpExp + Exp Exp ➝ Exp “*” ExpExp * Exp + Exp
    78. 78. Exp Exp ➝ Exp “+” ExpExp + Exp Exp ➝ Exp “*” ExpExp * Exp + Exp Exp ➝ Num3 * Exp + Exp
    79. 79. Exp Exp ➝ Exp “+” ExpExp + Exp Exp ➝ Exp “*” ExpExp * Exp + Exp Exp ➝ Num3 * Exp + Exp Exp ➝ Num3 * 7 + Exp
    80. 80. Exp Exp ➝ Exp “+” ExpExp + Exp Exp ➝ Exp “*” ExpExp * Exp + Exp Exp ➝ Num3 * Exp + Exp Exp ➝ Num3 * 7 + Exp Exp ➝ Num3 * 7 + 21
    81. 81. THEY MADE LANGUAGES BY MAKING GRAMMARS
    82. 82. GRAMMAR
    83. 83. LANGUAGE
    84. 84. TRUTH
    85. 85. Exp ➝ Exp “+” Exp Exp “+” Exp ➝ ExpExp ➝ Exp “*” Exp Exp “*” Exp ➝ ExpExp ➝ Num Num ➝ Expproductive reductive
    86. 86. 3 * 7 + 21
    87. 87. 3 * 7 + 21 Num ➝ Exp3 * 7 + Exp
    88. 88. 3 * 7 + 21 Num ➝ Exp3 * 7 + Exp Num ➝ Exp3 * Exp + Exp
    89. 89. 3 * 7 + 21 Num ➝ Exp3 * 7 + Exp Num ➝ Exp3 * Exp + Exp Num ➝ ExpExp * Exp + Exp
    90. 90. 3 * 7 + 21 Num ➝ Exp3 * 7 + Exp Num ➝ Exp3 * Exp + Exp Num ➝ ExpExp * Exp + Exp Exp “*” Exp ➝ ExpExp + Exp
    91. 91. 3 * 7 + 21 Num ➝ Exp3 * 7 + Exp Num ➝ Exp3 * Exp + Exp Num ➝ ExpExp * Exp + Exp Exp “*” Exp ➝ ExpExp + Exp Exp “+” Exp ➝ ExpExp
    92. 92. THEY TURNED WORDS INTO TREES
    93. 93. SENTENCES
    94. 94. STRUCTURE
    95. 95. Exp ➝ Exp “+” Exp Exp “+” Exp ➝ ExpExp ➝ Exp “*” Exp Exp “*” Exp ➝ ExpExp ➝ Num Num ➝ Expproductive reductive
    96. 96. Exp Exp ExpNum Exp + Exp Exp * Exp tree construction
    97. 97. 3 * 7 + 21
    98. 98. ExpNum Exp Exp Exp 3 * 7 + 21
    99. 99. Exp ExpExp + Exp * Exp Exp Exp 3 * 7 + 21
    100. 100. Exp Exp ExpExp + Exp Exp Exp Exp 3 * 7 + 21
    101. 101. ONE FORMALISM
    102. 102. THREE READINGS
    103. 103. PURE
    104. 104. DECLARATIVE
    105. 105. BEAUTIFUL
    106. 106. PARADISE LOST
    107. 107. EFFICIENCY
    108. 108. THE FIRST PLAGUE WEREGRAMMAR CLASSES
    109. 109. context-free grammars
    110. 110. context-free grammars LL(0)
    111. 111. context-free grammars LL(1) LL(0)
    112. 112. context-free grammars LL(k) LL(1) LL(0)
    113. 113. context-free grammars LL(k) LL(1) LL(0) LR(0)
    114. 114. context-free grammars LL(k) LL(1) LR(1) LL(0) LR(0)
    115. 115. context-free grammars LL(k) LR(k) LL(1) LR(1) LL(0) LR(0)
    116. 116. context-free grammars LL(k) LR(k) LL(1) LR(1) SLR LL(0) LR(0)
    117. 117. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
    118. 118. Exp “+” Exp ➝ Exp Term (“+” Term)* ➝ ExpExp “*” Exp ➝ Exp Fact (“*” Fact)* ➝ Term Num ➝ Exp Num ➝ Fact paradise grammar classes
    119. 119. Exp * Exp * Exp Term Term TermExp Exp Exp Fact Fact Fact3 + 7 + 21 3 + 7 + 21 paradise grammar classes
    120. 120. THE SECOND PLAGUE WASDISAMBIGUATION
    121. 121. Exp Exp Exp ExpExp Exp Exp Exp Exp Exp3 * 7 + 21 3 * 7 + 21
    122. 122. precedence operators associativity 1 ( ), [ ] non-associative 2 new non-associative 3 . left-associative 4 ++, -- non-associative 5 -, +, !, ~, ++, --, (type) right-associative 6 *, /, % left-associative 7 +, - left-associative … … … text books
    123. 123. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
    124. 124. context-free grammars unambigous LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
    125. 125. Exp “+” Term ➝ Exp Term ➝ Exp Term “*” Fact ➝ Term Fact ➝ Term Num ➝ Factgrammar classes
    126. 126. FALSE PROPHETS
    127. 127. L = {ab, a} L = {ab, a}“a” “b” ➝ A “a” “b” / “a” ➝ A “a” ➝Aparadise PEGs
    128. 128. L = {ab, a} L = {a} “a” ➝ A “a” /“a” “b” ➝ A “a” “b” ➝ Aparadise PEGs
    129. 129. if c1 then if c2 then s1 else s2 dangling else
    130. 130. “if ” E “then” S “else” S / “if ” E “then” S /“if ” E “then” S ➝S “if ” E “then” S “else” S ➝ S PEGs
    131. 131. THE THIRD PLAGUE WASLEXICAL SYNTAX
    132. 132. morphology & syntax
    133. 133. LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)limited look-ahead
    134. 134. scanners
    135. 135. 3 * 7 + 21
    136. 136. 3 * 7 + 21
    137. 137. parsers
    138. 138. 3 * 7 + 21
    139. 139. Exp ExpExp Exp Exp3 * 7 + 21
    140. 140. x = 1. * .10y : array [ 1 .. 10 ] of integer
    141. 141. x = 1. * .10y : array [ 1 .. 10 ] of integer
    142. 142. x = 1. * .10y : array [ 1. .10 ] of integer
    143. 143. THE FOURTH PLAGUE WASTREE CONSTRUCTION
    144. 144. Exp Add Exp AddExp Exp Exp Const Const Const3 + 7 + 21 3 7 21 paradise
    145. 145. Exp * Add * AddTerm Term Term Const Const ConstFact Fact Fact 3 7 21 3 + 7 + 21 grammar classes
    146. 146. expr: INTEGER { $$ = con($1); }| expr + expr { $$ = opr(+, 2, $1, $3);}| expr * expr { $$ = opr(*, 2, $1, $3);}; semantic actions
    147. 147. THE FIFTH PLAGUE WAS EVOLUTION
    148. 148. Exp “+” Term ➝ ExpExp “+” Exp ➝ Exp Term ➝ ExpExp “*” Exp ➝ Exp Term “*” Fact ➝ Term Num ➝ Exp Fact ➝ Term Num ➝ Fact paradise grammar classes
    149. 149. CExp “+” Term ➝ CExp Term ➝ CExpExp “+” Exp ➝ Exp Term “*” Fact ➝ TermExp “*” Exp ➝ Exp Fact ➝ Term Num ➝ Exp Num ➝ FactExp “=” Exp ➝ Exp Exp “=” CExp ➝ ExpExp “<” Exp ➝ Exp Exp “<” CExp ➝ ExpExp “>” Exp ➝ Exp Exp “>” CExp ➝ Exp CExp ➝ Exp paradise grammar classes
    150. 150. THE SIXTH PLAGUE WASCOMPOSITION
    151. 151. parsers
    152. 152. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
    153. 153. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
    154. 154. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
    155. 155. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
    156. 156. scanners
    157. 157. public boolean authenticate(String user, String pw) { SQL stm = <| SELECT id FROM Users WHERE name = ${user} AND password = ${pw} |>; return executeQuery(stm).size() != 0;}
    158. 158. THE SEVENTH PLAGUE WASRESTRICTION TO PARSERS
    159. 159. PRETTY PRINTERS
    160. 160. SENTENCEGENERATORS
    161. 161. AST ACCESS
    162. 162. IDE SUPPORT
    163. 163. PARADISEREGAINED
    164. 164. GENERALISEDPARSING
    165. 165. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
    166. 166. context-free grammars
    167. 167. context-free syntaxExp “+” Exp ➝ ExpExp “*” Exp ➝ Exp Exp "+" Exp -> Exp Num ➝ Exp Exp "*" Exp -> Exp NUM -> Exp paradise SDF
    168. 168. DECLARATIVEDISAMBIGUATION
    169. 169. context-free grammars unambigous LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
    170. 170. context-free grammars unambigous
    171. 171. context-free grammars
    172. 172. precedence operators associativity 1 ( ), [ ] non-associative 2 new non-associative 3 . left-associative 4 ++, -- non-associative 5 -, +, !, ~, ++, --, (type) right-associative 6 *, /, % left-associative 7 +, - left-associative … … … text books
    173. 173. context-free priorities Exp "*" Exp -> Exp {left}> Exp "+" Exp -> Exp {left}
    174. 174. context-free syntax "if" E "then" S -> S {prefer} "if" E "then" S "else" S -> S
    175. 175. SCANNERLESSPARSING
    176. 176. morphology & syntax
    177. 177. lexical syntax [0-9]+ -> NUM [ tn] -> LAYOUT "//" ~[n]* [n] -> LAYOUT
    178. 178. parser
    179. 179. parser
    180. 180. DECLARATIVETREE CONSTRUCTION
    181. 181. Exp Add Exp AddExp Exp Exp Const Const Const3 + 7 + 21 3 7 21 paradise
    182. 182. context-free syntax Exp "+" Exp -> Exp {cons("Add")} Exp "*" Exp -> Exp {cons("Mul")} NUM -> Exp {cons("Const")} SDF
    183. 183. Add Add( Add( Add Const("3"), Const("7")Const Const Const ), Const("21") 3 7 21 ) paradise SDF
    184. 184. SEAMLESSEVOLUTION
    185. 185. context-free syntax Exp "+" Exp -> Exp {cons("Add")} Exp "*" Exp -> Exp {cons("Mul")} NUM -> Exp {cons("Const")} Exp "=" Exp -> Exp {cons("Eq")} Exp ">" Exp -> Exp {cons("Gt")} Exp "<" Exp -> Exp {cons("Lt")}
    186. 186. MODULARCOMPOSITION
    187. 187. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
    188. 188. context-free grammars
    189. 189. public boolean authenticate(String user, String pw) { SQL stm = <| SELECT id FROM Users WHERE name = ${user} AND password = ${pw} |>; return executeQuery(stm).size() != 0;}
    190. 190. module Java-SQL imports Java SQL exports context-free syntax "<|" Query "|>" -> Exp {cons("ToSQL")} "${" Exp "}" -> SqlExp {cons("FromSQL")}
    191. 191. BEYONDPARSERS
    192. 192. PRETTY PRINTERS
    193. 193. SENTENCEGENERATORS
    194. 194. AST ACCESS
    195. 195. IDE SUPPORT
    196. 196. PARADISE DENIED
    197. 197. still around
    198. 198. still around
    199. 199. still have to use it
    200. 200. still have to learnLL, LR, SLR, LALR
    201. 201. still think usingparser generators is hard
    202. 202. modern parser generator
    203. 203. modern parser generator
    204. 204. PARADISE OPEN
    205. 205. coffee break Pretty Printing 181
    206. 206. IIpretty-printing Pretty Printing 182
    207. 207. Recap: Parsing & AST constructionlet function fact(n : int): int = if n < 1 then 1 else n * fact(n - 1)in printint(fact(10))endLet( [ FunDec( "fact" , [FArg("n", Tid("int"))] , Tid("int") , IfThenElse( Lt(Var("n"), Int("1")) , Int("1") , Times(Var("n"), Call("fact", [Minus(Var("n"), Int("1"))])) ) ) ], [Call("printint", [Call("fact", [Int("10")])])]) Pretty Printing 183
    208. 208. Pretty Printingfrom ASTs to text • keywords • layout: spaces, line breaks, indentationspecification • partially defined in grammar • missing layout Pretty Printing 184
    209. 209. Box Layoutbasic boxes “foo” KW [ “foo” ] _1 Pretty Printing 185
    210. 210. Box Layouthorizontal boxes H hs=x [ B B B ] B B B Pretty Printing 186
    211. 211. Box Layoutvertical boxes V vs=y is=i [ B hs=x B B ] B B B Pretty Printing 187
    212. 212. Pretty Print Tablesgeneration"if" Exp "then" Exp -> Exp {"IfThen"}"let" Dec* "in" {Exp ";"}* "end" -> Exp {"Let"}Exp.IfThen -- KW["if"] _1 KW["then"] _2,Exp.Let -- KW["let"] _1 KW["in"] _2 KW["end"],Exp.Let.1:iter-star -- _1,Exp.Let.2:iter-star-sep -- _1 KW[";"] Pretty Printing 188
    213. 213. Pretty Print Tablesmanual override"if" Exp "then" Exp -> Exp {"IfThen"}"let" Dec* "in" {Exp ";"}* "end" -> Exp {"Let"}Exp.Let -- V vs=1 is=0 [ V vs=1 is=2 [KW["let"] _1] V vs=1 is=2 [KW["in"] _2] KW["end"] ] Pretty Printing 189
    214. 214. IIIsyntax templates Pretty Printing 190
    215. 215. Recap: SDFsorts Exp context-free syntax LValue ":=" Exp -> Exp "let" Dec* "in" {Exp ";"}* "end" -> Exp "if" Exp "then" Exp -> Exp "if" Exp "then" Exp "else" Exp -> Exp "while" Exp "do" Exp -> Exp "for" Id ":=" Exp "to" Exp "do" Exp -> Exp "break" -> Exp "(" ")" -> Exp "(" Exp ")" -> Exp "(" {Exp ";"}+ ")" -> Exp Pretty Printing 191
    216. 216. Pretty Print Tablesmanual overridesExp.Let -- V vs=1 is=0 [ V vs=1 is=2 [KW["let"] _1] V vs=1 is=2 [KW["in"] _2] KW["end"] ] Pretty Printing 192
    217. 217. Recap: Code Completionmodule Tiger-Completionsimports Tiger-Completions.generatedcompletions completion keyword : "function" completion template Exp: "if " <e> " thennt" <s> (blank) completion proposer : editor-complete Pretty Printing 193
    218. 218. Templatesexplicit layout in syntax definitionscontext-free syntax Exp "+" Exp -> Exp {left, "Plus"} Exp "-" Exp -> Exp {left, "Minus"}context-free syntax Exp.Plus = <<Exp> + <Exp>> {left} Exp.Minus = <<Exp> - <Exp>> {left} Pretty Printing 194
    219. 219. Templatesexplicit layout in syntax definitionscontext-free syntax "let" Dec* "in" {Exp ";"}* "end" " Exp {"Let"}context-free syntax Exp.Let = < let <Dec*; separator="n"> in <Exp*; separator=";n"> end> Pretty Printing 195
    220. 220. IVsummary Pretty Printing 196
    221. 221. Summarylessons learned Pretty Printing 197
    222. 222. Summarylessons learnedWhy are efficient parsing algorithms problematic? • not longer pure, declarative, beautiful • paradise lost: seven plagues • paradise regained: scannerless generalised parsing Pretty Printing 197
    223. 223. Summarylessons learnedWhy are efficient parsing algorithms problematic? • not longer pure, declarative, beautiful • paradise lost: seven plagues • paradise regained: scannerless generalised parsingHow can we specify pretty-printers? • generated from SDF • manual overrides • one stop shop: TemplateLanguage Pretty Printing 197
    224. 224. Summarylessons learnedWhy are efficient parsing algorithms problematic? • not longer pure, declarative, beautiful • paradise lost: seven plagues • paradise regained: scannerless generalised parsingHow can we specify pretty-printers? • generated from SDF • manual overrides • one stop shop: TemplateLanguage Pretty Printing 197
    225. 225. Literaturelearn more Pretty Printing 198
    226. 226. Literaturelearn moredeclarative syntax definition Lennart C. L. Kats, Eelco Visser, Guido Wachsmuth: Pure and Declarative Syntax Definition - Paradise Lost and Regained. SPLASH 2010 Pretty Printing 198
    227. 227. Literaturelearn moredeclarative syntax definition Lennart C. L. Kats, Eelco Visser, Guido Wachsmuth: Pure and Declarative Syntax Definition - Paradise Lost and Regained. SPLASH 2010pretty-printing Marc G.J. van den Brand, Eelco Visser: Generation of Formatters for Context- Free Languages. ACM TOSEM 5(1):1-41, 1996. Merijn de Jonge. Pretty-Printing for Software Reengineering. ICSM 2002 Pretty Printing 198
    228. 228. Literaturelearn moredeclarative syntax definition Lennart C. L. Kats, Eelco Visser, Guido Wachsmuth: Pure and Declarative Syntax Definition - Paradise Lost and Regained. SPLASH 2010pretty-printing Marc G.J. van den Brand, Eelco Visser: Generation of Formatters for Context- Free Languages. ACM TOSEM 5(1):1-41, 1996. Merijn de Jonge. Pretty-Printing for Software Reengineering. ICSM 2002template language Tobi Vollebregt, Lennart C. L. Kats, Eelco Visser. Declarative Specification of Template-Based Textual Editors. LDTA 2012 Pretty Printing 198
    229. 229. Outlookbeyond IN4303Model-driven Software Development • domain-specific languages • build your own language with SpoofaxSeminar Metaprogramming • science behind the scenesMaster theses • theory & practice • WebDSL & mobl • Spoofax Pretty Printing 199
    230. 230. Outlookcoming nextdeclarative semantics definition • Lecture 4: Term Rewriting • Lecture 5: Static Analysis and Error Checking • Lecture 6: Code GenerationLabs • Sep 27 syntax definition • Oct 4 code completion & pretty printing Pretty Printing 200
    231. 231. copyrights Pretty Printing 201
    232. 232. Pretty Printing 202
    233. 233. Picturesattribution & copyrightsSlide 1: Latin Bible by Gerard Brils (photo by Adrian Pingstone), public domainSlide 188: West Cornwall Pasty Co. by Dominica Williamson, some rights reservedSlide 199-200: Ostsee by Mario Thiel, some rights reservedSlides 5-180: Pure and Declarative Syntax Definition: Paradise Lost and Regained, some rights reserved Pretty Printing 203

    ×