©University of Sheffieldcom2010 Com2010 - Functional Programming Demos: LexPrs & PrsRes & Software Engineering Design and Coding Marian Gheorghe Lecture.

Slides:



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

INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
Context-Free Grammars Lecture 7
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
How domain specific are Domain Specific Languages? Greg Michaelson School of Mathematical & Computer Sciences Heriot-Watt University.
ANTLR Andrew Pangborn & Zach Busser. ANTLR in a Nutshell ANother Tool for Language Recognition generates lexers generates parsers (and parse trees)‏ Java-based,
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
1 214 review. 2 What we have learnt Generate scanner and parser –We do not program directly –Instead we write the specifications for the scanner and parser.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax Directed Definitions Synthesized Attributes
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Com Functional Programming Higher Order Functions and Computation Patterns (I) Marian Gheorghe Lecture 10 Module homepage Mole &
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Review Joey Paquet,
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.
PART I: overview material
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lexical Analysis (I) Compiler Baojian Hua
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 3, 09/11/2003 Prof. Roy Levow.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Copyright © Curt Hill Languages and Grammars This is not English Class. But there is a resemblance.
©University of Sheffieldcom2010 Com Functional Programming Syntax Analysis Marian Gheorghe Lecture 16 Module homepage Mole &
Com Functional Programming Regular Expressions and Abstract Data Types Marian Gheorghe Lecture 14 Module homepage Mole &
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 1, 08/28/03 Prof. Roy Levow.
CPS 506 Comparative Programming Languages Syntax Specification.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
Com Functional Programming Lazy Evaluation Marian Gheorghe Lecture 13 Module homepage Mole & ©University of Sheffieldcom2010.
Com Functional Programming Lexical Analysis Marian Gheorghe Lecture 15 Module homepage Mole & ©University of Sheffieldcom2010.
CSE 413 Languages & Implementation Hal Perkins Autumn 2012 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R T W O Syntax.
What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Sahar Mosleh California State University San MarcosPage 1 Finite State Machine.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CMSC 330: Organization of Programming Languages Operational Semantics.
©SoftMoore ConsultingSlide 1 Context-Free Grammars.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 13 CS2110 – Spring
Regular Expressions, Backus-Naur Form and Reverse Polish Notation
Chapter 3 – Describing Syntax
Parsing 2 of 4: Scanner and Parsing
A Simple Syntax-Directed Translator
CS 326 Programming Languages, Concepts and Implementation
Parsing and Parser Parsing methods: top-down & bottom-up
SOFTWARE DESIGN AND ARCHITECTURE
Overview of Compilation The Compiler Front End
Overview of Compilation The Compiler Front End
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
PROGRAMMING LANGUAGES
Syntax versus Semantics
Compiler Construction (CS-636)
Presentation by Julie Betlach 7/02/2009
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
ADTs, Grammars, Parsing, Tree traversals
C H A P T E R T W O Syntax.
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Chapter 3 Describing Syntax and Semantics.
6.001 SICP Interpretation Parts of an interpreter
High-Level Programming Language
COMPILER CONSTRUCTION
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 2, 09/04/2003 Prof. Roy Levow.
Faculty of Computer Science and Information System
Overview of the Course.
Compiler design Review COMP 442/6421 – Compiler Design
Presentation transcript:

©University of Sheffieldcom2010 Com Functional Programming Demos: LexPrs & PrsRes & Software Engineering Design and Coding Marian Gheorghe Lecture 18 Module homepage Mole &

Computational models utilised lexical analysis and parsing (regular expressions, finite state machines, EBNF, syntax diagrams etc) Haskell key concepts: –Complex data structures: algebraic types (simple, compound, polymorphic, recursive) –Recursive functions – recursive descent parser –Higher order functions – three basic parsing diagrams –List comprehension Software engineering approach on designing, coding and maintenance What have we learned? ©University of Sheffieldcom2010

Compiling AST ©University of Sheffieldcom2010 Lexical analysisParsing Compiler Semantics; code, evaluation… Source fileToken unitsAST "a = a + 1 " ++ “/*comment*/" [(1,"a"),(5,"="),(1,"a"), (3,"+"),(2,"1"),(0,"Eop")] ?? ? AST – Abstract Syntax Tree Key - codeLexical unit - string spaces, comment only in source

Note how lex_aut represents various transitions ( Move constructor) It recognizes: identifiers, numbers, comments, delimiters ( +,-,=,; ), spaces Invoked as: lex_an lex_aut inLexN for inLex1 = "ident = ; /*comment*/ anotherIdentThenNumber12 124" ~~> OK inLex2 = "something 1 /*wrong_identifier*/ ident_y ok" ~~> ident_y is not identifier inLex3 = "something 2 /*wrongNumber*/ 1.24 ok" ~~> 1.24 is not number inLex4 = "something 3 /*wrongDelimiter*/ < ok" ~~> < is not delimiter LexPrs: Lexical analysis ©University of Sheffieldcom2010

