Chapter 3. Describing Syntax and Semantics

Similar documents
CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

Describing Syntax and Semantics

Syntax. In Text: Chapter 3

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

Chapter 3. Describing Syntax and Semantics ISBN

Programming Language Syntax and Analysis

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics

Programming Language Definition. Regular Expressions

Habanero Extreme Scale Software Research Project

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

Syntax and Semantics

ICOM 4036 Spring 2004

3. DESCRIBING SYNTAX AND SEMANTICS

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

Syntax. A. Bellaachia Page: 1

Homework & Announcements

Programming Languages

Chapter 3. Topics. Languages. Formal Definition of Languages. BNF and Context-Free Grammars. Grammar 2/4/2019

Principles of Programming Languages COMP251: Syntax and Grammars

COP 3402 Systems Software Syntax Analysis (Parser)

EECS 6083 Intro to Parsing Context Free Grammars

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

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

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

CPS 506 Comparative Programming Languages. Syntax Specification

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Defining syntax using CFGs

Lecture 10 Parsing 10.1

CMSC 330: Organization of Programming Languages. Context Free Grammars

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

CMSC 330: Organization of Programming Languages

3. Context-free grammars & parsing

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages

Dr. D.M. Akbar Hussain

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Context Free Grammars

Formal Languages. Formal Languages

Parsing II Top-down parsing. Comp 412

Introduction to Parsing

Context-free grammars (CFG s)

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

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

Part 5 Program Analysis Principles and Techniques

Chapter 3. Describing Syntax and Semantics. Introduction. Why and How. Syntax Overview

Programming Languages, Summary CSC419; Odelia Schwartz

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Languages and Compilers

CSCI312 Principles of Programming Languages!

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Principles of Programming Languages COMP251: Syntax and Grammars

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

Chapter 4. Lexical and Syntax Analysis

CSE302: Compiler Design

This book is licensed under a Creative Commons Attribution 3.0 License

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

CS 230 Programming Languages

CSCE 314 Programming Languages

Syntax Analysis Check syntax and construct abstract syntax tree

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

Lexical and Syntax Analysis. Top-Down Parsing

Theory and Compiling COMP360

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

P L. rogramming anguages. Fall COS 301 Programming Languages. Syntax & Semantics. UMaine School of Computing and Information Science

Syntax & Semantics. COS 301 Programming Languages

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

CS 314 Principles of Programming Languages

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

Lexical Scanning COMP360

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

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

Introduction to Lexing and Parsing

Optimizing Finite Automata

A Simple Syntax-Directed Translator

Compiler Design Concepts. Syntax Analysis

Defining Languages GMU

CSE 3302 Programming Languages Lecture 2: Syntax

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

4. Lexical and Syntax Analysis

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

CSE 12 Abstract Syntax Trees

3. Parsing. Oscar Nierstrasz

2.2 Syntax Definition

4. Lexical and Syntax Analysis

EDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised:

Building Compilers with Phoenix

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Syntax Intro and Overview. Syntax

CMPT 755 Compilers. Anoop Sarkar.

Transcription:

Chapter 3 Describing Syntax and Semantics

Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings of Programs: Dynamic Semantics 2

Introduction Syntax: the form or structure of the expressions, statements, and program units Semantics: the meaning of the expressions, statements, and program units Syntax and semantics provide a language s definition Users of a language definition Other language designers Implementers Programmers (the users of the language) 3

The General Problem of Describing Syntax: Terminology A sentence is a string of characters over some alphabet A language is a set of sentences A lexeme is the lowest level syntactic unit of a language (e.g., *, sum, begin) A token is a category of lexemes (e.g., identifier) 4

Formal Definition of Languages Recognizers A recognition device reads input strings over the alphabet of the language and decides whether the input strings belong to the language Example: syntax analysis part of a compiler - Detailed discussion of syntax analysis appears in chapter 4 Generators A device that generates sentences of a language One can determine if the syntax of a particular sentence is syntactically correct by comparing it to the structure of the generator 5

Generating images Deep learning in your Prisma app See also Gatys et al. 2015: Separating content and style in a deep network 6

Generating images Deep learning in your Prisma app 7

Natural language (Google translate) Haruki Murakami compares his own translation from Japanese to English (of Hemingway s Snows of Kilimanjaro ) to the 2016 deep learning Google translate NO. 1: Kilimanjaro is a snow-covered mountain 19,710 feet high, and is said to be the highest mountain in Africa. Its western summit is called the Masai Ngaje Ngai, the House of God. Close to the western summit there is the dried and frozen carcass of a leopard. No one has explained what the leopard was seeking at that altitude. NO. 2: Kilimanjaro is a mountain of 19,710 feet covered with snow and is said to be the highest mountain in Africa. The summit of the west is called Ngaje Ngai in Masai, the house of God. Near the top of the west there is a dry and frozen dead body of leopard. No one has ever explained what leopard wanted at that altitude. from New York Times, Dec 14 2016 8

BNF and Context-Free Grammars Context-Free Grammars Developed by Noam Chomsky in the mid-1950s for natural languages Language generators, meant to describe the syntax of natural languages Define a class of languages called context-free languages Backus-Naur Form (1959) Invented by John Backus to describe Algol 58 BNF is equivalent to context-free grammars 9

BNF Fundamentals In BNF, abstractions are used to represent syntactic structures (also called nonterminal symbols, or just nonterminals) Terminals are lexemes or tokens A rule has a left-hand side (LHS), which is a nonterminal, and a right-hand side (RHS), which is a string of terminals and/or nonterminals Nonterminals are often enclosed in angle brackets Examples of BNF rules: <assign> <var> = <expression> <if_stmt> if <logic_expr> then <stmt> Grammar: a finite non-empty set of rules A start symbol is a special element of the nonterminals of a grammar 10

