Programming Languages Third Edition Chapter 6 Syntax.

Slides:



Advertisements
Similar presentations
Introduction To Compilers And Phase 1 Inside a compiler. Inside a C-- compiler. The compilation process. Example C-- code. Extended Backus-Naur.
Advertisements

Compilers and Language Translation
CPSC Compiler Tutorial 9 Review of Compiler.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
PZ02A - Language translation
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
COMP205 Comparative Programming Languages Part 1: Introduction to programming languages Lecture 2: Structure of programs and programming languages as communication.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
ParsingParsing. 2 Front-End: Parser  Checks the stream of words and their parts of speech for grammatical correctness scannerparser source code tokens.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Invitation to Computer Science 5th Edition
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
CSC 338: Compiler design and implementation
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
Programming Languages Third Edition Chapter 6 Syntax.
ProgrammingLanguages Programming Languages Language Syntax This lecture introduces the the lexical structure of programming languages; the context-free.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
Introduction to Compiling
Chapter 1 Introduction Major Data Structures in Compiler
1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Syntax and Grammars.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Chapter 3 – Describing Syntax
Introduction Chapter : Introduction.
Chapter 1 Introduction.
Lexical and Syntax Analysis
A Simple Syntax-Directed Translator
Introduction.
Overview of Compilation The Compiler Front End
Overview of Compilation The Compiler Front End
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Chapter 1 Introduction.
PROGRAMMING LANGUAGES
Compiler Lecture 1 CS510.
CSE 3302 Programming Languages
Compiler Designs and Constructions
Introduction CI612 Compiler Design CI612 Compiler Design.
CPSC 388 – Compiler Design and Construction
Compilers B V Sai Aravind (11CS10008).
Lecture 4: Lexical Analysis & Chomsky Hierarchy
CMPE 152: Compiler Design August 21/23 Lab
Programming Languages 2nd edition Tucker and Noonan
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
High-Level Programming Language
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Chapter 10: Compilers and Language Translation
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Introduction Chapter : Introduction.
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 2, 09/04/2003 Prof. Roy Levow.
Faculty of Computer Science and Information System
Presentation transcript:

Programming Languages Third Edition Chapter 6 Syntax

Objectives Understand the lexical structure of programming languages Understand context-free grammars and BNFs Become familiar with parse trees Understand ambiguity, associativity, and precedence Read Sections 6.1 – 6.4, pp Programming Languages, Third Edition2

Introduction Syntax is the structure of a language Syntax rules are analogous to the grammar rules of a natural language John Backus and Peter Naur developed a notational system for describing these grammars, now called Backus-Naur forms, or BNFs –First used to describe the syntax of Algol60 Every modern computer scientist needs to know how to read, interpret, and apply BNF descriptions of language syntax Programming Languages, Third Edition3

4 Source Code (your program) Object Code (machine language) Compiler Flowchart for Compilation

Programming Languages, Third Edition5 Source Code (your program = char stream) Object Code (machine language) Scanner (lexical analysis) Flowchart for Compilation - Details Lexical items / Tokens Parser (syntactic analysis) Parse tree Intermediate Code Semantic analysis (analyses meaning) Optimization

Lexical Structure of Programming Languages Lexical structure: the structure of the tokens, or words, of a language –Related to, but different than, the syntactic structure Scanning phase: the phase in which a translator collects sequences of characters from the input program and forms them into tokens Parsing phase: the phase in which the translator processes the tokens, determining the program’s syntactic structure Programming Languages, Third Edition6

Lexical Structure of Programming Languages (cont’d.) Tokens generally fall into several categories: –Reserved words (or keywords) –Literals or constants –Special symbols, such as “;” “<=“ “+” –Identifiers Programming Languages, Third Edition7

Lexical Structure of Programming Languages (cont’d.) Token delimiters (or white space): formatting that affects the way tokens are recognized Indentation can be used to determine structure Free-format language: one in which format has no effect on program structure other than satisfying the principle of longest substring Fixed format language: one in which all tokens must occur in prespecified locations on the page Tokens can be formally described by regular expressions Programming Languages, Third Edition8

Scanning Regular Expressions Metalanguage for describing patterns for strings of characters – metasymbols are | means choice * means zero or more occurrences + means one of more occurrences ? means one optional occurrence [ ] choose one of list of chars in brackets can use a range. (period) means one of any character ( ) can be used for grouping \ can precede metasymbol with this to use metasymbol in string Programming Languages, Third Edition9

