May 21, 2002 The metafront Tool AoPL, S'02 Language Transformation: The metafront Tool Claus Brabrand Michael I. Schwartzbach BRICS, University of Aarhus,

Slides:



Advertisements
Similar presentations
1 Mariano Ceccato FBK Fondazione Bruno Kessler The TXL Programming Language (2)
Advertisements

Compiler Construction
Lexical Analysis Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!
YANGYANG 1 Chap 5 LL(1) Parsing LL(1) left-to-right scanning leftmost derivation 1-token lookahead parser generator: Parsing becomes the easiest! Modifying.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Top-Down Parsing.
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
ML-YACC David Walker COS 320. Outline Last Week –Introduction to Lexing, CFGs, and Parsing Today: –More parsing: automatic parser generation via ML-Yacc.
Prof. Bodik CS 164 Lecture 61 Building a Parser II CS164 3:30-5:00 TT 10 Evans.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Chapter 2 A Simple Compiler
May 14, 2002 Macro Languages AoPL, S'02 Macro Languages Claus Brabrand Michael I. Schwartzbach BRICS, University of Aarhus, Denmark.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #5 Introduction.
Compilation 2007 Context-Free Languages Parsers and Scanners Michael I. Schwartzbach BRICS, University of Aarhus.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
Lexical Analysis The Scanner Scanner 1. Introduction A scanner, sometimes called a lexical analyzer A scanner : – gets a stream of characters (source.
1 Scanning Aaron Bloomfield CS 415 Fall Parsing & Scanning In real compilers the recognizer is split into two phases –Scanner: translate input.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
LEX and YACC work as a team
1 Chapter 2 A Simple Compiler. 2 Outlines 2.1 The Structure of a Micro Compiler 2.2 A Micro Scanner 2.3 The Syntax of Micro 2.4 Recursive Descent Parsing.
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Intro to Lexing & Parsing CS 153. Two pieces conceptually: – Recognizing syntactically valid phrases. – Extracting semantic content from the syntax. E.g.,
# 1 CMPS 450 Parsing CMPS 450 J. Moloney. # 2 CMPS 450 Check that input is well-formed Build a parse tree or similar representation of input Recursive.
1 Top Down Parsing. CS 412/413 Spring 2008Introduction to Compilers2 Outline Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form.
COMP Parsing 2 of 4 Lecture 22. How do we write programs to do this? The process of getting from the input string to the parse tree consists of.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Context-Free Grammars
Context-Free Grammars and Parsing
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
COMP313A Programming Languages Lexical Analysis. Lecture Outline Lexical Analysis The language of Lexical Analysis Regular Expressions.
LANGUAGE TRANSLATORS: WEEK 14 LECTURE: REGULAR EXPRESSIONS FINITE STATE MACHINES LEXICAL ANALYSERS INTRO TO GRAMMAR THEORY TUTORIAL: CAPTURING LANGUAGES.
CS412/413 Introduction to Compilers Radu Rugina Lecture 4: Lexical Analyzers 28 Jan 02.
Left Recursion Lecture 7 Fri, Feb 4, 2005.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CPS 506 Comparative Programming Languages Syntax Specification.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Syntax Analysis – Part I EECS 483 – Lecture 4 University of Michigan Monday, September 17, 2006.
Top-Down Parsing.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 2: Lexical Analysis.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 2 Scanning. Dr.Manal AbdulazizCS463 Ch22 The Scanning Process Lexical analysis or scanning has the task of reading the source program as a file.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 due on Monday February 8 th Name and date your submission Submit electronically in Homework Server.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Growing Languages with Metamorphic Syntax Macros Claus Brabrand Michael Schwartzbach.
Programming Languages Translator
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 2 Scanning – Part 1 June 10, 2018 Prof. Abdelaziz Khamis.
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Context-Free Grammars
Compiler Construction (CS-636)
Top-Down Parsing.
Compiler Design 4. Language Grammars
Context-Free Grammars
CPSC 388 – Compiler Design and Construction
Lecture 7: Introduction to Parsing (Syntax Analysis)
CSC 4181Compiler Construction Context-Free Grammars
CSC 4181 Compiler Construction Context-Free Grammars
Context-Free Grammars
Context-Free Grammars
Presentation transcript:

May 21, 2002 The metafront Tool AoPL, S'02 Language Transformation: The metafront Tool Claus Brabrand Michael I. Schwartzbach BRICS, University of Aarhus, Denmark

May 21, 2002 The metafront Tool AoPL, S'02 Outline Introduction Specificity parsing Language transformation (by example) Current metafront limitations Exercises

May 21, 2002 The metafront Tool AoPL, S'02 Last Week Extend language with macros: New productions New nonterminals (via inductive morphings) Safety: Guaranteed termination Only legal syntax after expansion Errors caught at macro definition-time

May 21, 2002 The metafront Tool AoPL, S'02 metafront Macros are just a special case usage: A is an extension of B: m: L+ => L Make sure only need to write delta:  = L+ \ L metafront x: A => B AB program.a program.b transformation input language input program(s)output program(s) output language

May 21, 2002 The metafront Tool AoPL, S'02 Lex/Yacc? Extensibility: New productions and nonterminals? New terminals (lexical structure)? Errors: S/R, R/R: non-local error propagation Scanner: Scanner/Parser state-correspondence

May 21, 2002 The metafront Tool AoPL, S'02 Lex/Yacc? Extensibility: New productions and nonterminals? New terminals (lexical structure)? Errors: S/R, R/R: non-local error propagation Scanner: Scanner/Parser state-correspondence Instead…

May 21, 2002 The metafront Tool AoPL, S'02 Specificity Parsing

May 21, 2002 The metafront Tool AoPL, S'02 Specificity Grammar Definition:  base alphabet T  Reg(  ) set of regular languages over  N set of nonterminals s  N start nonterminal  : N  P(E*) \ Ø, where E = (T U N) G = ( ,T,N,s,  )

May 21, 2002 The metafront Tool AoPL, S'02 Example: While.l language While { terminal Num = { [0-9]+ } terminal Id = { [a-zA-Z0-9]+ } stm[ass]  := ; [if1]  if ( ) [if2]  if ( ) else [while]  while ( )  … // block and sequence exp[add]  + // and -,*,/ [base]  base[num]  [id]  } language While { terminal Num = { [0-9]+ } terminal Id = { [a-zA-Z0-9]+ } stm[ass]  := ; [if1]  if ( ) [if2]  if ( ) else [while]  while ( )  … // block and sequence exp[add]  + // and -,*,/ [base]  base[num]  [id]  } if(n)x:=0;elsex:=x+1;

May 21, 2002 The metafront Tool AoPL, S'02 Specificity Parsing parse(A: P(E*),  :  *) = 1.Calculate first-set (based on production set) 2.Determine applicability (first-set vs. input string) 3.Terminal Selection (among applicable terminals) 4.Production Selection (based on winner terminal) 5.Parse entity (based on winner production) case t: eat terminal t    ’ case n: parse n (recursively): parse(  (n),  )    ’ case  : stop! 6.Parse tail: parse(advance(A),  ’) parse(A: P(E*),  :  *) = 1.Calculate first-set (based on production set) 2.Determine applicability (first-set vs. input string) 3.Terminal Selection (among applicable terminals) 4.Production Selection (based on winner terminal) 5.Parse entity (based on winner production) case t: eat terminal t    ’ case n: parse n (recursively): parse(  (n),  )    ’ case  : stop! 6.Parse tail: parse(advance(A),  ’)

May 21, 2002 The metafront Tool AoPL, S'02 Specificity Parsing Challenge rounds: 1.Calculate first-set: { if, while, } 2.Determine applicability: { if : 2, while : -1, : 2 } stm[ass]  := ;  [if1]  if ( )  [if2]  if ( ) else  [while]  while ( )  stm[ass]  := ;  [if1]  if ( )  [if2]  if ( ) else  [while]  while ( )  #1 #2 #3 #4 #5 #6 #7 #8 input string: if(n)x:=0;elsex:=x+1;

May 21, 2002 The metafront Tool AoPL, S'02 Specificity Parsing 3.Terminal selection: { if : 2, while : -1, : 2 } –Selection priority: lexicographically –length: prefer longest match; –terminal specificity:prefer most specific match –In example: { if : 2, while : -1, : 2 }  winner: if -terminal if < id because { if }  { [a-zA-Z0-9]+ } t < t’ := regexp(t)  regexp(t’)

May 21, 2002 The metafront Tool AoPL, S'02 Specificity Parsing 4.Production(s) selection: given if - terminal –Selection priority: terminal < nonterminal <  prefer most specific –Also, n < n’ := first(n)  first(n’) –In Example: stm[ass,while] die!; winner: if -entity stm[ass]  := ;  [if1]  if ( )  [if2]  if ( ) else  [while]  while ( )  stm[ass]  := ;  [if1]  if ( )  [if2]  if ( ) else  [while]  while ( )  #1 #2 #3 #4 #5 #6 #7 #8

May 21, 2002 The metafront Tool AoPL, S'02 Specificity Parsing 5.Parse entity: if 6.Parse tail: advance if(n)x:=0;elsex:=x+1; stm[ass]  := ;  [if1]  if ( )  [if2]  if ( ) else  [while]  while ( )  stm[ass]  := ;  [if1]  if ( )  [if2]  if ( ) else  [while]  while ( )  #1 #2 #3 #4 #5 #6 #7 #8 stm[if1]  ( )  [if2]  ( ) else  stm[if1]  ( )  [if2]  ( ) else 

May 21, 2002 The metafront Tool AoPL, S'02 Specificity Parsing parse(A: P(E*),  :  *) = 1.Calculate first-set (based on production set) 2.Determine applicability (first-set vs. input string) 3.Terminal Selection (among applicable terminals) 4.Production Selection (based on winner terminal) 5.Parse entity (based on winner production) case t: eat terminal t    ’ case n: parse n (recursively): parse(  (n),  )    ’ case  : stop! 6.Parse tail: parse(advance(A),  ’) parse(A: P(E*),  :  *) = 1.Calculate first-set (based on production set) 2.Determine applicability (first-set vs. input string) 3.Terminal Selection (among applicable terminals) 4.Production Selection (based on winner terminal) 5.Parse entity (based on winner production) case t: eat terminal t    ’ case n: parse n (recursively): parse(  (n),  )    ’ case  : stop! 6.Parse tail: parse(advance(A),  ’)

May 21, 2002 The metafront Tool AoPL, S'02 Comments and Whitespace Semantics (“ omit o; ”) : language While { terminal { Whitespace = { [ \t\n\r]+ } EndOfLineComment = { "//".. \n } MultiLineComment = { "/*".. "*/" } Omit = { ( | | )* } } omit Omit; // omit defaults to { [ \t\n\r]+ } … } language While { terminal { Whitespace = { [ \t\n\r]+ } EndOfLineComment = { "//".. \n } MultiLineComment = { "/*".. "*/" } Omit = { ( | | )* } } omit Omit; // omit defaults to { [ \t\n\r]+ } … } n  e 1 e 2 … e n corresponds to n  o e 1 o e 2 o … e n o

May 21, 2002 The metafront Tool AoPL, S'02 Well-formedness No left-recursion As for metamorphisms last week… Derivability As for metamorphisms last week… Unique final winner: Production specificity Terminal specificity

May 21, 2002 The metafront Tool AoPL, S'02 Lookahead? Cannot discern stm[decl] from stm[exp] by looking at *** specificity clash: stm[decl vs. exp] round #1 on language JavaSubset { stm[decl]  [exp]  ; decl[var]  ; exp[id]   … } language JavaSubset { stm[decl]  [exp]  ; decl[var]  ; exp[id]   … }

May 21, 2002 The metafront Tool AoPL, S'02 Attractors “Try-to-parse” semantics: false  kill true  select –First([  ]  ) := First(  )  ignored! language JavaSubset { stm[decl]  [ ] [exp]  ; decl[var]  ; exp[id]   … } language JavaSubset { stm[decl]  [ ] [exp]  ; decl[var]  ; exp[id]   … }

May 21, 2002 The metafront Tool AoPL, S'02 The Case Trap Consider: stms will never stop on input “ case… ” because  First( stms ) and case  First( stms) Add “case trap”!: Recall: stm[switch]  switch ( ) { } swb[one]  [more]  case[case]  case : stm[switch]  switch ( ) { } swb[one]  [more]  case[case]  case : First([  ]  ) := First(  )  ignored! stm[trap]  [case] 

May 21, 2002 The metafront Tool AoPL, S'02 The Conjunction Trap Consider: Cannot parse: exp[and] will always attempt to steal an ampersand Add “conjunction trap”!: exp1[exp2]  [andand]  && exp2[exp3]  [and]  & exp3[id]  exp1[exp2]  [andand]  && exp2[exp3]  [and]  & exp3[id]  exp2[trap]  [&&]  x && y & higher precedence than &&

May 21, 2002 The metafront Tool AoPL, S'02 Specificity Parsing Advantages Scanner implicitly derived from grammar Deterministic disambiguation Local conflict resolution Avoids keywordification Selection independent of definition-order Convenient overloading Commit: no explosion; no backtracking

May 21, 2002 The metafront Tool AoPL, S'02 Language Transformation - by example -

May 21, 2002 The metafront Tool AoPL, S'02 Uminus Extend While language’s syntax: Specify transformation: Shorthand: language MyWhile extends While { exp[uminus]  - } language MyWhile extends While { exp[uminus]  - } morph MyWhile2While: MyWhile ==> While { metamorph stms,stm,exp,base; exp[uminus](B) base(B)=>xB ==> >> } morph MyWhile2While: MyWhile ==> While { metamorph stms,stm,exp,base; exp[uminus](B) base(B)=>xB ==> >> } bindersinductive transformations > B=>xB short for base(B)=>xB

May 21, 2002 The metafront Tool AoPL, S'02 More Examples

May 21, 2002 The metafront Tool AoPL, S'02 Current metafront Limitations metafront (170K Java, 6300 lines): –Current limitations: No terminal transformations (micros) All metamorphs must be explicitly declared Attractors must be nonterminals (no in-lining) No metamorph arguments –Current inefficiencies: No terminal selection caching Attractor implementation

May 21, 2002 The metafront Tool AoPL, S'02 Future Language/transformation calculus: Aidx(A): A => A x: A => B,source(x): A x: A => B,target(x): B x: A => B, y: B => Cx;y: A => C x: A => B, y: A => Cx*y: A => B*C …

May 21, 2002 The metafront Tool AoPL, S'02 Exercises

May 21, 2002 The metafront Tool AoPL, S'02 Exercises (any 3 out of 4) 1.Extend Java with a maybe construction: 1.Write Maybe.l, extending Java.l 2.Write Maybe2Java.x 2.Extend Java with a foreach construction: 1.Write Foreach.l, extending Java.l 2.Write Foreach2Java.x 3.Extend While with a switch construction: 1.Write Switch.l, extending While.l 2.Write Switch2While.x 4.Extend Java with any construction of your own choice: 1.Write.l, extending Java.l 2.Write 2Java.x

May 21, 2002 The metafront Tool AoPL, S'02 FIN