CS 326 Programming Languages, Concepts and Implementation

Slides:



Advertisements
Similar presentations
Chapter 5: Languages and Grammar 1 Compiler Designs and Constructions ( Page ) Chapter 5: Languages and Grammar Objectives: Definition of Languages.
Advertisements

Chapter 2 Syntax. Syntax The syntax of a programming language specifies the structure of the language The lexical structure specifies how words can be.
ISBN Chapter 3 Describing Syntax and Semantics.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Chapter 3: Formal Translation Models
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 4: Syntax Specification COMP 144 Programming Language Concepts Spring 2002 Felix.
COP4020 Programming Languages
(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,
Software II: Principles of Programming Languages
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
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.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
1 Syntax Specification (Sections ) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos at UNC.
COP4020 Programming Languages Syntax Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
CPS 506 Comparative Programming Languages Syntax Specification.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 4.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
CSE 425: Syntax I Syntax and Semantics Syntax gives the structure of statements in a language –Allowed ordering, nesting, repetition, omission of symbols.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 3.
Mid-Terms Exam Scope and Introduction. Format Grades: 100 points -> 20% in the final grade Multiple Choice Questions –8 questions, 7 points each Short.
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 will be out this evening Due Monday, 2/8 Submit in HW Server AND at start of class on 2/8 A review.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Chapter 2. Formal Languages Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Last Chapter Review Source code characters combination lexemes tokens pattern Non-Formalization Description Formalization Description Regular Expression.
CS 3304 Comparative Languages
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Chapter 2 :: Programming Language Syntax
Chapter 3 Context-Free Grammar and Parsing
Chapter 3 – Describing Syntax
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
What does it mean? Notes from Robert Sebesta Programming Languages
PROGRAMMING LANGUAGES
Formal Language Theory
CSE 3302 Programming Languages
Compiler Design 4. Language Grammars
Programming Language Syntax 2
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
COP4020 Programming Languages
Chapter 2: A Simple One Pass Compiler
Programming Languages
R.Rajkumar Asst.Professor CSE
Lecture 4: Lexical Analysis & Chomsky Hierarchy
CS 3304 Comparative Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
BNF 9-Apr-19.
High-Level Programming Language
Programming Languages 2nd edition Tucker and Noonan
COMPILER CONSTRUCTION
Faculty of Computer Science and Information System
Presentation transcript:

CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 3 Good afternoon and thank you everyone for coming. My talk today will describe the research I performed at the IRIS at USC, the object of this work being to build a computational framework that addresses the problem of motion analysis and interpretation.

Classification Chomsky hierarchy (incomplete): Language Generator Acceptor Compile phase Regular Regular grammar Finite automaton Lexical analysis (scanning) Context-free Context-free grammar Push-down automaton Syntax analysis (parsing) Regular languages are a subset of context-free ones

Specifying Syntax Define the structure of a language Interest for programmers, to write syntactically valid programs Will discuss: regular expressions (equivalent to regular grammars) context-free grammars

Regular Expressions Define what tokens are valid in a programming language The set of all valid tokens  a formal language that is regular described using regular expressions Tokens (Pascal): symbols: + - ; := keywords: begin end while if integer literals: 141 real literals: 5.38e25 string literals: 'Tom' identifiers: myVariable

Regular Expressions Variations: uppercase / lowercase distinction (C vs Pascal) keywords must be lowercase (C vs Modula-3) free format: white space is ignored, only the relative order of tokens counts, not position in page exceptions: fixed format, 72 characters per line, special purpose columns (Fortran < 90) line-breaks separate statements (Basic) indentation matters (Haskell, Occam)

Regular Expressions Example: natural numbers Or better: digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 non_zero_digit = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 natural_number = non_zero_digit digit* Or better: non_zero_digit = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 digit = 0 | non_zero_digit natural_number = non_zero_digit digit*

Regular Expressions Example: numeric literals (Pascal) digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 unsigned_int = digit digit* unsigned_number = unsigned_int (( . unsigned_int ) | ε ) (( e ( + | - | ε ) unsigned_int ) | ε ) number = ( + | - | ε ) unsigned_number

Regular Expressions Definition: A regular expression R is either: a character the empty string ε R1 R2 (concatenation) R1 | R2 (alternation) R1* (repetition zero or more times - Kleene closure) Also used: R+ (repetition one or more times) ↔ Note: no recursion allowed, if it has recursion it is not regular R R*

Regular Expressions Language: Regular expression: Language: set of strings over alphabet {a,b} that contain at least one b Regular expression: ( a | b )* b ( a | b )* Language: set of all Social Security Numbers, including the separator – Regular expression: (0|1|2|3|4|5|6|7|8|9)3 – (0|1|2|3|4|5|6|7|8|9)2 – (0|1|2|3|4|5|6|7|8|9)4

Regular Expressions Regular expression: Language: Regular expression: ( 0 | 1 )* 0 0 Language: set of all strings over alphabet {0,1} that end in 00 Regular expression: ( a | b )* a ( a | b )* a ( a | b )* Language: set of all strings over alphabet {a,b} that contain at least two a’s

Context-Free Grammars Language: set of strings over alphabet {a,b} that read the same from left to right as from right to left (palindromes) Grammar: S → a S a | b S b | a | b | ε

Context-Free Grammars Example: arithmetic expression expression → identifier | number | - expression | ( expression ) | expression operator expression operator → + | - | * | / nonterminals: expression, operator terminals: identifier, number, +, -, *, /, (, ) start symbol: expression

Derivation and Parse Trees Generate "slope * x + intercept" expression => expression operator expression => expression operator identifier => expression + identifier => expression operator expression + identifier => expression operator identifier + identifier => expression * identifier + identifier => identifier * identifier + identifier (slope) (x) (intercept)

Derivation and Parse Trees expression => expression operator expression => expression operator identifier => expression + identifier => expression operator expression + identifier => expression operator identifier + identifier => expression * identifier + identifier => identifier * identifier + identifier (slope) (x) (intercept) Derivation – the series of replacements Sentential form – any intermediate string of symbols Yield – the final sentential form, with only terminals Right-most / left-most derivation – strategy on what nonterminal to expand

Derivation and Parse Trees Another parse tree:

Derivation and Parse Trees Issues: ambiguity: more than one parse tree for any given CF language - infinitely many CF grammars avoid ambiguous ones reflect useful properties: associativity: 10-4-3 means (10-4)-3 precedence: 3+4*5 means 3+(4*5)

Derivation and Parse Trees A new grammar for arithmetic expressions: expression → term | expression add_op term term → factor | term mult_op factor factor → identifier | number | - factor | ( expression ) add_op → + | - mult_op → * | / Unambiguous, also captures associativity and precedence

Derivation and Parse Trees Precedence: 3 + 4 * 5

Derivation and Parse Trees Associativity: 10 - 4 - 3

Announcements Readings Homework Rest of Chapter 2, up to (and including) 2.2.3 Homework HW 1 out – due on September 14 Submission at the beginning of class with a title page: Name, Class, Assignment #, Date everything stapled together preferably typed