Syntax and Grammars 1 / 21

Similar documents
Syntax-Directed Translation. Lecture 14

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

A simple syntax-directed

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

Syntax Intro and Overview. Syntax

CPS 506 Comparative Programming Languages. Syntax Specification

Syntax. In Text: Chapter 3

A Simple Syntax-Directed Translator

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Chapter 3. Describing Syntax and Semantics

Course Overview. Introduction (Chapter 1) Compiler Frontend: Today. Compiler Backend:

Syntax. A. Bellaachia Page: 1

CSE 3302 Programming Languages Lecture 2: Syntax

Principles of Programming Languages COMP251: Syntax and Grammars

CSCE 314 Programming Languages

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

This book is licensed under a Creative Commons Attribution 3.0 License

B The SLLGEN Parsing System

Week 2: Syntax Specification, Grammars

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

Related Course Objec6ves

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

LECTURE 3. Compiler Phases

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Denotational Semantics. Domain Theory

Parsing CSCI-400. Principles of Programming Languages.

Lexical and Syntax Analysis. Top-Down Parsing

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Grammars & Parsing. Lecture 12 CS 2112 Fall 2018

Types and Static Type Checking (Introducing Micro-Haskell)

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

CMPT 755 Compilers. Anoop Sarkar.

Context-free grammars (CFG s)

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Compiling Regular Expressions COMP360

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

LANGUAGE PROCESSORS. Presented By: Prof. S.J. Soni, SPCE Visnagar.

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

A programming language requires two major definitions A simple one pass compiler

Principles of Programming Languages COMP251: Syntax and Grammars

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Lexical and Syntax Analysis

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

CS 314 Principles of Programming Languages

A language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

COP 3402 Systems Software Syntax Analysis (Parser)

Program Analysis ( 软件源代码分析技术 ) ZHENG LI ( 李征 )

INFOB3TC Solutions for the Exam

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

Principles of Programming Languages 2017W, Functional Programming

Context Free Grammars and Recursive Descent Parsing

CS558 Programming Languages. Winter 2013 Lecture 1

CSCI312 Principles of Programming Languages!

CSE302: Compiler Design

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Semantic Analysis. Lecture 9. February 7, 2018

Lexical Scanning COMP360

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

High Level Languages. Java (Object Oriented) This Course. Jython in Java. Relation. ASP RDF (Horn Clause Deduction, Semantic Web) Dr.

2.2 Syntax Definition

Introduction. Compiler Design CSE Overview. 2 Syntax-Directed Translation. 3 Phases of Translation

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Semantics of programming languages

Types and Static Type Checking (Introducing Micro-Haskell)

COP5621 Exam 3 - Spring 2005

CSE 12 Abstract Syntax Trees

A Tour of Language Implementation

Wednesday, August 31, Parsers

5. Syntax-Directed Definitions & Type Analysis

Compiler Code Generation COMP360

ASTS, GRAMMARS, PARSING, TREE TRAVERSALS. Lecture 14 CS2110 Fall 2018

22c:111 Programming Language Concepts. Fall Syntax III

UPTR - a simple parse tree representation format

Part 5 Program Analysis Principles and Techniques

CS101 Introduction to Programming Languages and Compilers

CS 415 Midterm Exam Spring 2002

Syntax-Directed Translation. Introduction

Operational Semantics. One-Slide Summary. Lecture Outline

Public-Service Announcement

Course Overview. Levels of Programming Languages. Compilers and other translators. Tombstone Diagrams. Syntax Specification

ASTs, Objective CAML, and Ocamlyacc

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

EECS 6083 Intro to Parsing Context Free Grammars

CS 360 Programming Languages Interpreters

Question Bank. 10CS63:Compiler Design

EECS 700 Functional Programming

Today. Elements of Programming Languages. Concrete vs. abstract syntax. L Arith. Lecture 1: Abstract syntax

Transcription:

Syntax and Grammars 1 / 21

Outline What is a language? Abstract syntax and grammars Abstract syntax vs. concrete syntax Encoding grammars as Haskell data types What is a language? 2 / 21

What is a language? Language: a system of communication using words in a structured way Natural language used for arbitrary communication complex, nuanced, and imprecise Programming language used to describe aspects of computation i.e. systematic transformation of representation programs have a precise structure and meaning English, Chinese, Hindi, Arabic, Spanish,... Haskell, Java, C, Python, SQL, XML, HTML, CSS,... We use a broad interpretation of programming language What is a language? 3 / 21

Object vs. metalanguage Important to distinguish two kinds of languages: Object language: the language we re defining Metalanguage: the language we re using to define the structure and meaning of the object language! A single language can fill both roles at different times! (e.g. Haskell) What is a language? 4 / 21

Syntax vs. semantics Two main aspects of a language: syntax: the structure of its programs semantics: the meaning of its programs Metalanguages for defining syntax: grammars, Haskell,... Metalanguages for defining semantics: mathematics, inference rules, Haskell,... What is a language? 5 / 21

Outline What is a language? Abstract syntax and grammars Abstract syntax vs. concrete syntax Encoding grammars as Haskell data types Abstract syntax and grammars 6 / 21

