CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 3.

Slides:



Advertisements
Similar presentations
Chapter 2 Syntax. Syntax The syntax of a programming language specifies the structure of the language The lexical structure specifies how words can be.
Advertisements

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.
Context-Free Grammars Lecture 7
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 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.
Chapter 3: Formal Translation Models
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 4: Syntax Specification COMP 144 Programming Language Concepts Spring 2002 Felix.
COP4020 Programming Languages
1 Context-Free Languages. 2 Regular Languages 3 Context-Free 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,
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Software II: Principles of Programming Languages
Compiler Principle and Technology Prof. Dongming LU Mar. 7th, 2014.
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.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Grammars CPSC 5135.
PART I: overview material
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.
Chapter 3 Describing Syntax and Semantics
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.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 4.
ISBN Chapter 3 Describing Syntax and Semantics.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
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.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
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.
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.
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 3 – Describing Syntax
CSE 3302 Programming Languages
Programming Language Syntax 2
COP4020 Programming Languages
Programming Languages
Lecture 4: Lexical Analysis & Chomsky Hierarchy
CS 3304 Comparative Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
COMPILER CONSTRUCTION
Presentation transcript:

CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 3

22/69 Classification Chomsky hierarchy (incomplete): LanguageGeneratorAcceptorCompile 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

33/69 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

44/69 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

55/69 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)

66/69 Regular Expressions Example: natural numbers 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*

77/69 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

88/69 Regular Expressions Definition: −A regular expression R is either: −a character −the empty string ε −R 1 R 2 (concatenation) −R 1 | R 2 (alternation) −R 1 * (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*

99/69 Regular Expressions 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

1010/69 Regular Expressions 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

1111/69 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 | ε

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

1313/69 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)

1414/69 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

1515/69 Derivation and Parse Trees Parse tree: Another parse tree:

1616/69 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: means (10-4)-3 −precedence: 3+4*5 means 3+(4*5)

1717/69 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

1818/69 Derivation and Parse Trees Precedence: * 5

1919/69 Derivation and Parse Trees Associativity:

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