Chapter 3: Describing Syntax and Semantics

Slides:



Advertisements
Similar presentations
ISBN Chapter 3 Describing Syntax and Semantics.
Advertisements

Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
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?
1 Note As usual, these notes are based on the Sebesta text. The tree diagrams in these slides are from the lecture slides provided in the instructor resources.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
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.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
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)
Grammars CPSC 5135.
Chapter 3 Part I Describing Syntax and Semantics.
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.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
ISBN Chapter 3 Describing Syntax and Semantics.
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.
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:
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
BBM Programming Languages
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
PROGRAMMING LANGUAGES
Describing Syntax and Semantics
CS510 Compiler Lecture 4.
Chapter 3 Context-Free Grammar and Parsing
Chapter 3 – Describing Syntax
Concepts of Programming Languages
Syntax (1).
What does it mean? Notes from Robert Sebesta Programming Languages
Context-Free Grammars
Syntax versus Semantics
Compiler Construction (CS-636)
CS 363 Comparative Programming Languages
Describing Syntax and Semantics
Syntax One - Hybrid CMSC 331.
Describing Syntax and Semantics
Describing Syntax and Semantics
Context-Free Grammars
Context-Free Grammars
Programming Language Syntax 2
Chapter 2: A Simple One Pass Compiler
CSC 4181Compiler Construction Context-Free Grammars
R.Rajkumar Asst.Professor CSE
Describing Syntax and Semantics
CSC 4181 Compiler Construction Context-Free Grammars
Context-Free Grammars
Syntax vs Semantics Backus-Naur Form Extended BNF Derivations
Chapter 3 Describing Syntax and Semantics.
BNF 9-Apr-19.
Describing Syntax and Semantics
Describing Syntax and Semantics
- Who must use language definitions?
Context-Free Grammars
Programming Languages 2nd edition Tucker and Noonan
COMPILER CONSTRUCTION
Presentation transcript:

Chapter 3: Describing Syntax and Semantics Lectures # 7

Chapter 3 Topics Syntax Graphs 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 Chapter 3: Describing Syntax and Semantics 2

Derivation A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence. In each step of a derivation, exactly one nonterminal is expanded. Every string of symbols in a derivation is a sentential form. A sentential form may contain terminal and nonterminal symbols. A sentence is a sentential form that has only terminal symbols. Chapter 3: Describing Syntax and Semantics 3

Types of Derivations Leftmost derivation: a derivation in which the leftmost nonterminal in the sentential form is always the one that is expanded. Rightmost derivation: a derivation in which the rightmost nonterminal in the sentential form is always the one that is expanded. A derivation may be neither leftmost nor rightmost. Chapter 3: Describing Syntax and Semantics 4

An Example Grammar <program>  <stmts> <stmts>  <stmt> | <stmt> ; <stmts> <stmt>  <var> = <expr> <var>  a | b | c | d <expr>  <term> + <term> | <term> - <term> <term>  <var> | const Chapter 3: Describing Syntax and Semantics 5

An Example Derivation Using the above grammar derive the following sentence: a = b + const <program>  <stmts>  <stmt>  <var> = <expr>  a = <expr>  a = <term> + <term>  a = <var> + <term>  a = b + <term>  a = b + const Sentential Forms Sentence Chapter 3: Describing Syntax and Semantics 6

Parse Trees Parse tree is a well-defined representation of a syntactic structure of a sentence. A parse tree is a tree with the following properties: 1) Root is labeled with the starting symbol; 2) Each leaf is labeled with a terminal symbol (token); 3) Each internal node is labeled with a nonterminal symbol; Chapter 3: Describing Syntax and Semantics 7

Parse Tree vs Derivation <program>  <stmts>  <stmt>  <var> = <expr>  a = <expr>  a = <term> + <term>  a = <var> + <term>  a = b + <term>  a = b + const Chapter 3: Describing Syntax and Semantics 8

Ambiguous Grammars A grammar is ambiguous if and only if it generates a sentential form that has 2 or more distinct parse trees. Example: Using the sentence const – const/const, prove that the following expression grammar is ambiguous: <expr>  <expr> <op> <expr> | const <op>  / | - Chapter 3: Describing Syntax and Semantics 9

An Ambiguous Expression Grammar The sentence: const – const/const <expr>  <expr> <op> <expr> | const <op>  / | - Chapter 3: Describing Syntax and Semantics 10

Indicating Precedence If we use the parse tree to indicate precedence levels of the operators, we can avoid ambiguity. <expr>  <expr> - <term> | <term> <term>  <term>/const | const Chapter 3: Describing Syntax and Semantics 11

Associativity of Operators Operator associativity can also be indicated by a grammar. The following tree is left associative: <expr>  <expr> + <term> | <term> <term>  <term> * const | const Produce the expression: (3 + 4) + 5 Chapter 3: Describing Syntax and Semantics 12

Associativity of Operators (cont.) Suppose we reverse the order of <expr> and <term> on the RHS of the first rule: <expr>  <term> + <expr> | <term> <term>  <term> * const | const The tree corresponds to: 3 + (4 + 5), meaning + is now right associative. Chapter 3: Describing Syntax and Semantics 13

Rule of thumb for associativity A left recursive production results in left associativity E  E + T (+ is left associative) A right recursive production results in right associativity E  T + E (+ is right associative) Chapter 3: Describing Syntax and Semantics 14

Extended BNF (EBNF) Optional parts are placed in brackets [ ]. <proc_call>  ident [(<expr_list>)] BNF: <proc_call>  ident | ident(<expr_list>) Alternative parts of RHSs are placed inside parentheses and separated via vertical bars. <term>  <term> (+|-) const BNF: <term>  <term> + const | <term> - const Repetitions (0 or more) are placed inside braces { }. <ident>  letter {letter|digit} BNF: <ident>  <ident>(letter|digit) | letter Chapter 3: Describing Syntax and Semantics \ 15

BNF and EBNF BNF <expr>  <expr> + <term> <term>  <term> * <factor> | <term> / <factor> | <factor> EBNF <expr>  <term>{(+|-)<term>} <term>  <factor>{(*|/)<factor>} Chapter 3: Describing Syntax and Semantics 16

Syntax Graphs Syntax graphs use directed graphs to represent syntax graphically. Terminals are placed in circles. Nonterminals are placed in rectangles. Circles and rectangles are connected with lines with arrowheads. Pascal type declarations Chapter 3: Describing Syntax and Semantics 17