Syntax. In Text: Chapter 3

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

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

Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics ISBN

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

Programming Language Syntax and Analysis

Chapter 3. Describing Syntax and Semantics

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

ICOM 4036 Spring 2004

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Syntax and Semantics

Syntax. A. Bellaachia Page: 1

Programming Language Definition. Regular Expressions

Habanero Extreme Scale Software Research Project

Chapter 3. Describing Syntax and Semantics. Introduction. Why and How. Syntax Overview

3. DESCRIBING SYNTAX AND SEMANTICS

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

COP 3402 Systems Software Syntax Analysis (Parser)

CPS 506 Comparative Programming Languages. Syntax Specification

EECS 6083 Intro to Parsing Context Free Grammars

Programming Languages

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

Chapter 3. Topics. Languages. Formal Definition of Languages. BNF and Context-Free Grammars. Grammar 2/4/2019

Homework & Announcements

3. Context-free grammars & parsing

CS 230 Programming Languages

Chapter 4. Lexical and Syntax Analysis

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

Dr. D.M. Akbar Hussain

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

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Introduction to Parsing

Principles of Programming Languages COMP251: Syntax and Grammars

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

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

4. Lexical and Syntax Analysis

Theory and Compiling COMP360

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

Lexical and Syntax Analysis

CMSC 330: Organization of Programming Languages

CSE 3302 Programming Languages Lecture 2: Syntax

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

4. Lexical and Syntax Analysis

Parsing II Top-down parsing. Comp 412

P L. rogramming anguages. Fall COS 301 Programming Languages. Syntax & Semantics. UMaine School of Computing and Information Science

Syntax & Semantics. COS 301 Programming Languages

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

CMSC 330: Organization of Programming Languages

CSE302: Compiler Design

EDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised:

CMSC 330: Organization of Programming Languages. Context Free Grammars

Building Compilers with Phoenix

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

Formal Languages. Formal Languages

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

Part 5 Program Analysis Principles and Techniques

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages

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

Lexical Scanning COMP360

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

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

CS 314 Principles of Programming Languages

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

CSCI312 Principles of Programming Languages!

CMSC 330: Organization of Programming Languages. Context Free Grammars

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

CSCE 314 Programming Languages

Introduction. Introduction. Introduction. Lexical Analysis. Lexical Analysis 4/2/2019. Chapter 4. Lexical and Syntax Analysis.

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

Context-free grammars (CFG s)

Lecture 10 Parsing 10.1

10/5/17. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntax Analysis

10/4/18. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntactic Analysis

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

CS 406/534 Compiler Construction Parsing Part I

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

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

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

Lexical and Syntax Analysis. Top-Down Parsing

Defining syntax using CFGs

Syntax Intro and Overview. Syntax

Lexical and Syntax Analysis (2)

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

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

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Languages and Compilers

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Principles of Programming Languages COMP251: Syntax and Grammars

This book is licensed under a Creative Commons Attribution 3.0 License

Syntax Analysis Check syntax and construct abstract syntax tree

Introduction to Lexing and Parsing

Context-Free Grammar (CFG)

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Transcription:

Syntax In Text: Chapter 3 1

Outline Syntax: Recognizer vs. generator BNF EBNF Chapter 3: Syntax and Semantics 2

Basic Definitions Syntax the form or structure of the expressions, statements, and program units Semantics the meaning of the expressions, statements, and program units Why write a language definition; who will use it? Other language designers Implementors (compiler writers) Programmers (the users of the language) Chapter 3: Syntax and Semantics 3

What is a Language? A sentence is a string of characters over some alphabet A language is 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) Chapter 3: Syntax and Semantics 4

Recognizers vs. Generators We don t want to use English to describe a language (too long, tedious, imprecise), so There are two formal approaches to describing syntax: Recognizers Given a string, a recognizer for a language L tells whether or not the string is in L (ex: Compiler) Generators A generator for L will produce an arbitrary string in L on demand. (ex: Grammar, BNF) Recognition and generation are useful for different things, but are closely related Chapter 3: Syntax and Semantics 5

Grammars Developed by Noam Chomsky in the mid- 1950s 4-level hierarchy (0-3) Language generators, meant to describe the syntax of natural languages Context-free grammars define a class of languages called context-free languages (level 2) Chapter 3: Syntax and Semantics 6

