Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.

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 #5 Lecture #5 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
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.
ISBN Chapter 3 More Syntax –BNF –Derivations –Practice.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
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.
1 CSE305 Programming Languages Syntax What is it? How is it specified? Who uses it? Why is it needed?
ISBN Chapter 3 Describing Syntax and Semantics.
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.
Chapter 3: Formal Translation Models
Specifying Languages CS 480/680 – Comparative Languages.
ISBN Chapter 3 Describing Syntax and Semantics.
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.
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.
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal 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.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Grammars CPSC 5135.
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.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
CPS 506 Comparative Programming Languages Syntax Specification.
D Goforth COSC Translating High Level Languages.
C HAPTER 3 Describing Syntax and Semantics. T OPICS Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
Chapter 3 Describing Syntax and Semantics
ISBN Chapter 3 Describing Syntax and Semantics.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports 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:
CS 3304 Comparative Languages
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Describing Syntax and Semantics
Concepts of Programming Languages
CS510 Compiler Lecture 4.
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
CSE 3302 Programming Languages
Describing Syntax and Semantics
Context–free Grammar CFG is also called BNF (Backus-Naur Form) grammar
CS 3304 Comparative Languages
September 13th Grammars.
Chapter 3 Describing Syntax and Semantics.
High-Level Programming Language
Describing Syntax and Semantics
Presentation transcript:

Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6

Chapter 3 Topics Definitions Tokens and Lexemes Formal Definition of Languages Formal Methods of Describing Syntax  Context Free Grammar (CFG)  Backus-Naur Form (BNF)  Derivation  Parse Trees An Ambiguous Expression Grammar Presidency and associativity of grammars Syntax Graphs 2Chapter 3: Describing Syntax and Semantics

Definitions 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. Syntax of programming languages can be specified by Context Free Grammar (CFG) (will be discussed later). 3Chapter 3: Describing Syntax and Semantics

Tokens and Lexemes A lexeme is the lowest level syntactic unit of a language (meaningful units which compose a sentence). A token is a category of lexemes (e.g., identifier, constant, …). Example: A sentence if index > 2 then count := 17; LexemesTokens if, then key word index, count identifier (id) 2, 17 constant (const) > relational operator (relop) := assignment operator ; end of statement 4Chapter 3: Describing Syntax and Semantics

Formal Definition of Languages Recognizers o Either accepts or rejects an input string. o Given a string, a recognizer for a language L tells whether or not the string is in L. o The syntax analysis part of a compiler is a recognizer for the language. Generators  A device that generates sentences of a language.  A generator for L will produce an arbitrary string in L on demand. (ex: Grammar, BNF) 5Chapter 3: Describing Syntax and Semantics

Formal Methods of Describing Syntax Backus-Naur Form (BNF) and Context-Free Grammars (CFG):  Most widely known method for describing programming language syntax. Extended BNF (EBNF)  Improves readability and writability of BNF. 6Chapter 3: Describing Syntax and Semantics

Context-Free Grammars (CFG) Context-Free Grammars (CFGs):  Developed by Noam Chomsky in the mid-1950s.  Language generators, meant to describe the syntax of natural languages.  Context-free grammars are used to describe the syntax of modern programming languages. 7Chapter 3: Describing Syntax and Semantics

Context-Free Grammars (cont.) A CFG is a 4-tuple (N, T, S, P), where:  N is a set of nonterminal symbols.  T is a set of terminal symbols (N  T =  ).  S  N is the start symbol.  P is a set of productions (also called rules). 8Chapter 3: Describing Syntax and Semantics

Context-Free Grammars (cont.) Example: P = { S  bA, S  aA, A  aA, A  b }  Nonterminals = {S, A}  Terminals = {a, b}  Start symbol = S A sentence consists entirely of terminal symbols (in this grammar, a’s and b’s). The productions can be used to generate sentences, starting with a rule for the start symbol (S). 9Chapter 3: Describing Syntax and Semantics

Using production rules To generate a sentence, we start with a rule whose left hand side is the start symbol (S). In the preceding grammar we start with: S  bA or S  aA We systematically replace nonterminals in the resulting expression with the right hand sides of rules for the nonterminals. This is called expanding the nonterminal. In the preceding grammar, we might replace an A with either aA or b. 10Chapter 3: Describing Syntax and Semantics

Generating a sentence We demonstrate the generation of a sentence baab using the productions: P = { S  bA, S  aA, A  aA, A  b } S  bA (start with start symbol)  baA(replace A with aA)  baaA (replace A with aA)  baab (replace A with b) 11Chapter 3: Describing Syntax and Semantics

More sentences P = { S  bA, S  aA, A  aA, A  b } S  bA  bb S  bA  baA  bab S  aA  aaA  aaaA  aaab 12Chapter 3: Describing Syntax and Semantics

CFG Conventions Nonterminals are often distinguished by using:  Upper case or italicized letters (S, A, Stmt, …)  Angle brackets (e.g., ) Terminals are distinguished by using:  Lower case letters (a, b, …). The first production rule is a rule for the start symbol. Using these conventions, we can define the grammar by listing only the rules. 13Chapter 3: Describing Syntax and Semantics

Backus-Naur Form (BNF) Backus-Naur Form (BNF) (1959):  Invented by John Backus to describe Algol 58.  BNF is equivalent to context-free grammars (CFG).  In BNF, rules are used to describe the syntax of a language.  In BNF, there is at least one rule for each language abstraction (nonterminal). 14Chapter 3: Describing Syntax and Semantics

BNF Fundamentals Nonterminals: BNF abstractions. Terminals: lexemes and tokens. Grammar: a collection of rules.  Examples of BNF rules:  |  while do  if then 15Chapter 3: Describing Syntax and Semantics

BNF Rules A rule has a left-hand side (LHS) and a right-hand side (RHS), and consists of terminal and nonterminal symbols. A grammar is a finite nonempty set of rules. An abstraction (or nonterminal symbol) can have more than one RHS.  | begin end 16Chapter 3: Describing Syntax and Semantics

Recursive BNF rules Syntactic lists are described in BNF using recursion:  ident | ident,  num | + num 17Chapter 3: Describing Syntax and Semantics