Habanero Extreme Scale Software Research Project

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

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

Describing Syntax and Semantics

Syntax. In Text: Chapter 3

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics ISBN

3. Context-free grammars & parsing

Principles of Programming Languages COMP251: Syntax and Grammars

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

CPS 506 Comparative Programming Languages. Syntax Specification

Programming Language Syntax and Analysis

EECS 6083 Intro to Parsing Context Free Grammars

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

Dr. D.M. Akbar Hussain

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

COP 3402 Systems Software Syntax Analysis (Parser)

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

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

Principles of Programming Languages COMP251: Syntax and Grammars

Syntax. A. Bellaachia Page: 1

Introduction to Parsing

Syntax Analysis Check syntax and construct abstract syntax tree

Homework & Announcements

Defining syntax using CFGs

Programming Language Definition. Regular Expressions

Part 5 Program Analysis Principles and Techniques

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CS 314 Principles of Programming Languages

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

Programming Languages

Syntax and Semantics

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

Parsing II Top-down parsing. Comp 412

Syntax Intro and Overview. Syntax

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

3. DESCRIBING SYNTAX AND SEMANTICS

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

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

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

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

Context-Free Grammars

ICOM 4036 Spring 2004

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

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

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

CSE 3302 Programming Languages Lecture 2: Syntax

Compiler Design Concepts. Syntax Analysis

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

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

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.

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Context Free Grammars

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

CMSC 330: Organization of Programming Languages

CSCI312 Principles of Programming Languages!

This book is licensed under a Creative Commons Attribution 3.0 License

CMPT 755 Compilers. Anoop Sarkar.

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

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

Syntax & Semantics. COS 301 Programming Languages

Context-Free Grammar (CFG)

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

CSE302: Compiler Design

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

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

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

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

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

Languages and Compilers

Introduction to Lexing and Parsing

Defining Languages GMU

CS 406/534 Compiler Construction Parsing Part I

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

Formal Languages. Formal Languages

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

DEMO A Language for Practice Implementation Comp 506, Spring 2018

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CMSC 330: Organization of Programming Languages. Context Free Grammars

Context-free grammars (CFG s)

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

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

Theory and Compiling COMP360

CS 403: Scanning and Parsing

Chapter 4. Lexical and Syntax Analysis

3. Parsing. Oscar Nierstrasz

Part 3. Syntax analysis. Syntax analysis 96

Transcription:

Habanero Extreme Scale Software Research Project

Comp215: Grammars Zoran Budimlić (Rice University)

Grammar, which knows how to control even kings - Moliere

So you know everything about regular expressions Are regular expressions sufficient for describing syntax? Example: ((a+b) * c) Wouldn t it be great if we had a name for this? Can you write a regular expression to recognize something like this? Why not? How about this: a n b n, n = 0, 1, 2,

Grammars to the rescue! We use Context-Free Grammars (CFGs) to specify context-free syntax. A CFG describes how a sentence of a language may be generated. Example: <EvilLaugh> ::= mwa <EvilCackle> <EvilCackle> ::= ha <EvilCackle> <EvilCackle> ::= ha! Use this grammar to generate the sentence mwa ha ha ha!

