CPS 506 Comparative Programming Languages. Syntax Specification

Similar documents
Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Syntax. In Text: Chapter 3

Syntax. A. Bellaachia Page: 1

Syntax Intro and Overview. Syntax

A simple syntax-directed

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

CSE 3302 Programming Languages Lecture 2: Syntax

Part 5 Program Analysis Principles and Techniques

Lecture 4: Syntax Specification

Chapter 3. Describing Syntax and Semantics ISBN

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

CSCI312 Principles of Programming Languages!

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Habanero Extreme Scale Software Research Project

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP 3402 Systems Software Syntax Analysis (Parser)

A programming language requires two major definitions A simple one pass compiler

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units

ECE251 Midterm practice questions, Fall 2010

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

3. Context-free grammars & parsing

Principles of Programming Languages COMP251: Syntax and Grammars

This book is licensed under a Creative Commons Attribution 3.0 License

Describing Syntax and Semantics

CMSC 330: Organization of Programming Languages

EECS 6083 Intro to Parsing Context Free Grammars

Principles of Programming Languages COMP251: Syntax and Grammars

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Chapter 3. Describing Syntax and Semantics

Programming Language Definition. Regular Expressions

A Simple Syntax-Directed Translator

Programming Language Syntax and Analysis

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Week 2: Syntax Specification, Grammars

Dr. D.M. Akbar Hussain

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

1 Lexical Considerations

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

CSCE 314 Programming Languages

Languages and Compilers

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

Last time. What are compilers? Phases of a compiler. Scanner. Parser. Semantic Routines. Optimizer. Code Generation. Sunday, August 29, 2010

CSCI312 Principles of Programming Languages!

PL Revision overview

More Assigned Reading and Exercises on Syntax (for Exam 2)

Chapter 3. Describing Syntax and Semantics ISBN

Defining syntax using CFGs

High Level Languages. Java (Object Oriented) This Course. Jython in Java. Relation. ASP RDF (Horn Clause Deduction, Semantic Web) Dr.

CMSC 330: Organization of Programming Languages

Lecture 10 Parsing 10.1

CMSC 330: Organization of Programming Languages. Context Free Grammars

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Introduction to Parsing

CMSC 330: Organization of Programming Languages. Context Free Grammars

Building Compilers with Phoenix

Chapter 3. Describing Syntax and Semantics

CMSC 330: Organization of Programming Languages

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

CSE302: Compiler Design

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Lexical Considerations

Lexical Analysis. Introduction

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

Lexical and Syntax Analysis. Top-Down Parsing

Chapter 3. Describing Syntax and Semantics

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Homework & Announcements

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

CS321 Languages and Compiler Design I. Winter 2012 Lecture 4

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

Compiler Design Concepts. Syntax Analysis

3. DESCRIBING SYNTAX AND SEMANTICS

Syntax and Grammars 1 / 21

Programming Languages

Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler)

1. Consider the following program in a PCAT-like language.

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CSE 401 Midterm Exam Sample Solution 2/11/15

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

ICOM 4036 Spring 2004

Syntax Analysis Check syntax and construct abstract syntax tree

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

CSE302: Compiler Design

Context-free grammars (CFG s)

Chapter 4. Lexical and Syntax Analysis

Lexical Considerations

Decaf Language Reference Manual

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

Parsing. COMP 520: Compiler Design (4 credits) Professor Laurie Hendren.

Transcription:

CPS 506 Comparative Programming Languages Syntax Specification

Compiling Process Steps Program Lexical Analysis Convert characters into a stream of tokens Lexical Analysis Syntactic Analysis Send tokens to develop an abstract representation or parse tree 2

Compiling Process Steps (con t) Syntactic Analysis Semantic Analysis Send parse tree to analyze for semantic consistency and convert for efficient run in the architecture (Optimization) Semantic Analysis Machine Code Convert abstract t representation ti n to executable machine code using code generation n 3

Meta-Language Formal Methods and Language Processing A language to define other languages BNF (Backus-Naur Form) A set of rewriting rules ρ A set of terminal symbols A set of non-terminal symbols Ν A start symbol S є Ν ρ : Α ω ΑєΝand ωє(ν U Σ) Right-hand hand side: a sequence of terminal and non-terminal symbols Left-hand side: a non-terminal symbol 4

BNF (con t) The words in Ν : grammatical categories or Identifier, Expression, Loop, Program, S : principal grammatical category Symbols in Σ : the basic alphabet Example 1: Example 2: binarydigit 0 binarydigit 1 binarydigit 0 1 Integer Digit Integer Digit Digit 0 1 2 3 4 5 6 7 8 9 5

BNF (con t) Parse Tree Integer Integer Digit Integer Digit 1 Digit 8 Derivation 2 Integer Integer Digit Integer Digit Digit Digit Digit Digit 2 Digit Digit 28 Digit 281 6

BNF (con t) Lexeme: The lowest-level l l syntactic units Tokens : A set of all grammatical categories that define strings of non-blank characters (Lexical Syntax) Identifier (variable names, function names, ) Literal (integer and decimal numbers, ) Operator (+,-,*,/, ) Separator (;,.,(,),{,}, )( ) Keyword (int, if, for, where, ) 7

