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.

Slides:



Advertisements
Similar presentations
Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
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.
ISBN Chapter 3 Describing Syntax and Semantics.
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
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.
A basis for computer theory and A means of specifying languages
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.
Grammars Examples and Issues. Examples from Last Lecture a + b a b + a*bc* First draw a state diagram Then create a rule for each transition.
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
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
1 Disambiguating the grammar If we use the parse tree to indicate precedence levels of the operators, we can remove the ambiguity. The following rules.
Roadmap Ch. 1 Classification of languages What make a “good” language?
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.
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.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
ISBN Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
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.
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.
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.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved. 1-2 Chapter 3 Topics Introduction The General Problem.
Syntax and Semantics Structure of programming languages.
C HAPTER 3 Describing Syntax and Semantics. T OPICS Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
Chapter 3 Describing Syntax and Semantics
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 © 2002 by Addison Wesley Longman, Inc Introduction - Who must use language definitions? 1. Other language designers 2. Implementors 3.
Describing Syntax and Semantics Session 2 Course : T Programming Language Concept Year : February 2011.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 3 Topics Introduction The General Problem.
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.
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.
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:
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.
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Chapter 3 Context-Free Grammar and Parsing
Chapter 3 – Describing Syntax
Syntax (1).
CS 363 Comparative Programming Languages
Describing Syntax and Semantics
COMPILER CONSTRUCTION
Presentation transcript:

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 for the text, and were made by David Garrett.

2 Context-free (CF) grammar A CF grammar is formally presented as a 4-tuple G=(T,NT,P,S), where: –T is a set of terminal symbols (the alphabet) –NT is a set of non-terminal symbols –P is a set of productions (or rules), where P  NT  (T  NT)* –S  NT

3 Example 1 L 1 = { 0, 00, 1, 11 } G 1 = ( {0,1}, {S}, { S  0, S  00, S  1, S  11 }, S )

4 Example 2 L 2 = { the dog chased the dog, the dog chased a dog, a dog chased the dog, a dog chased a dog, the dog chased the cat, … } G 2 = ( { a, the, dog, cat, chased }, { S, NP, VP, Det, N, V }, { S  NP VP, NP  Det N, Det  a | the, N  dog | cat, VP  V | VP NP, V  chased }, S ) Notes: S = Sentence, NP = Noun Phrase, N = Noun VP = Verb Phrase, V = Verb, Det = Determiner

5 Examples of lexemes and tokens LexemesTokens fooidentifier i sumidentifier -3integer_literal 10integer_literal 1 ;statement_separator =assignment_operator

6 BNF Fundamentals Sample rules [p. 128] → = → if then → if then else non-terminals/tokens surrounded by lexemes are not surrounded by keywords in language are in bold → separates LHS from RHS | expresses alternative expansions for LHS → if then | if then else = is in this example a lexeme

7 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 often given simply as a set of rules (terminal and non-terminal sets are implicit in rules, as is start symbol)

8 Describing Lists There are many situations in which a programming language allows a list of items (e.g. parameter list, argument list). Such a list can typically be as short as empty or consisting of one item. Such lists are typically not bounded. How is their structure described?

9 Describing lists The are described using recursive rules. Here is a pair of rules describing a list of identifiers, whose minimum length is one:  ident | ident, Notice that ‘, ’ is part of the object language

10 Example 3 L 3 = { 0, 1, 00, 11, 000, 111, 0000, 1111, … } G 3 = ( {0,1}, {S,ZeroList,OneList}, { S  ZeroList | OneList, ZeroList  0 | 0 ZeroList, OneList  1 | 1 OneList }, S )

11 Derivation of sentences from a grammar A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols).

12 Example: derivation from G 2 Example: derivation of the dog chased a cat S  NP VP  Det N VP  the N VP  the dog VP  the dog V NP  the dog chased NP  the dog chased Det N  the dog chased a N  the dog chased a cat

13 Example: derivations from G 3 Example: derivation of S  ZeroList  0 ZeroList  0 0 ZeroList  ZeroList  Example: derivation of S  OneList  1 OneList  1 1 OneList  1 1 1

14 Observations about derivations Every string of symbols in the derivation is a sentential form. A sentence is a sentential form that has only terminal symbols. A leftmost derivation is one in which the leftmost nonterminal in each sentential form is the one that is expanded. A derivation can be leftmost, rightmost, or neither.

15 An example grammar  | ;  =  a | b | c | d  + | -  | const

16 A leftmost derivation => => = => a = => a = + => a = b + => a = b + const

17 Parse tree A hierarchical representation of a derivation const a = b +

18 Parse trees and compilation A compiler builds a parse tree for a program (or for different parts of a program). If the compiler cannot build a well-formed parse tree from a given input, it reports a compilation error. The parse tree serves as the basis for semantic interpretation/translation of the program.

19 Extended BNF Optional parts are placed in brackets [ ] -> ident [( )] Alternative parts of RHSs are placed inside parentheses and separated via vertical bars → (+|-) const Repetitions (0 or more) are placed inside braces { } → letter {letter|digit}

20 Comparison of BNF and EBNF sample grammar fragment expressed in BNF  + | - |  * | / | same grammar fragment expressed in EBNF  {(+ | -) }  {(* | /) }

21 Ambiguity in grammars A grammar is ambiguous if and only if it generates a sentential form that has two or more distinct parse trees

22 An ambiguous grammar for arithmetic expressions  | const  / | - const --//

23 Disambiguating the grammar If we use the parse tree to indicate precedence levels of the operators, we can remove the ambiguity. The following rules give / a higher precedence than -  - |  / const| const const / -

24 Links to BNF-style grammars for actual programming languages Below are some links to grammars for real programming languages. Look at how the grammars are expressed. – – In the ones listed below, find the parts of the grammar that deal with operator precedence. – – – Jacques.Levy/poly/mainB/node23.htmlhttp:// Jacques.Levy/poly/mainB/node23.html –

25 Associativity of operators When multiple operators appear in an expression, we need to know how to interpret the expression. Some operators (e.g. +) are associative, meaning that the meaning of an expression with multiple instances of the operator is the same no matter how it is interpreted: (a+b)+c = a+(b+c) Some operators (e.g. -) are not associative: (a-b)-c  a-(b-c)e.g. try a=10, b=8, c=6 (10-8)-6 = -4 but 10-(8-6)=8

26 Associativity of Operators Operator associativity can also be indicated by a grammar -> + | const (ambiguous) -> + const | const (unambiguous) const + +

27 Links to BNF-style grammars for actual programming languages Below are some links to grammars for real programming languages. Look at how the grammars are expressed. – – In the ones listed below, find the parts of the grammar that deal with operator associativity. – – – Jacques.Levy/poly/mainB/node23.htmlhttp:// Jacques.Levy/poly/mainB/node23.html –