Backus-Naur Form Invented by John Backus and Peter Naur to describe syntax of Algol 58/60 BNF is equivalent to context-free grammars A metalanguage: a language used to describe another language Chapter 3: Syntax and Semantics 7

BNF Nonterminals In BNF, abstractions are used to represent classes of syntactic structures they act like syntactic variables (also called nonterminal symbols) <while_stmt> -> while <logic_expr> do <stmt> This is a rule; it describes the structure of a while statement Chapter 3: Syntax and Semantics 8

BNF Rules 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 An abstraction (or nonterminal symbol) can have more than one RHS: <stmt> -> <single_stmt> begin <stmt_list> end Syntactic lists are described using recursion: <ident_list> -> ident ident, <ident_list> Chapter 3: Syntax and Semantics 9

An Example Grammar <program> -> <stmts> <stmts> -> <stmt> <stmt> ; <stmts> <stmt> -> <var> = <expr> <var> -> a b c d <expr> -> <term> + <term> <term> - <term> <term> -> <var> const Chapter 3: Syntax and Semantics 10

Derivations A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols): <program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const Chapter 3: Syntax and Semantics 11

Sentential Forms 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 next in the derivation A rightmost derivation works right to left instead Some derivations are neither leftmost nor rightmost Chapter 3: Syntax and Semantics 12

Parse Trees A parse tree is a hierarchical representation of a derivation A grammar is ambiguous iff it generates a sentential form that has two or more distinct parse trees <program> <stmts> <stmt> <var> = <expr> a <term> + <term> <var> const b Chapter 3: Syntax and Semantics 13

Ambigous Grammars An ambiguous expression grammar: <expr> -> <expr> <op> <expr> const <op> -> / - <expr> <expr> <expr> <op> <expr> <expr> <op> <expr> <expr><op><expr> <expr><op><expr> const - const / const const - const / const Chapter 3: Syntax and Semantics 14

Indicating Precedence If we use the parse tree to indicate precedence levels of the operators, we cannot have ambiguity: <expr> -> <expr> - <term> <term> <term> -> <term> / const cons <expr> => <expr> - <term> => <term> - <term> => const - <term> => const - <term> / const => const - const / const <expr> <expr> - <term> <term> <term> / const const const Chapter 3: Syntax and Semantics 15

Operator Associativity Operator associativity can also be indicated by a grammar <expr> -> <expr> + <expr> const (ambiguous) <expr> -> <expr> + const const (unambiguous) <expr> <expr> + const <expr> + const const Chapter 3: Syntax and Semantics 16

Extended BNF (EBNF) Optional parts are placed in brackets ([]) <proc_call> -> ident [ (<expr_list>) ] Put alternative parts of RHS in parentheses and separate them with vertical bars <term> -> <term> (+ -) const Put repetitions (0 or more) in braces ({}) <ident> -> letter {letter digit} Chapter 3: Syntax and Semantics 17

BNF and EBNF Side by Side BNF: <expr> -> <expr> + <term> <expr> - <term> <term> <term> -> <term> * <factor> <term> / <factor> <factor> EBNF: <expr> -> <term> {(+ -) <term>} <term> -> <factor> {(* /) <factor> Chapter 3: Syntax and Semantics 18

Recursive Descent Parsing Parsing is the process of tracing or constructing a parse tree for a given input string Parsers usually do not analyze lexemes; that is done by a lexical analyzer, which is called by the parser A recursive descent parser traces out a parse tree in topdown order; it is a top-down parser Each nonterminal in the grammar has a subprogram associated with it; the subprogram parses all sentential forms that the nonterminal can generate The recursive descent parsing subprograms are built directly from the grammar rules Recursive descent parsers, like other top-down parsers, cannot be built from left-recursive grammars Chapter 3: Syntax and Semantics 19

Recursive Descent Example Example: For the grammar: <term> -> <factor> {(* /) <factor>} Simple recursive descent parsing subprogram: void term() { factor(); /* parse the first factor*/ while (next_token == ast_code next_token == slash_code) { lexical(); /* get next token */ factor(); /* parse the next factor */ } } Chapter 3: Syntax and Semantics 20