BNF (con t) Comment Keyword Identifier // comments void main ( ) { float p; p = 3.14 ; } Literal Operator Separator 8

BNF (con t) 9

Regular Expressions An alternative ti for BNF to define a language lexical l rules x : A character abc : A literal string A B : A or B A B : Concatenation of A and B A* : Zero or more occurrence of A A+ : One or more occurrence of A A? : Zero or one occurrence of A [a-z A-Z] : Any alphabetic character [0-9] : Any digit. : Any single character Example Integer : [0-9]+ Identifier : [a-z A-Z][a-z A-Z 0-9]* 10

Syntactic Analysis Primary tool: BNF Input: Tokens from lexical analysis Output: Parse Syntactic categories Program Declaration Assignment Expression Loop Function definition 11

Syntactic Analysis (con t) Example Arithmetic Expression Term Arithmetic Expression + Term Arithmetic ti Expression Term Term Factor Term * Factor Term / Factor Factor Identifier Literal ( Arithmetic Expression ) 12

Syntactic Analysis (con t) Example Arithmetic Expression 2 * a - 3 Term Arithmetic Expression Term * Factor Term Factor Identifier Factor Literal Letter Literal Integer a Integer 2 3 13

Syntactic Analysis (con t) BNF limitations Declaration of identifiers? Initial value of identifiers? In statically typed languages Using Type System for the first problem Detect in compile time or run time 14

Ambiguous Grammar A string is parsed into two or more various trees Example Exp Identifier Literal Exp Exp Input: A B C Output: 1- A (B C) 2- (A B) C Another example is dangling else Using BNF rules Using extra-grammatical rules 15

Operator Precedence <expr> <id> + <expr> <id> * <expr> ( <expr> ) <id> A = B + C * A A = B + (C * A) A = B * C + A A = B * (C + A) Solution <expr> <expr> + <term> <term> <term> <term> * <factor> <factor> <factor> ( <expr> ) <id> A = B + C * A A = B + (C * A) A = B * C + A A = (B * C) + A 16

Associativity of Operators A + B + C A * B * C A / B / C Left Associativity Left Recursive: In a grammar rule, LHS also appears at the beginning of its RHS <expr> <expr> + <term> <term> A + B + C (A + B) + C Right Associativity Right Recursive: In a grammar rule, LHS also appears at the end of its RHS <factor> <exp> ** <factor> <exp> <exp> ( <expr> ) <id> A + B ** C A + (B ** C) 17

Extended BNF (EBNF) Optional part of an RHS <if_stmt> if ( <expr> ) <statement> [ else <statement> ] Repetition, or recursion, part of an RHS <id_list> <id> {, <id_list> } Multiple choice option of an RHS <term> <term> ( * / % ) <factor> Optional use of * and + <id_list> <id> {, <id_list> }* <integer> {0 9}+ 18

Extended BNF (EBNF) (con t) opt subscript Conditional Statement if ( Expr ) Statement { else Statement } opt Syntax Diagram Term Factor * / 19

Case Study A BNF or EBNF for one grammar, such as Expression, different Literals, or if Statement in Java, C, C++, or Pascal BNF or EBNF for floating gpoint numbers in Java, C, C++ BNF or EBNF for loop statements in one language 20

Abstract Syntax Consider the following codes: Pascal C or Java While i < 10 do begin i := i+ 1; end; while (i < 10) { i = i + 1; } Although h syntax are different, they are essentially equivalent Abstract Syntax is a solution to show the essential elements of a language g 21

Abstract Syntax (con t) General Form Abstract Syntax Class = list of essential components n Member Example Loop = Expression test; Statement body Element A Java class for abstract syntax of loop class Loop extends Statement { Expression test; Statement body; } 22

Abstract Syntax (con t) More examples Member Assignment = Variable target; Expression source Element A Java class for abstract syntax of Assignment class Assignment extends Statement { Variable target; Expression source; } 23

Abstract Syntax Tree A tree to show the abstract syntax tree Example x = 2; x := 2; Assignment = Variable target; Expression source Statement Assignment Variable Expression x Vl Value 2 24

Recursive Descent Parser A top-down parser to verify the syntax of a stream of text from left to right It contains several recursive methods, each of which implements a rule of the grammar More details and parsing algorithms in Compiler course 25

Exercises 1. Modify the following grammar to add a unary minus operator that has higher precedence than either + or *. <assign> <id> = <expr> <id> A B C <expr> <expr> + <term> <term> <term> <term> * <factor> <factor> <factor> ( <expr> ) <id> 26

Exercises 2. Consider the following grammar: <S> <A> a <B> b <A> <A> b b <B> a <B> a Which of the following sentences are in the language generated by this grammar? 1. baab 2. bbbab 3. bbaaaaa 4. bbaab 27

Exercises 3. Convert the following EBNF to BNF: S A { ba } A a [b]a 4. Using grammar in question 1, add the ++ and unary operators of Java. 5. Using grammar in question 1, show a parse tree and a leftmost derivation for each of the following statements: a) A = (A+B) * C b) A = B * (C * (A + B)) 28

Exercises 6. Rrewrite the BNF in question 1 to give + precedence over *, and force + to be right associative. 7. Using BNF write an algorithm for the language consisting of strings {ab} n, where n>0, such as ab, aabb,. Can you write this using regular expressions? 29