Concepts of Programming Languages

Slides:



Advertisements
Similar presentations
Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #4 Lecture #4 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
ISBN Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
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.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
1 Introduction: syntax and semantics Syntax: a formal description of the structure of programs in a given language. Semantics: a formal description of.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
ISBN Chapter 3 Describing Syntax and Semantics.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Describing Syntax and Semantics
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
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)
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Muhammad Idrees Lecturer University of Lahore 1. Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved. 1-2 Chapter 3 Topics Introduction The General Problem.
ISBN Chapter 3 Describing Syntax and Semantics.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
Describing Syntax and Semantics Session 2 Course : T Programming Language Concept Year : February 2011.
ISBN Chapter 3 Describing Syntax and Semantics.
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
C H A P T E R T W O Syntax and Semantic. 2 Introduction Who must use language definitions? Other language designers Implementors Programmers (the users.
1 CS Programming Languages Class 04 September 5, 2000.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
CS 3304 Comparative Languages
BBM Programming Languages
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Describing Syntax and Semantics
Lecture 3 Concepts of Programming Languages
Chapter 3 – Describing Syntax
Concepts of Programming Languages
What does it mean? Notes from Robert Sebesta Programming Languages
Automata and Languages What do these have in common?
Syntax versus Semantics
CS 363 Comparative Programming Languages
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
CS 3304 Comparative Languages
CS 3304 Comparative Languages
Chapter 3: Describing Syntax and Semantics Sangho Ha
Describing Syntax and Semantics
Describing Syntax and Semantics
Chapter 3 Describing Syntax and Semantics.
High-Level Programming Language
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
- Who must use language definitions?
Describing Syntax and Semantics
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Presentation transcript:

Concepts of Programming Languages Lecturer: Dr Emad Nabil Lecture # 3 Fall 2018 Copyright © 2012 Pearson Education. All rights reserved.

Code Samples of some Programming languages C#, Java, Lisp, Ada, Prolog Copyright © 2012 Pearson Education. All rights reserved.

Lisp Output =================== Hello World (write-line "Hello World") ; greet the world (write-line "single quote used, it inhibits evaluation") (write-line " " ) (write '(* 2 3)) (write-line " " ) (write-line "single quote not used, so expression evaluated") (write (* 2 3)) (defun averagenum (n1 n2 n3 n4) (/ ( + n1 n2 n3 n4) 4) ) (write(averagenum 10 20 30 40)) Output =================== Hello World single quote used, it inhibits evaluation (* 2 3) single quote not used, so expression evaluated 6 25 In LISP: The value of the last expression in the body is returned as the value of the function.

================================================= Prolog male(james1). male(charles1). male(charles2). male(james2). male(george1). female(catherine). female(elizabeth). female(sophia). parent(charles1, james1). parent(elizabeth, james1). parent(charles2, charles1). parent(catherine, charles1). parent(james2, charles1). parent(sophia, elizabeth). parent(george1, sophia). Running ================================================= ?-parent(charles1, george1). %Was George I the parent of Charles I? False ?-parent(charles1,X). %Who was Charles I's parent? james1 ?-parent(X,charles1). % Who were the children of Charles I? X = charles2; X = Catherine; X = james2; C++ function fun(integer foo) { if(foo == 5) doSomething(); else doSomeOtherThing(); } Prolog Copyright © 2012 Pearson Education. All rights reserved. fun(5) :- doSomething. fun(Foo) :- Foo =/= 5, doSomeOtherThing.

C# Java Python Ada with Ada.Text_IO; use Ada.Text_IO; using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program public static void Main(string[] args) //Your code goes here Console.WriteLine("Hello, world!"); } //'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 Packadge myPackadge ; import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) System.out.println("Hello, World!"); } Python Ada def my_function(): print("Hello From My Function!") def sum_two_numbers(a, b): return a + b my_function() print(sum_two_numbers(2,3)) with Ada.Text_IO; use Ada.Text_IO; procedure Hello is begin Put_Line ("Hello, world!"); end Hello;

Describing Syntax and Semantics Chapter 3 Describing Syntax and Semantics

Chapter 3 Topics Introduction (syntax and semantics) The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings of Programs: Dynamic Semantics Copyright © 2012 Addison-Wesley. All rights reserved.

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 (who impellent the language) Programmers (the users of the language)

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) Copyright © 2012 Addison-Wesley. All rights reserved.

Formal Method pf describing a syntax Context-Free Grammars Backus-Naur Form Copyright © 2012 Pearson Education. All rights reserved.

Formal Method pf describing a syntax: BNF and Context-Free Grammars Developed by Noam Chomsky in the mid-1950s Meant to describe the syntax of natural languages Define a class of languages called context-free languages Avram Noam Chomsky is an American linguist, philosopher, cognitive scientist, historian, social critic, and political activist.

Formal Method pf describing a syntax: BNF and Context-Free Grammars Backus-Naur Form (1959) Invented by John Backus and peter Naur to describe the syntax of Algol 58 BNF is equivalent to context-free grammars John Warner Backus was an American computer scientist. He directed the team that invented the first widely used high-level programming language and was the inventor of the Backus-Naur form, a widely used notation to define formal language syntax. He directed the team that invented the first widely used high-level programming language (FORTRAN) Peter Naur was a Danish computer science pioneer and Turing award winner. His last name is the "N" in the BNF notation, used in the description of the syntax for most programming languages. He contributed to the creation of the ALGOL 60 programming language.

Formal Method pf describing a syntax: BNF Fundamentals In BNF: abstractions are used to represent classes of syntactic structures—(syntactic variables or nonterminal symbols) A rule has a left-hand side (LHS), which is a nonterminal, a right-hand side (RHS), which is a string of terminals and/or nonterminals Copyright © 2012 Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Education. All rights reserved.

Copyright © 2012 Pearson Education. All rights reserved.

Set of rules (productions) Formal Method pf describing a syntax:BNF Fundamentals Grammar(G) Start symbol Set of rules (productions) Set of terminals Set of non-terminals Nonterminals are often enclosed in angle brackets Example of BNF rule: <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

BNF Rules Formal Method pf describing a syntax:BNF Fundamentals An abstraction (or nonterminal symbol) can have more than one RHS <stmt>  <single_stmt> | begin <stmt_list> end Copyright © 2012 Addison-Wesley. All rights reserved.

Formal Method pf describing a syntax:BNF Fundamentals: Rules Syntactic lists are described using recursion <ident_list>  ident | ident, <ident_list> A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols) Copyright © 2012 Addison-Wesley. All rights reserved.

An Example Grammar Copyright © 2012 Addison-Wesley. All rights reserved.

An Example Derivation Copyright © 2012 Pearson Education. All rights reserved.

An Example Derivation Every string of symbols in a derivation is a sentential form Copyright © 2012 Addison-Wesley. All rights reserved.

An Example Derivation Copyright © 2012 Pearson Education. All rights reserved.