Workshop: Towards Highly Portable Software Jakarta, 21 – 23 January 2003 Diselenggarakan oleh Universitas IndonesiaUniversitas Indonesia Part 1 : Programming.

Slides:



Advertisements
Similar presentations
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Advertisements

Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
© M. Winter COSC 4P41 – Functional Programming Abstract data types (ADTs) An ADT is a data type together with some functions to manipulate elements.
What is a Parser? A parser is a program that analyses a piece of text to determine its syntactic structure  3 means 23+4.
ISBN Chapter 3 More Syntax –BNF –Derivations –Practice.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Prof. Bodik CS 164 Lecture 61 Building a Parser II CS164 3:30-5:00 TT 10 Evans.
Grammars Examples and Issues. Examples from Last Lecture a + b a b + a*bc* First draw a state diagram Then create a rule for each transition.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Grammars and Parsing. Sentence  Noun Verb Noun Noun  boys Noun  girls Noun  dogs Verb  like Verb  see Grammars Grammar: set of rules for generating.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Syntax Directed Definitions Synthesized Attributes
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
CS 2104 Prog. Lang. Concepts Dr. Abhik Roychoudhury School of Computing Introduction.
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Machine-independent code improvement Target code generation Machine-specific.
CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 6, 10/02/2003 Prof. Roy Levow.
0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Workshop: Towards Highly Portable Software Jakarta, 20 – 23 January 2003 Diselenggarakan oleh Universitas IndonesiaUniversitas Indonesia Using Pretty Printing.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 3 Describing Syntax and Semantics
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Workshop: Towards Highly Portable Software Jakarta, 21 – 23 January 2003 Diselenggarakan oleh Universitas IndonesiaUniversitas Indonesia Part 1 : Transformation.
BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
What is a Parser? A parser is a program that analyses a piece of text to determine its syntactic structure  3 means 23+4.
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
CS510 Compiler Lecture 4.
PROGRAMMING IN HASKELL
Chapter 3 – Describing Syntax
What does it mean? Notes from Robert Sebesta Programming Languages
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Compiler Construction
Syntax versus Semantics
CS 363 Comparative Programming Languages
CSE 3302 Programming Languages
Compiler Design 4. Language Grammars
Syntax-Directed Translation
PROGRAMMING IN HASKELL
Chapter 2: A Simple One Pass Compiler
Programming Languages
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Syntax-Directed Translation
PROGRAMMING IN HASKELL
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
COMPILER CONSTRUCTION
Faculty of Computer Science and Information System
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Workshop: Towards Highly Portable Software Jakarta, 21 – 23 January 2003 Diselenggarakan oleh Universitas IndonesiaUniversitas Indonesia Part 1 : Programming and Abstraction Dr. Ir. I.S.W.B. A. Azurat Lecture 2: Grammar and Parsing

2 Transformation Transformation: Transforming one form of structured information to another form. Any kind of information processing is transformation. Information with complicated structure: program, specification, formula Example of applications : –compiler, interpreter, translator –HTML tool –Y2K tool –Oracle code generator

3 Describing structure Example: e -10 Grammar : SignedFloat  Sign Float | Float Float  Pdigit. Pint e SignedExp SignedExpr  Sign Pint | Pint Pdigit  1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Pint  Digit | Digit Pint Digit  0 | Pdigit

4 Terminology Float  Pdigit. Pint e SignedExp This is called a production rule A grammar is a collection of a production rules. A grammar is context free (CGF) if the left hand side of all its rules consists of only one non-termimal. Non-terminal symbol Terminal symbols

5 Deriving sentences P   | APA | BPB | CPC A  a B  b C  c with start P as the symbol. A sentence s is a sentence of G, if it can be derived from the start symbol of G. Example: P   P  APA  AA ...  aa P  APA  ABPBA ...  abba

6 Language P   | APA | BPB | CPC A  a B  b C  c with start P as the symbol. L(G) = the set of all (terminal) sentences of G Example: for the above grammar L(G) = the set of all even length palindromes over {a,b,c}

7 Bigger Example Program  Identifier (ParameterList) Decl Body ParameterList  Parameter | Parameter, ParameterList Parameter  Identifier : Type... Body  { Statement } Statement  skip | Assignment |... Assignment  Identifier := Expression...

8 Transformation sentence transformation result "abba" P A P A P B B a a b b parse tree parser semantic function

9 Parse Tree Program Identifier ( ) ParameterList Decl Body x*y+1 Statement Assignment Identifier Expression poo x:int,y:int { } x :=

10 Representing Parse Tree data Pal = Empty | A Pal | B Pal | C Pal Example: A (B (A Empty)) represents abaaba

11 Representing Parse Tree Form  Form /\ Form | Form \/ Form | Form ==> Form | ~ Form | Var | Const Var ... Const  T | F

12 Representing Parse Tree a b a b \/ ~ ==> Expr Var Expr

