CS 331, Principles of Programming Languages Chapter 2.

Slides:



Advertisements
Similar presentations
Programming Language Concepts
Advertisements

Where Syntax Meets Semantics
ICE1341 Programming Languages Spring 2005 Lecture #5 Lecture #5 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
ISBN Chapter 3 Describing Syntax and Semantics.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
Honors Compilers An Introduction to Grammars Feb 12th 2002.
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
A basis for computer theory and A means of specifying languages
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Programming Languages An Introduction to Grammars Oct 18th 2002.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
30-Jun-15 BNF. Metalanguages A metalanguage is a language used to talk about a language (usually a different one) We can use English as its own metalanguage.
Chapter 3: Formal Translation Models
Specifying Languages CS 480/680 – Comparative Languages.
S YNTAX. Outline Programming Language Specification Lexical Structure of PLs Syntactic Structure of PLs Context-Free Grammar / BNF Parse Trees Abstract.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Syntactic Analysis Dr. Hyunyoung Lee.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
A sentence (S) is composed of a noun phrase (NP) and a verb phrase (VP). A noun phrase may be composed of a determiner (D/DET) and a noun (N). A noun phrase.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Grammars CPSC 5135.
LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE
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.
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.
11 Chapter 4 Grammars and Parsing Grammar Grammars, or more precisely, context-free grammars, are the formalism for describing the structure of.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
Syntax Context-Free Grammars, Syntax Trees. CFG for Arithmetic Expressions E::= E op E| (E) | num op ::= + | * num ::= 0 | 1 | 2 | … Backus-Naur Form.
CSE 425: Syntax II Context Free Grammars and BNF In context free grammars (CFGs), structures are independent of the other structures surrounding them Backus-Naur.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 3 Part II Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
ISBN Chapter 3 Describing Syntax and Semantics.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
LESSON 04.
CS 331, Principles of Programming Languages Chapter 2.
Syntax and Grammars.
Semantics (1).
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
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.
©SoftMoore ConsultingSlide 1 Context-Free Grammars.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
CS510 Compiler Lecture 4.
Chapter 3 – Describing Syntax
Syntax (1).
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

CS 331, Principles of Programming Languages Chapter 2

Overview What’s the difference between syntax and semantics? Expressions Grammars Tree representations –abstract syntax trees –parse trees

How to describe a PL Tutorials - SNOBOL is still the best example Reference manuals - ADA Formal definitions - to describe both syntax and semantics, which is hard –Pascal, ADA, PL/I

Syntax vs. Semantics Syntax - what is a legal program? Semantics - what does a (legal) program mean? Three major approaches: –axiomatic, i.e. a set of proof rules –denotational, i.e. mathematical description –operational, i.e. operations on a real or abstract machine

How to describe syntax? By example? –Possibly ambiguous or incomplete –Used to describe shells, e.g. man pages By use of a meta-language –Also possibly ambiguous or incomplete –But probably more precise –Possible to give some semantics in the same notation

Context-free Grammars There are lots of varieties of grammars –regular, context-free, context-sensitive, and unrestricted CFGs are constrained so that exactly one non-terminal can appear on the left-side of a production –but a non-terminal may appear on the left-side of more than one production

CFG Notation CFG productions have exactly one non- terminal on the left side, and zero or more non-terminals or terminals on the right side Usually, nonterminals are enclosed in Terminals (aka tokens) may be quoted for clarity

Backus-Naur Form (BNF) BNF is a popular notation for CFGs –from a simple subset of Pascal ::=. ::= ::= begin end ::= ::= ; ::= | | |... ::= if then ::= if then else

BNF Operators Sequence ::= c Alternation ::= | Optional ::= [ ] Zero or more ::= * One or more ::= + –note that * is a shorthand for [ +]

Formal Grammars Set of terminal symbols (or tokens) Set of non-terminal symbols A designated start symbol A set of productions (or rules) that specify how symbols are to be combined to form legal strings G=

Expressions Prefix, postfix, or infix Issues related to operators –arity (unary, binary, ternary, or whatever) –associativity exponentiation is right-associative, usually other ops are usually left-associative –precedence follows rules from arithmetic

Abstract Syntax Trees Useful for indicating how an expression is evaluated The expression is represented Or is it?

Examples of Prefix and Postfix Prefix –LISP operators use prefix Postfix –Postscript operators use postfix The simple expression 8-(7*3) is represented as: mul sub –Old HP calculators did, too - no parens keys

To run LISP in emacs Invoke emacs –M-x lisp-interaction-mode –type control-j at the end of each line Or using an inferior emacs lisp process, –M-x ielm

(+ 2 2) 4 (sqrt 9) 3.0 (setq b 6) 6 (setq a 2) 2 (setq c 5) 5 a 2 (- b) -3 (+ (- b) (sqrt( - (* b b) (* (* 4 a) c))))

Prefix, Infix, Postfix Given an abstract syntax tree, an expression can be represented in any of the three ways Consider for example a+b*c/d What does the abstract syntax tree look like? What are the prefix and postfix expressions equivalent to the infix form given above?

Ambiguity There may be many (equivalent) grammars for a language. There may be more than one way to evaluate a string with respect to a grammar A grammar is ambiguous if, for any string in the language, that string can be parsed in more than one way.

Dangling-Else Suppose a grammar has the production How should we parse this statement? if E then if E2 then S1 else S2 ::= if then | if then else

A different ambiguity header ::= title (link? | script?) title ::= text link ::= text script ::= text This grammar allows the and constructs to appear in either order. The grammar above is then ambiguous!

header ::= title (link? | script?) title ::= text link ::= text script ::= text How do we parse this string? Some Title