CSCE 314: Programming Languages Dr. Dylan Shell

Slides:



Advertisements
Similar presentations
Application: Yacc A parser generator A context-free grammar An LR parser Yacc Yacc input file:... definitions... %... production rules... %... user-defined.
Advertisements

Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
String is a synonym for the type [Char].
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
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.
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Topic #3: Lexical Analysis
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)
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax (cont.) Lecture 4 – Syntax (Cont.), Spring CSE3302 Programming Languages,
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lexical and Syntax Analysis
Interpreter CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns Some material taken from:
Functional Programming guest lecture by Tim Sheard Parsing in Haskell Defining Parsing Combinators.
Advanced Functional Programming Tim Sheard 1 Lecture 14 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.
Advanced Programming Andrew Black and Tim Sheard Lecture 11 Parsing Combinators.
Comp 311 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright August 28, 2009.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
Com Functional Programming Lexical Analysis Marian Gheorghe Lecture 15 Module homepage Mole & ©University of Sheffieldcom2010.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Workshop: Towards Highly Portable Software Jakarta, 21 – 23 January 2003 Diselenggarakan oleh Universitas IndonesiaUniversitas Indonesia Part 1 : Programming.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
CSE 311 Foundations of Computing I Lecture 18 Recursive Definitions: Context-Free Grammars and Languages Autumn 2011 CSE 3111.
CSE 3302 Programming Languages
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.
5. Context-Free Grammars and Languages
Chapter 3 – Describing Syntax
Announcements Today: the Parsing Domain-specific Language
String is a synonym for the type [Char].
Conditional Expressions
Koen Lindström Claessen
Instructor: Laura Kallmeyer
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
PROGRAMMING IN HASKELL
Chapter 3 – Describing Syntax
Syntax (1).
A lightening tour in 45 minutes
What does it mean? Notes from Robert Sebesta Programming Languages
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Compiler Construction
Syntax Analysis Sections :.
CSE 3302 Programming Languages
Lecture 3: Introduction to Syntax (Cont’)
Syntax Analysis Sections :.
Syntax-Directed Definition
PROGRAMMING IN HASKELL
Programming Language Syntax 2
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
PROGRAMMING IN HASKELL
Chapter 2: A Simple One Pass Compiler
PROGRAMMING IN HASKELL
Types and Classes in Haskell
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Higher Order Functions
PROGRAMMING IN HASKELL
SYNTAX DIRECTED DEFINITION
CSCE 314: Programming Languages Dr. Dylan Shell
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
A simple function.
Compiler Design Yacc Example "Yet Another Compiler Compiler"
Discrete Maths 13. Grammars Objectives
PROGRAMMING IN HASKELL
Faculty of Computer Science and Information System
Presentation transcript:

CSCE 314: Programming Languages Dr. Dylan Shell Parsing and grammars

Arithmetic Expressions Consider a simple form of expressions built up from single digits using the operations of addition + and multiplication *, together with parentheses. We also assume that: * and + associate to the right. * has higher priority than +.

Example: Parsing a token space :: Parser () space = many (sat isSpace) >> return () token :: Parser a -> Parser a token p = space >> p >>= \v -> space >> return v identifier :: Parser String identifier = token ident ident :: Parser String ident = sat isLower >>= \x -> many (sat isAlphaNum) >>= \xs -> return (x:xs)

Formally, the syntax of such expressions is defined by the following context free grammar: expr → term '+' expr | term term → factor '*' term | factor factor → digit | '(' expr ')' digit → '0' | '1' | … | '9'

However, for reasons of efficiency, it is important to factorize the rules for expr and term: expr → term ('+' expr | ε) term → factor ('*' term | ε) Note: The symbol ε denotes the empty string.

It is now easy to translate the grammar into a parser that evaluates expressions, by simply rewriting the grammar rules using the parsing primitives. That is, we have: expr → term ('+' expr | ε) term → factor ('*' term | ε) expr :: Parser Int expr = do t ← term do char '+' e ← expr return (t + e) +++ return t

term → factor ('*' term | ε) expr → term ('+' expr | ε) term → factor ('*' term | ε) term :: Parser Int term = do f ← factor do char '*' t ← term return (f * t) +++ return f factor :: Parser Int factor = do d ← digit return (digitToInt d) +++ do char '(' e ← expr char ')' return e

then we try out some examples: Finally, if we define eval :: String → Int eval xs = fst (head (parse expr xs)) then we try out some examples: > eval "2*3+4" 10 > eval "2*(3+4)" 14 > eval "2+5-" 7 > eval "+5-" *** Exception: Prelude.head: empty list

Here’s a variation: expr → term '+' expr | term term → factor '*' term | factor factor → digit | '(' expr ')' | '[' expr ']' digit → '0' | '1' | … | '9'

factor :: Parser Int factor = do d ← digit return (digitToInt d) +++ do char '(' e ← expr char ')' return e +++ do char '[' char ']'