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

Similar documents
Syntax. In Text: Chapter 3

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

Describing Syntax and Semantics

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

Chapter 3. Describing Syntax and Semantics ISBN

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

Programming Language Syntax and Analysis

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

ICOM 4036 Spring 2004

Habanero Extreme Scale Software Research Project

Syntax and Semantics

Syntax. A. Bellaachia Page: 1

Programming Language Definition. Regular Expressions

3. Context-free grammars & parsing

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

3. DESCRIBING SYNTAX AND SEMANTICS

Programming Languages

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

CPS 506 Comparative Programming Languages. Syntax Specification

COP 3402 Systems Software Syntax Analysis (Parser)

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

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

Syntax & Semantics. COS 301 Programming Languages

EECS 6083 Intro to Parsing Context Free Grammars

Dr. D.M. Akbar Hussain

Principles of Programming Languages COMP251: Syntax and Grammars

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

CS 230 Programming Languages

Part 5 Program Analysis Principles and Techniques

Homework & Announcements

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

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

CSCI312 Principles of Programming Languages!

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

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

CSE302: Compiler Design

CS 314 Principles of Programming Languages

Compiler Design Concepts. Syntax Analysis

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

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

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

A simple syntax-directed

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Syntax Analysis Check syntax and construct abstract syntax tree

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

Chapter 4. Lexical and Syntax Analysis

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

CMSC 330: Organization of Programming Languages

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

Theory and Compiling COMP360

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

Parsing II Top-down parsing. Comp 412

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Formal Languages. Formal Languages

CSE 3302 Programming Languages Lecture 2: Syntax

Syntax Intro and Overview. Syntax

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

CMSC 330: Organization of Programming Languages

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

This book is licensed under a Creative Commons Attribution 3.0 License

Lexical Scanning COMP360

Principles of Programming Languages COMP251: Syntax and Grammars

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

CMSC 330: Organization of Programming Languages. Context Free Grammars

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

Programming Languages, Summary CSC419; Odelia Schwartz

Introduction to Parsing

4. Lexical and Syntax Analysis

Building Compilers with Phoenix

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

CMSC 330: Organization of Programming Languages. Context Free Grammars

4. Lexical and Syntax Analysis

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

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

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

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

CMSC 330: Organization of Programming Languages. Context Free Grammars

CSCE 314 Programming Languages

Lecture 10 Parsing 10.1

Lexical and Syntax Analysis. Top-Down Parsing

Syntax. 2.1 Terminology

Lecture 4: Syntax Specification

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

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

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

B The SLLGEN Parsing System

Context-free grammars (CFG s)

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

CMSC 330: Organization of Programming Languages

CSCI312 Principles of Programming Languages!

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

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

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

Transcription:

Syntax - the form or structure of the expressions, statements, and program units Semantics - the meaning of the expressions, statements, and program units Who must use language definitions? 1. Other language designers 2. Implementors 3. Programmers (the users of the language) Lexical Structure of Programming Languages A language is a set of sentences A sentence is a set of lexemes A lexeme is a set of character (e.g., *, sum, begin) A token is a category of lexemes Reserved word or keyword such as if, while.. Literal or constants such as 23 (numeric literal)), hello (a string literal) Special symbols such as ;, <, identifier CSCI325 Concepts of Programming Languages Dr Ahmed Rafea 1

Context-Free Grammars - Developed by Noam Chomsky in the mid-1950s - Language generators, meant to describe the syntax of natural languages - Define a class of languages called context-free languages Backus Normal Form (1959) - Invented by John Backus to describe Algol 58 - BNF is equivalent to context-free grammars A metalanguage is a language used to describe another language. In BNF, abstractions are used to represent classes of syntactic structures--they act like syntactic variables (also called nonterminal symbols) e.g. <while_stmt> -> while <logic_expr> do <stmt> This is a rule; it describes the structure of a while statement CSCI325 Concepts of Programming Languages Dr Ahmed Rafea 2

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 in BNF using recursion <ident_list> -> ident ident, <ident_list> A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols) CSCI325 Concepts of Programming Languages Dr Ahmed Rafea 3

An example grammar: <program> -> <stmts> <stmts> -> <stmt> <stmt> ; <stmts> <stmt> -> <var> = <var> -> a b c d -> <term> + <term> <term> - <term> <term> -> <var> const An example derivation: <program> => <stmts> => <stmt> => <var> = => a = => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const 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 A derivation may be neither leftmost nor rightmost CSCI325 Concepts of Programming Languages Dr Ahmed Rafea 4

A parse tree is a hierarchical representation of a derivation <program> <stmts> <stmt> <var> = a <term> + <term> <var> const b A grammar is ambiguous iff it generates a sentential form that has two or more distinct parse trees CSCI325 Concepts of Programming Languages Dr Ahmed Rafea 5

An ambiguous expression grammar: -> <op> const <op> -> / - <op> <op> <op> <op> const - const / const const - const / const If we use the parse tree to indicate precedence levels of the operators, we cannot have ambiguity An unambiguous expression grammar: -> - <term> <term> <term> -> <term> / const const - <term> <term> <term> / const const const CSCI325 Concepts of Programming Languages Dr Ahmed Rafea 6

=> - <term> => <term> - <term> => const - <term> => const - <term> / const => const - const / const Operator associativity can also be indicated by a grammar -> + const (ambiguous) -> + const const (unambiguous) + const + const const Extended BNF (just abbreviations): 1. Optional parts are placed in brackets ([]) <proc_call> -> ident [ ( <expr_list>)] 2. Put alternative parts of RHSs in parentheses and separate them with vertical bars <term> -> <term> (+ -) const 3. Put repetitions (0 or more) in braces ({}) <ident> -> letter {letter digit} CSCI325 Concepts of Programming Languages Dr Ahmed Rafea 7

BNF: -> + <term> - <term> <term> <term> -> <term> * <factor> <term> / <factor> <factor> EBNF: -> <term> {(+ -) <term>} <term> -> <factor> {(* /) <factor>} Syntax Graphs - put the terminals in circles or ellipses and put the nonterminals in rectangles; connect with lines with arrowheads e.g., Pascal type declarations type_identifier ( identifier ), constant.. constant CSCI325 Concepts of Programming Languages Dr Ahmed Rafea 8