BNF Rules An abstraction (or nonterminal symbol) can have more than one RHS <stmt> <single_stmt> begin <stmt_list> end 11

Describing Lists Syntactic lists are described using recursion <ident_list> ident ident, <ident_list> 12

Example Grammar for small language <program> begin <stmt_list> end <stmt_list> <stmt> <stmt> ; <stmt_list> <stmt> <var> = <expression> <var> a b c <expression> <var> + <var> <var> - <var> <var> 13

Example Grammar for small language <program> begin <stmt_list> end <stmt_list> <stmt> <stmt> ; <stmt_list> <stmt> <var> = <expression> <var> a b c <expression> <var> + <var> <var> - <var> <var> 14

Example derivation <program> => begin <stmt_list> end We ll derive A = B + C; B = C with this grammar A derivation is a repeated application of rules, starting with the start symbol (in this case program) => reads derives 15

Example derivation <program> => begin <stmt_list> end => begin <stmt> ; <stmt_list> end 16

Example derivation <program> => begin <stmt_list> end => begin <stmt> ; <stmt_list> end => begin <var> = <expression> ; <stmt_list> end 17

Example derivation <program> => begin <stmt_list> end => begin <stmt> ; <stmt_list> end => begin <var> = <expression> ; <stmt_list> end => begin A = <expression> ; <stmt_list> end 18

Example derivation <program> => begin <stmt_list> end => begin <stmt> ; <stmt_list> end => begin <var> = <expression> ; <stmt_list> end => begin A = <expression> ; <stmt_list> end => begin A = <var> + <var> ; <stmt_list> end 19

Example derivation <program> => begin <stmt_list> end => begin <stmt> ; <stmt_list> end => begin <var> = <expression> ; <stmt_list> end => begin A = <expression> ; <stmt_list> end => begin A = <var> + <var> ; <stmt_list> end => begin A = B + <var> ; <stmt_list> end 20

Example derivation <program> => begin <stmt_list> end => begin <stmt> ; <stmt_list> end => begin <var> = <expression> ; <stmt_list> end => begin A = <expression> ; <stmt_list> end => begin A = <var> + <var> ; <stmt_list> end => begin A = B + <var> ; <stmt_list> end => begin A = B + C ; <stmt_list> end 21

Example derivation <program> => begin <stmt_list> end => begin <stmt> ; <stmt_list> end => begin <var> = <expression> ; <stmt_list> end => begin A = <expression> ; <stmt_list> end => begin A = <var> + <var> ; <stmt_list> end => begin A = B + <var> ; <stmt_list> end => begin A = B + C ; <stmt_list> end => begin A = B + C ; <stmt> end 22

Example derivation <program> => begin <stmt_list> end => begin <stmt> ; <stmt_list> end => begin <var> = <expression> ; <stmt_list> end => begin A = <expression> ; <stmt_list> end => begin A = <var> + <var> ; <stmt_list> end => begin A = B + <var> ; <stmt_list> end => begin A = B + C ; <stmt_list> end => begin A = B + C ; <stmt> end => begin A = B + C ; <var> = <expression> end 23

Example derivation <program> => begin <stmt_list> end => begin <stmt> ; <stmt_list> end => begin <var> = <expression> ; <stmt_list> end => begin A = <expression> ; <stmt_list> end => begin A = <var> + <var> ; <stmt_list> end => begin A = B + <var> ; <stmt_list> end => begin A = B + C ; <stmt_list> end => begin A = B + C ; <stmt> end => begin A = B + C ; <var> = <expression> end => begin A = B + C ; B = <expression> end 24

Example derivation <program> => begin <stmt_list> end => begin <stmt> ; <stmt_list> end => begin <var> = <expression> ; <stmt_list> end => begin A = <expression> ; <stmt_list> end => begin A = <var> + <var> ; <stmt_list> end => begin A = B + <var> ; <stmt_list> end => begin A = B + C ; <stmt_list> end => begin A = B + C ; <stmt> end => begin A = B + C ; <var> = <expression> end => begin A = B + C ; B = <expression> end => begin A = B + C ; B = <var> end 25

Example derivation <program> => begin <stmt_list> end => begin <stmt> ; <stmt_list> end => begin <var> = <expression> ; <stmt_list> end => begin A = <expression> ; <stmt_list> end => begin A = <var> + <var> ; <stmt_list> end => begin A = B + <var> ; <stmt_list> end => begin A = B + C ; <stmt_list> end => begin A = B + C ; <stmt> end => begin A = B + C ; <var> = <expression> end => begin A = B + C ; B = <expression> end => begin A = B + C ; B = <var> end => begin A = B + C ; B = C end 26

Derivations Every string of symbols in a derivation is called a sentential form A sentence is a sentential form that has only terminal symbols A leftmost derivation is one in which the leftmost nonterminal in each sentential form is the one that is expanded A derivation may be neither leftmost nor rightmost 27

Parse Tree A hierarchical representation of a derivation <program> <stmt_list> <stmt> <var> = <expression> a <var> + <var> B C 28

Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has two or more distinct parse trees Problematic for compilers since parse tree, and therefore meaning of the structure, cannot be determined uniquely 29

An Ambiguous Expression Grammar <expr> <expr> <op> <expr> const <op> / - <expr> <expr> <expr> <op> <expr> <expr> <op> <expr> <expr> <op> <expr> <expr> <op> <expr> const - const / const const - const / const 30

Example 2 parse trees for the sentence A=B+C*A Operator precedence Conflicting precedence 31