Programs are trees! Abstract syntax tree (AST): captures the essential structure of a program everything needed to determine its semantics + * if 2 * + + true + 5 3 4 5 6 7 8 2 3 2 + 3 * 4 (5 + 6) * (7 + 8) if true then (2+3) else 5 Abstract syntax and grammars 7 / 21

Grammars Grammars are a metalanguage for describing syntax The language we re defining is called the object language syntactic category nonterminal symbol s Sentence ::= n v n s and s n Noun ::= cats dogs ducks v Verb ::= chase cuddle production rules terminal symbol Abstract syntax and grammars 8 / 21

Generating programs from grammars How to generate a program from a grammar 1. start with a nonterminal s 2. find production rules with s on the LHS 3. replace s by one possible case on the RHS A program is in the language if and only if it can be generated by the grammar! Animal behavior language s Sentence ::= n v n s and s n Noun ::= cats dogs ducks v Verb ::= chase cuddle s n v n cats v n cats v ducks cats cuddle ducks Abstract syntax and grammars 9 / 21

Exercise Animal behavior language s Sentence ::= n v n s and s n Noun ::= cats dogs ducks v Verb ::= chase cuddle Is each program in the animal behavior language? cats chase dogs cats and dogs chase ducks dogs cuddle cats and ducks chase dogs dogs chase cats and cats chase ducks and ducks chase dogs Y : Yes N : No? : Abstract syntax and grammars 10 / 21

Abstract syntax trees Grammar (BNF notation) t Term ::= true false not t if t t t Example ASTs true if true false true not not false Language generated by grammar: set of all ASTs not Term = {true, false} { t t Term} { if t 1 t 2 t 3 t 1, t 2, t 3 Term} Abstract syntax and grammars 11 / 21

Exercise Arithmetic expression language i Int ::= 1 2... e Expr ::= add e e mul e e neg e i 1. Draw two different ASTs for the expression: 2+3+4 2. Draw an AST for the expression: -5*(6+7) 3. What are the integer results of evaluating the following ASTs: neg add add neg 3 5 3 5 Abstract syntax and grammars 12 / 21

Outline What is a language? Abstract syntax and grammars Abstract syntax vs. concrete syntax Encoding grammars as Haskell data types Abstract syntax vs. concrete syntax 13 / 21

Abstract syntax vs. concrete syntax Abstract syntax: captures the essential structure of programs typically tree-structured what we use when defining the semantics Concrete syntax: describes how programs are written down typically linear (e.g. as text in a file) what we use when we re writing programs in the language Abstract syntax vs. concrete syntax 14 / 21

Parsing Parsing: transforms concrete syntax into abstract syntax source code (concrete syntax) Parser abstract syntax tree Typically several steps: lexical analysis: chunk character stream into tokens generate parse tree: parse token stream into intermediate concrete syntax tree convert to AST: convert parse tree into AST Not a focus of this class! Abstract syntax vs. concrete syntax 15 / 21

Pretty printing Pretty printing: transforms abstract syntax into concrete syntax Inverse of parsing! abstract syntax tree Pretty Printer source code (concrete syntax) Abstract syntax vs. concrete syntax 16 / 21

Abstract grammar vs. concrete grammar Abstract grammar t Term ::= true false not t if t t t Concrete grammar t Term ::= true false not t if t then t else t ( t ) Our focus is on abstract syntax we re always writing trees, even if it looks like text use parentheses to disambiguate textual representation of ASTs but they are not part of the syntax Abstract syntax vs. concrete syntax 17 / 21

Outline What is a language? Abstract syntax and grammars Abstract syntax vs. concrete syntax Encoding grammars as Haskell data types Encoding grammars as Haskell data types 18 / 21

Encoding abstract syntax in Haskell defines set Abstract grammar Abstract syntax trees b Bool ::= true false t Term ::= not t if t t t b true if true false true not not false linear encoding Haskell data type definition data Term = Not Term If Term Term Term Lit Bool defines set Haskell values Lit True If (Lit True) (Lit False) (Lit True) Not (Not (Lit False)) Encoding grammars as Haskell data types 19 / 21

Translating grammars into Haskell data types Strategy: grammar Haskell 1. For each basic nonterminal, choose a built-in type, e.g. Int, Bool 2. For each other nonterminal, define a data type 3. For each production, define a data constructor 4. The nonterminals in the production determine the arguments to the constructor Special rule for lists: in grammars, s ::= t can translate any of these to a Haskell list: data Term =... type Sentence = [Term] is shorthand for: s ::= ɛ t s or s ::= ɛ t, s Encoding grammars as Haskell data types 20 / 21

Example: Annotated arithmetic expression language Abstract syntax n Nat ::= (natural number) c Comm ::= (comment string) e Expr ::= neg e negation e @ c comment e + e addition e * e multiplication n literal Haskell encoding type Comment = String data Expr = Neg Expr Annot Comment Expr Add Expr Expr Mul Expr Expr Lit Int Encoding grammars as Haskell data types 21 / 21