1 CS Programming Languages Class 04 September 5, 2000
CS 403, Class 04 Slide # 2 Today’s Agenda Take up Homework on ‘printf’ Finish Chapter 2 Chapter 3: Syntax
CS 403, Class 04 Slide # 3 Homework 1 Take up homework 1: C ‘printf’ program & discussion.
CS 403, Class 04 Slide # 4 C (Finish Ch 2) Developed at Bell Labs by Stroustrup Evolved from C and SIMULA 67 Facilities for object-oriented programming, taken partially from SIMULA 67, were added to C Also has exception handling
CS 403, Class 04 Slide # 5 C++ (2) A large and complex language, in part because it supports both procedural and OO programming Rapidly grew in popularity, along with OOP ANSI standard approved in November, 1997
CS 403, Class 04 Slide # 6 Java Developed at Sun in the early 1990s Based on C++ Significantly simplified Supports only OOP Has references, but not pointers Includes support for applets and a form of concurrency
CS 403, Class 04 Slide # 7 Chapter 3: Syntax & Semantics Syntax: the form or structure of the expressions statements, and program units Semantics : the meaning of the expressions, statements, and program units Who needs language definitions? Other language designers Implementers (compiler writers) Programmers (users of the language)
CS 403, Class 04 Slide # 8 Basic Terms Sentence: a string of characters over some alphabet Language: 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)
CS 403, Class 04 Slide # 9 Formal Approaches to Syntax Recognizers - first part of any compiler Generators - what we'll study
CS 403, Class 04 Slide # 10 Context-Free Grammars (CFGs) Developed by Noam Chomsky in the mid-1950s. Language generators, meant to describe the syntax of natural languages. Define a class of languages called context-free languages (CFLs).
CS 403, Class 04 Slide # 11 Backus Normal Form (BNF) Backus Normal Form (1959) Invented by John Backus to describe Algol 58 BNF is equivalent to context-free grammars (Also called “Backus-Naur Form”)
CS 403, Class 04 Slide # 12 CFGs/CFLs and BNF A metalanguage is a language used to describe another language. In BNF, abstractions are used to represent classes of syntactic structures--they act like syntactic variables (also called nonterminal symbols.
CS 403, Class 04 Slide # 13 BNF Rules while do This is a rule; it describes the structure of a while statement. 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.
CS 403, Class 04 Slide # 14 BNF Rules (2) An abstraction (or nonterminal symbol) can have more than one RHS | begin end Syntactic lists are described in BNF using recursion. ident | ident,
CS 403, Class 04 Slide # 15 BNF Derivation A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols).
CS 403, Class 04 Slide # 16 An Example Grammar | ; = a | b | c | d + | - | const
CS 403, Class 04 Slide # 17 An Example Derivation = a = a = + a = b + a = b + const
CS 403, Class 04 Slide # 18 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 may be neither leftmost nor rightmost.
CS 403, Class 04 Slide # 19 Parse Tree A parse tree is a hierarchical representation of a derivation. = a + const b
CS 403, Class 04 Slide # 20 Exercise Given the following grammar, draw a parse tree for this expression: x = y + 2
CS 403, Class 04 Slide # 21 Exercise Grammar = | + | 1 | 2 | 3 | … | 9 | 0 a | b | c | … | x | y | z
CS 403, Class 04 Slide # 22 Question: For any programming language you know (Ada, Pascal, C++, assembler, …) Is it a context-free language? Does it have an ambiguous grammar?