13 Representing Parse Tree data Form = Form `AND` Form | Form `OR` Form | Form `IMP` Form | NOT Form | Var String | Const Bool Example: (Var "a" `OR` Var "b") `IMP` Var "a" Notice also similarty between data type definition and CFG.

14 Semantic Function Example: simple tautology checker (a /\ T) \/ ~a  taulogy we won't do this (a /\ F) ==> ~a  taulogy Represent result with Maybe Bool data Maybe a = Just a | nothing staut :: Form -> Maybe Bool

15 Semantic Function staut (Var a)= Nothing staut (Const c)= Just c staut (p `AND` q)= case (staut p, staut q) of (Just False, _) -> Just False (_, Just False) -> Just False (Just True, Just True) -> Just True otherwise -> Nothing

16 Semantic Function Anopther example: (simple) simplifier (a \/ T) /\ ~a  ~a simp :: Form -> Form

17 Semantic Function simp (Var a)= Var a simp (Const c)= Const c simp (p `AND` q)= case (simp p, simp q) of (Const False, _) -> Const False (_, Const False) -> Const False (Const True, q') -> q' (p', Const True)-> p' otherwise -> Nothing

18 Parser Parser: Take a string, and tries to build a parse tree. For our example grammar of formula: parser :: String -> Form –type Parser result = String -> result –type Parser result = String -> (result,String) –type Parser result = String -> [(result,String)] –type Parser sym res= [sym] -> [(res,[sym])]

19 Several Primitive Parsers symbol :: Eq s => s -> Parser s s symbol a [ ] = [ ] symbol a (x : xs) | x == a = [(x,xs)] | otherwise = [ ] Example: symbol 'C' :: Parser Char Char symbol 'C' "CLASS" = [('C', "LASS")]

20 Several Primitive Parsers satisfy :: (s -> Bool) -> Parser s s satisfy p [ ] = [ ] satisfy p (x : xs) | p x = [(x,xs)] | otherwise = [ ] Example: satisfy isDigit :: Parser Char Char satisfy isDigit "100" = [('1', "00")]

21 Several Primitive Parsers token :: Eq s => [s] -> Parser s [s] Example: token "class" "class A { } " = [("class", " A { } ")] token "class" "cla <= 0"= [ ]

22 Several Primitive Parsers failp :: Parser s a failp xs = [ ] succeed :: a -> Parser s a succeed r xs = [(r,xs)]

23 Parser Combinators ( ) :: Parser s a -> Parser s a -> Parser s a (p q) xs = p xs ++ q xs Example: pSign = symbol '+' symbol '-'

24 Parser Combinators ( ) :: Parser s (b -> a) -> Parser s b -> Parser s a (p q) xs = [ (f x, zs) | (f,ys) <- p xs, ( x, zs) <- q ys ] Example: (pSign succeed '+') satisfy isDigit symbol '.' pDigits

25 Parser Combinators ( ) :: (a -> b) -> Parser s a -> Parser s b (f p) xs = [ (f y, ys) | (y, ys) <- p xs ] Example: p = f satisfy isDigit where f :: String -> Int f c = read [c]

26 Priority and Associativity pDigits :: Parser Char String pDigits = pDigit ( (f pDigit) pDigits) where f digit = (\theRest -> digit : theRest) f digit theRest = digit : theRest or.... f is simply (:)

27 Priority and Associativity infixl 7 infixl 6 infixr 4 pDigits = pDigit (:) pDigit pDigits pDigits = read (pDigit (:) pDigit pDigits)

28 Example pIdentifier = (:) satisfy isLower pIdentifier sing satisfy isLower pVar :: Parser Char Form pVar = Var pIdentifier

29 Example pConst = mk_True symbol 'T' mk_False token 'F' where mk_True s = Const True mk_False s = Const False

30 Example pForm = mk_And pForm token "/\" pForm pVar pConst... left recursive!!

31 Removing Left Recursion Form  Atom /\ Form | Atom \/ Form | Atom ==> Form | ~ Form Atom  | Var | Const Var ... Const  T | F

32 Example pForm = mk_And pAtom token "/\" pForm... pAtom = pVar pConst where mk_And a _ f = a `AND` f

33 Greed pDigits = pDigit (:) pDigit pDigits Example: pDigit "123" = [("1","23"), ("12","3"), ("123", "") ] pDigits = (:) pDigit pDigits pDigit Example: pDigit "123" = [("123",""), ("12","3"), ("1", "23") ]

34 list x s = x : s Many many :: Parser s a -> Parser s [a] many p = list p many p succeed [ ] many1 :: Parser s a -> Parser s [a] many1 p = list p many p

35 Greedy greedy, greedy1 :: Parser s b -> Parser s [b] greedy = first. many greedy1 = first. many1 Example: pDigits = many1 (satisfy isDigit) pDigits = greedy1 (satisfy isDigit)