Download presentation
Presentation is loading. Please wait.
1
Concepts of Programming Languages
Lecturer: Dr Emad Nabil Lecture # 3 Fall 2018 Copyright © 2012 Pearson Education. All rights reserved.
2
Code Samples of some Programming languages
C#, Java, Lisp, Ada, Prolog Copyright © 2012 Pearson Education. All rights reserved.
3
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 )) 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.
4
=================================================
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.
5
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;
6
Describing Syntax and Semantics
Chapter 3 Describing Syntax and Semantics
7
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.
8
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)
9
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.
10
Formal Method pf describing a syntax Context-Free Grammars
Backus-Naur Form Copyright © 2012 Pearson Education. All rights reserved.
11
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.
12
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.
13
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.
14
Copyright © 2012 Pearson Education. All rights reserved.
15
Copyright © 2012 Pearson Education. All rights reserved.
16
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
17
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.
18
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.
19
An Example Grammar Copyright © 2012 Addison-Wesley. All rights reserved.
20
An Example Derivation Copyright © 2012 Pearson Education. All rights reserved.
21
An Example Derivation Every string of symbols in a derivation is a sentential form Copyright © 2012 Addison-Wesley. All rights reserved.
22
An Example Derivation Copyright © 2012 Pearson Education. All rights reserved.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.