Programming Language Definition. Regular Expressions

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

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

Syntax. In Text: Chapter 3

Chapter 3. Describing Syntax and Semantics

Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics

Programming Language Syntax and Analysis

Syntax. A. Bellaachia Page: 1

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

Syntax and Semantics

Formal Languages. Formal Languages

CPS 506 Comparative Programming Languages. Syntax Specification

3. DESCRIBING SYNTAX AND SEMANTICS

Programming Languages

Habanero Extreme Scale Software Research Project

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

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

Principles of Programming Languages COMP251: Syntax and Grammars

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

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

Dr. D.M. Akbar Hussain

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

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

COP 3402 Systems Software Syntax Analysis (Parser)

3. Context-free grammars & parsing

ICOM 4036 Spring 2004

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

Lecture 4: Syntax Specification

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Building Compilers with Phoenix

CMSC 330: Organization of Programming Languages. Context Free Grammars

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

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

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

CMSC 330: Organization of Programming Languages

Defining syntax using CFGs

CMSC 330: Organization of Programming Languages. Context Free Grammars

Part 5 Program Analysis Principles and Techniques

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages

A Simple Syntax-Directed Translator

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

CMSC 330: Organization of Programming Languages

CSE 3302 Programming Languages Lecture 2: Syntax

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

Context-free grammars (CFG s)

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

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

Programming Languages Third Edition

Grammar Rules in Prolog

Grammar Rules in Prolog!!

10/4/18. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntactic Analysis

Principles of Programming Languages COMP251: Syntax and Grammars

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

This book is licensed under a Creative Commons Attribution 3.0 License

B The SLLGEN Parsing System

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Lexical and Syntax Analysis

EECS 6083 Intro to Parsing Context Free Grammars

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

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

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions

CMPT 755 Compilers. Anoop Sarkar.

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

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

Lecture 10 Parsing 10.1

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

CSE302: Compiler Design

Syntax Analysis. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Non-deterministic Finite Automata (NFA)

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

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

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

CSE 12 Abstract Syntax Trees

Languages, Automata, Regular Expressions & Scanners. Winter /8/ Hal Perkins & UW CSE B-1

CSCE 314 Programming Languages

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

CS 314 Principles of Programming Languages

Chapter 2 - Programming Language Syntax. September 20, 2017

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

Syntax & Semantics. COS 301 Programming Languages

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

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Grammars, Scanners & Regular Expressions

Week 2: Syntax Specification, Grammars

A simple syntax-directed

Project 2 Interpreter for Snail. 2 The Snail Programming Language

Languages and Compilers

Homework & Announcements

COMPILER DESIGN UNIT I LEXICAL ANALYSIS. Translator: It is a program that translates one language to another Language.

Lexical Analysis. Introduction

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Syntax and Grammars 1 / 21

Transcription:

Programming Language Definition Syntax To describe what its programs look like Specified using regular expressions and context-free grammars Semantics To describe what its programs mean Specified using axiomatic semantics, operational semantics, or denotational semantics 1 Regular Expressions A regular expression is one of the following: A character The empty string, denoted by ε Two or more regular expressions concatenated Two or more regular expressions separated by (or) A regular expression followed by the Kleene star (concatenation of zero or more strings) 2 1

Regular Expressions The pattern of numeric constants can be represented as: 3 What is the meaning of following expressions? [0-9a-f]+ b[aeiou]+t a*(ba*ba*)* 4 2

Define Regular Expressions Match strings only consisting of a, b, or c characters Match only the strings Buy more milk, Buy more bread, or Buy more juice Match identifiers which contain letters and digits, starting with a letter 5 Lexeme, Token, & Pattern [1] Lexeme A sequence of characters in the source program with the lowest level of syntactic meanings E.g., sum, +, - Token A category of lexemes A lexeme is an instance of token The basic building blocks of programs 6 3

Token Examples Token Informal Description Sample Lexemes keyword All keywords defined in the language if else comparison <, >, <=, >=, ==,!= <=,!= id Letter followed by letters and digits pi, score, D2 number Any numeric constant 3.14159, 0, 6 literal Anything surrounded by s, but exclude core dumped 7 Lexeme, Token, & Pattern [1] Pattern A description of the form that the lexemes of a token may take Specified with regular expressions 8 4

Context-Free Grammars Using the notation Backus-Naur Form (BNF) A context-free grammar consists of [1] A set of terminals T A set of non-terminals N A start symbol S (a non-terminal) A set of productions P 9 Terminals T The basic symbols from which strings are formed Terminals are tokens if, foo, ->, a 10 5

Non-terminals N Syntactic variables that denote sets of strings or classes of syntactic structures expr, stmt Impose a hierarchical structure on the language 11 Start Symbol S One nonterminal Denote the language defined by the grammar 12 6

Production P Specify the manner in which terminals and nonterminals are combined to form strings Each production has the format nonterminal -> a string of nonterminals and terminals One nonterminal can be defined by a list of nonterminals and terminals 13 Production Nonterminal symbols can have more than one distinct definition, representing all possible syntactic forms in the language <if_stmt> -> if <logic_expr> then <stmt> <if_stmt> -> if <logic_expr> then <stmt> else <stmt> Or <if_stmt> -> if <logic_expr> then <stmt> if <logic_expr> then <stmt> else <stmt> 14 7

Backus-Naur Form Invented by John Backus and Peter Naur to describe syntax of Algol 58/60 Used to describe the context-free grammars A meta-language: a language used to describe another language 15 BNF Rules A rule has a left-hand side(lhs), one or more right-hand side (RHS), and consists of terminal and nonterminal symbols For a nonterminal, when there is more than one RHS, there are multiple alternative ways to expand/replace the nonterminal E.g., <stmt> -> <single_stmt> begin <stmt_list> end 16 8

BNF Rules Rules can be defined using recursion <ident_list> -> ident ident, <ident_list> Two types of recursion Left recursion: id_list_prefix -> id_list_prefix, id id Right recursion The above example 17 How does BNF work? It is like a mathematical game: You start with a symbol S You are given rules (Ps) describing how you can replace the symbol with other symbols (Ts or Ns) The language defined by the BNF grammar is the set of all terminal strings you can produce by following these rules 18 9

Derivation By repeatedly applying rules to nonterminals, we end up with strings containing only terminal symbols (sentences) All derived strings compose the language defined by the grammar 19 10