Some terminology Syntax Colorless green ideas sleep furiously. (Noam Chomsky) the form or structure of the expressions, statements, and program units Semantics the meaning of the expressions, statements, and program units Sentence a string of characters over some alphabet Language a set of sentences Lexeme the lowest level syntactic unit of a language :=, {, while Token a category of lexemes (e.g., identifier)

More terminology terminal symbols atomic components of statements in the language appear in source programs identifiers, operators, punctuation, keywords nonterminal symbols intermediate elements in producing terminal symbols never appear in source program start (or goal) symbol a special nonterminal which is the starting symbol for producing statements

Productions rules for transforming nonterminal symbols into terminals or other nonterminals nonterminal ::= terminals and/or nonterminals each has lefthand side (LHS) and righthand side (RHS) every nonterminal must appear on LHS of at least one production Example: <EvilLaugh> ::= mwa <EvilCackle> <EvilCackle> ::= ha <EvilCackle> <EvilCackle> ::= ha! production

Productions rules for transforming nonterminal symbols into terminals or other nonterminals nonterminal ::= terminals and/or nonterminals each has lefthand side (LHS) and righthand side (RHS) every nonterminal must appear on LHS of at least one production Example: <EvilLaugh> ::= mwa <EvilCackle> <EvilCackle> ::= ha <EvilCackle> <EvilCackle> ::= ha! nonterminal

Productions rules for transforming nonterminal symbols into terminals or other nonterminals nonterminal ::= terminals and/or nonterminals each has lefthand side (LHS) and righthand side (RHS) every nonterminal must appear on LHS of at least one production Example: <EvilLaugh> ::= mwa <EvilCackle> <EvilCackle> ::= ha <EvilCackle> <EvilCackle> ::= ha! terminal

Productions rules for transforming nonterminal symbols into terminals or other nonterminals nonterminal ::= terminals and/or nonterminals each has lefthand side (LHS) and righthand side (RHS) every nonterminal must appear on LHS of at least one production Example: start symbol <EvilLaugh> ::= mwa <EvilCackle> <EvilCackle> ::= ha <EvilCackle> <EvilCackle> ::= ha!

Categories of grammars regular good for identifiers, parameter lists, subscripts an expanded form of regular expressions left-regular or right-regular context free LHS of production is single non-terminal context sensitive LHS has a non-terminal that can be surrounded by terminals and/or non-terminals recursively enumerable COMP 215 LHS can be any non-empty sequence of terminals and/or non-terminals

Backus-Naur Form (BNF) Used to describe syntax of PL; first used for Algol-60 Nonterminals are enclosed in <...> <expression>, <identifier> Alternatives indicated by <digit> ::= 0 1 2 3 4 5 6 7 8 9 Options (0 or 1 occurrences) indicated by [...] <stmt> ::= if <cond> then <stmt> [ else <stmt>] Repetition (0 or more occurrences) indicated by {...} <unsigned> ::= <digit> {<digit>} Derivation How would you do this using regular espressions? apply the rules, starting with start symbol and ending with a sentence

BNF defined using BNF <syntax> ::= <rule> <rule> <syntax> <rule> ::= <opt-whitespace> "<" <rule-name> ">" <opt-whitespace> "::=" <opt-whitespace> <expression> <line-end> <opt-whitespace> ::= " " <opt-whitespace> "" <expression> ::= <list> <list> " " <expression> <line-end> ::= <opt-whitespace> <EOL> <line-end> <line-end> <list> ::= <term> <term> <opt-whitespace> <list> <term> ::= <literal> "<" <rule-name> ">" <literal> ::= '"' <text> '"' "'" <text> "'"

Our earlier example a n b n, n = 0, 1, 2, <expr> ::= [a <expr> b]

Example Grammar and Derivation <program> ::= <stmts> <stmts> ::= <stmt> <stmt> ; <stmts> <stmt> ::= <var> = <expr> <var> ::= a b c d <expr> ::= <term> + <term> <term> - <term> <term> ::= <var> const <program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const

Derivation Terminology 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 similarly for rightmost derivation A derivation may be neither leftmost nor rightmost! A language defined by the grammar is a set of all possible sentences that can be derived from the grammar

Derivation Tree A derivation tree is the tree resulting from applying productions to rewrite start symbol a parse tree is the same tree starting with terminals and building back to the start symbol (goal symbol) A parse tree is a useful data structure to reason about the meaning of the program/data <program> <stmts> <stmt> <var> = <expr> a <term> + <term> <var> const b

Ending a sentence with a preposition is something up with which I will not put. - Winston Churchill

Ambiguity A grammar is ambiguous iff it generates a sentential form that has two or more distinct parse trees 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

Dangling Else Ambiguity One famous ambiguity is dangling else <stmt> ::= if <cond> then <stmt> [else <stmt>] This can derive if X > 9 then if B = 4 then X := 5 else X := 0 which if does this else belong to?

Dangling Else Ambiguity Can solve syntactically by adding nonterminals & productions <stmt> ::= <matched> <unmatched> <matched> ::= if <cond> then <matched> else <matched> <unmatched> ::= if <cond> then <stmt> if <cond> then <matched> else <unmatched> Can also solve semantically elses are associated with immediately preceding unmatched then

Resolving Ambiguity An ambiguous expression grammar: <expr> ::= <expr> <op> <expr> const <op> ::= / - <expr> Unambiguous expression grammar: <expr> ::= <expr> - <term> <term> <term> ::= <term> / const const Some languages are inherently ambiguous! <expr> - <term> <expr> / const const <term> const

Recursion Left recursive grammars <expr> ::= <expr> + <term> <term> <term> ::= <term> * const const Right recursive grammars <expr> ::= <term> + <remain> <term> <term> ::= const * <term> const <remain> ::= <term> <term> + <expr>

Associativity Left associativity: a - b + c = (a - b) + c Right associativity: a ** b ** c = a ** (b ** c)

Example: Expressions Consider following unambiguous grammar for expressions: <expr> ::= [<expr> <addop>] <term> <term> ::= [<term> <mulop>] <factor> <factor> ::= (<expr>) <digit> <addop> ::= + - <mulop> ::= * / <digit> ::= 0... 9 This grammar is left recursive and generates expressions that are left associative Changing <factor> production produces right associative exponentiation <factor> ::= <expon> [ ** <factor> ]

Syntax Graphs equivalent to CFGs put the terminals in circles or ellipses and put the nonterminals in rectangles; Lines and arrows indicate how constructs are built <expr> ::= <term> [ <addop> < expr>] <term> ::= <factor> [ <mulop> <term>] expr: term term: factor addop mulop

JSON Grammar json.org :