Compiling AST ©University of Sheffieldcom2010 Lexical analysisParsing Compiler Semantics; code, evaluation… Source fileToken unitsAST "a = a + 1 " ++ “/*comment*/" [(1,"a"),(5,"="),(1,"a"), (3,"+"),(2,"1"),(0,"Eop")] ?? ? AST – Abstract Syntax Tree Key - codeLexical unit - string spaces, comment only in source

Note how basic diagrams are implemented Parser is invoked as fProgram (lex_an lex_aut inPr1) Tests: inPr1 = "a = 0 ; a = a + 1 ; b = a + a - 2“ ~~> Ok inPr2 = "a = 0 a = a + 1 ; b = a“ ~~> missing ; inPr3 = "a = 0 ; a = a + ; b = a" ~~> missing operand inPr4 = "a = 0 ; a = a 1 ; b = a" ~~> missing operator ( + ) LexPrs: parsing ©University of Sheffieldcom2010

Compiling AST ©University of Sheffieldcom2010 Lexical analysisParsing Compiler Semantics; code, evaluation… Source fileToken unitsAST "a = a + 1 " ++ “/*comment*/" [(1,"a"),(5,"="),(1,"a"), (3,"+"),(2,"1"),(0,"Eop")] ?? ? AST – Abstract Syntax Tree Key - codeLexical unit - string spaces, comment only in source

PrsRes: Postfix notation eval ©University of Sheffieldcom2010 SetOf TokenUnit replaced by ParserUnit Changes in the Parser code (effect terminal rules) Extract postfix notation Invoked as snd(snd(fProgram (lex_an lex_aut inp,([],[])))) Or extractOut inp Tests inp1 = "a = 0 ; a = a + 1 ; b = a + a - 2“ ~~> [(1,"a"),(2,"0"),(5,"="),(6,";"),(1,"a"),(1,"a"),(2,"1"),(3,"+"),(5,"="),(6,";"),(1,"b"), (1,"a"), (1,"a"),(3,"+"),(2,"2"),(4,"-"),(5,"="),(0,"Eop")]

PrsRes: Postfix notation eval (2) ©University of Sheffieldcom2010 Extract expressions Invoked as extractExp(extractOut inp,[]) Tests inp2 = "a = ; b = 8 ; c = " ~~> ([],[("a",["6","2","+"]),("b",["8"]),("c",["2","1","-","3","+"])]) Expression evaluation Invoked as evalSA(snd(extractExp(extractOut inp2,[]))) Tests inp2 – above [("a",8),("b",8),("c",4)]

Software Engineering Haskell Projects ©University of Sheffieldcom2010 Lexical analyser requires changes in the associated model (FSM), lex_aut –see the code Parser consists of a set of recursive functions – it requires a stepwise process with short iterations Implementing all SA functions (15) in one step simply does not work why? – get a complex set of recursive invocations impossible to manage and debug need a strategy... Initial step:

1. Program :: =(S)7. Trm::=(A) StmtList EopIdentifier 2. StmtList ::=(I)Number Assign8. Operator ::=(A) DelimAddOp 3. Assign ::=(S)MinOp LHandS RestAss9. LHandS ::= ident (T) 4. RestAss ::=(S)10. AssSymb ::= assg (T) AssSymb Exp11. Identifier ::= ident (T) 5. Exp ::=(I)12. Number ::= no (T) Trm13. Delim ::= sc (T) Operator14. Addop ::= pls (T) 6. Eop ::= eop(T)15. MinOp ::= mns (T) Instead of the Entire SA ©University of Sheffieldcom2010

Create an Initial Step ©University of Sheffieldcom2010 Use only diagrams 1, 6 and change 2 to a simpler form, i.e.: 2. StmtList ::= ident So, you have only 3 simple diagrams: a sequence (1) and two terminals (2, 6) These are simple to be tested. Test program: (See the code...) “a /*just_an_identifier*/”

Next Step ©University of Sheffieldcom2010 Revert diagram 2 to its initial form, i.e.: 2. StmtList ::= Assign {Delim Assign} where Assign is simply a terminal and Delim keeps its definition, i.e.: 3. Assign ::= ident 13. Delim ::= sc You have: a sequence (1), an iteration (2) and two terminals (6, 13) These will be tested by: (See the code...) “a ; a1 ; b /*just_identifiers_and_sc*/”

Generic Step ©University of Sheffieldcom2010 Based on the previous step, add on one or two new diagrams The associated components are made terminals Alternation and/or iteration sets, if any, might be adjusted Write a test set for this new iteration Looks like an XP approach!

Finally, the Back-end ©University of Sheffieldcom2010 To implement the back-end, the following transformations are made into the parser The generic SetOf TokenUnit is replaced Adequate output is implemented by adding certain functions the terminal rules (usually fTerm transformed into seqOf ) Functions to extract the output Functions implementing requested transformations (postfix notation etc.)

Where to find information ©University of Sheffieldcom2010 Lecture notes – Mole Lecture slides + LexPrs & PrsRes code on my page