Regular Expressions (cont’d.) Most modern text editors use regular expressions in text searches Utilities such as lex can automatically turn a regular expression description of a language’s tokens into a scanner Programming Languages, Third Edition10

Regular Expressions (cont’d.) Examples: (a|b)*c [ab]*c [aeiou] [aeiouAEIOU] [aeiouAEIOU]+ [aeiouAEIOU]* [A-Z][a-z]* [A-Z]+[a-z] [A-Za-z]* Programming Languages, Third Edition11

Regular Expressions (cont’d.) Examples: [0-9]+ [0-9]+(\.[0-9]+) Can test by making text file on Unix and using egrep –x “pattern” filename Programming Languages, Third Edition12

Regular Expressions (cont’d.) Let’s try writing some: –Signed integers, sign not optional –Signed integers, sign optional –Signed integers, sign optional, no signed zero –Signed integers, allow leading zeros, but no signed zero Programming Languages, Third Edition13

Regular Expressions (cont’d.) Let’s try writing some for license plates: –Start with VA, followed by zero or more digits –Start with VA, followed by one or more digits –Start with VA, followed by 2 digits, followed by zero or more lower case letters –Start with V or A, followed by -, followed by 2-4 digits –Start with VA, any case, followed by 2-3 digits or 2-3 letters (tried in class) –Start with VA, any case, followed by 2-3 digits, followed by 2-3 letters (meant to try) Programming Languages, Third Edition14

Parsing Context-Free Grammars and BNFs Context-free grammar: consists of –a series of grammar rules (Productions) Each rule has a single phrase structure name on the left, then a  metasymbol, followed by a sequence of symbols or other phrase structure names on the right –Nonterminals: names for phrase structures, since they are broken down into further phrase structures –Start symbol: one of the Nonterminals –Terminals: words or token symbols that cannot be broken down further Programming Languages, Third Edition15

Example 1: Unsigned Integers ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Terminals: 0, 1, …, 9 Nonterminals:, Start Symbol: Productions: there are 12 Metasymbols: “::=“, “|” Programming Languages, Third Edition16

Example 1 (cont’d) Derivation: the process of building in a language by beginning with the start symbol and replacing left-hand sides by choices of right-hand sides in the rules Let’s derive the number 123 (on board) Parse tree: graphical depiction of the replacement process in a derivation Let’s draw parse tree for 123 (on board) Programming Languages, Third Edition17

Example 1 (cont’d) Notice recursion in one of rules Notice recursive symbol is on left This is a left-recursive grammar This is a left-associative grammar Notice how parse tree cascades to left Programming Languages, Third Edition18

Example 2: Unsigned Integers ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Only made one change, so now grammar is Right-recursive Right-associative Let’s draw parse tree for 123 Programming Languages, Third Edition19

Ex 3: Simple Expression Grammar ::= + | * | ( ) | ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Let’s derive parse tree for: Programming Languages, Third Edition20

Ex 3 (cont’d) ::= + | * | ( ) | ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Is there another parse tree for: Programming Languages, Third Edition21

Ex 3 (cont’d) ::= + | * | ( ) | ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 A grammar is ambiguous if there are two parse trees for the same string Programming Languages, Third Edition22

Ex 3 (cont’d) ::= + | * | ( ) | ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Ambiguity is undesirable (but sometimes unavoidable) Let’s see why it’s undesirable: Derive parse trees for * 5 Programming Languages, Third Edition23

Ex 3 (cont’d) ::= + | * | ( ) | ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 So what was the problem? Which tree provides correct arithmetic interpretation? Programming Languages, Third Edition24

Ex 3 (cont’d) Can we modify the grammar to “fix” the problem? YES! Add more levels of productions: ::= + | ::= * | ::= ( ) | ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Programming Languages, Third Edition25

Ex 3 (cont’d) ::= + | ::= * | ::= ( ) | ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Redraw parse trees for and * 5 Programming Languages, Third Edition26

Chapter 6 Final Thoughts A grammar is context-free when nonterminals appear singly on the left sides of productions –There is no context under which only certain replacements can occur Anything not expressible using context-free grammars is a semantic, not a syntactic, issue BNF form of language syntax makes it easier to write translators Parsing stage can be automated Programming Languages, Third Edition27

Chapter 6 Final Thoughts Syntax establishes structure, not meaning –But meaning is related to syntax Syntax-directed semantics: process of associating the semantics of a construct to its syntactic structure –Must construct the syntax so that it reflects the semantics to be attached later Programming Languages, Third Edition28