More on Syntax. Agenda for the Day. Administrative Stuff. More on Syntax In-Class Exercise Using parse trees

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

Compilers. Yannis Smaragdakis, U. Athens (original slides by Sam

CS 314 Principles of Programming Languages

EECS 6083 Intro to Parsing Context Free Grammars

Syntax Analysis Check syntax and construct abstract syntax tree

Parsing II Top-down parsing. Comp 412

COMP 181. Prelude. Next step. Parsing. Study of parsing. Specifying syntax with a grammar

Introduction to Parsing

A simple syntax-directed

Part 5 Program Analysis Principles and Techniques

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

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

CS 406/534 Compiler Construction Parsing Part I

Front End. Hwansoo Han

CSE 3302 Programming Languages Lecture 2: Syntax

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

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

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

A Simple Syntax-Directed Translator

Principles of Programming Languages COMP251: Syntax and Grammars

Fall Compiler Principles Context-free Grammars Refresher. Roman Manevich Ben-Gurion University of the Negev

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

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

MA513: Formal Languages and Automata Theory Topic: Context-free Grammars (CFG) Lecture Number 18 Date: September 12, 2011

CSE302: Compiler Design

Syntax Analysis Part I

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

How do LL(1) Parsers Build Syntax Trees?

CSCE 314 Programming Languages

Prelude COMP 181 Tufts University Computer Science Last time Grammar issues Key structure meaning Tufts University Computer Science

3. Parsing. Oscar Nierstrasz

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

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

CSCI312 Principles of Programming Languages!

Introduction to Compiler

Introduction to Parsing. Comp 412

Describing Syntax and Semantics

Habanero Extreme Scale Software Research Project

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

Wednesday, September 9, 15. Parsers

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

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

LANGUAGE PROCESSORS. Presented By: Prof. S.J. Soni, SPCE Visnagar.

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

Principles of Programming Languages COMP251: Syntax and Grammars

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

Building Compilers with Phoenix

This book is licensed under a Creative Commons Attribution 3.0 License

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

Bottom-up Parser. Jungsik Choi

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Compiler Design Concepts. Syntax Analysis

CSCI312 Principles of Programming Languages

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

Compilers Course Lecture 4: Context Free Grammars

2.2 Syntax Definition

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Syntax. In Text: Chapter 3

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Defining syntax using CFGs

Compilers. Bottom-up Parsing. (original slides by Sam

Table-Driven Top-Down Parsers

Syntax Intro and Overview. Syntax

Goals for course What is a compiler and briefly how does it work? Review everything you should know from 330 and before

Introduction to Syntax Analysis. The Second Phase of Front-End

Homework & Announcements

COP 3402 Systems Software Syntax Analysis (Parser)

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

CSE 12 Abstract Syntax Trees

Lexical and Syntax Analysis. Top-Down Parsing

Introduction to Syntax Analysis

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

CPS 506 Comparative Programming Languages. Syntax Specification

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

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

Monday, September 13, Parsers

CMSC 330: Organization of Programming Languages

Lexical and Syntax Analysis

Wednesday, August 31, Parsers

CSX-lite Example. LL(1) Parse Tables. LL(1) Parser Driver. Example of LL(1) Parsing. An LL(1) parse table, T, is a twodimensional

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

Syntactic Analysis. Top-Down Parsing

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

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

Announcements. Written Assignment 1 out, due Friday, July 6th at 5PM.

CMSC 330: Organization of Programming Languages. Context-Free Grammars Ambiguity

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

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

Chapter 3. Parsing #1

Context-Free Languages and Parse Trees

Syntax and Grammars 1 / 21

Parsing II Top-down parsing. Comp 412

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

Lecture 8: Context Free Grammars

CMPT 379 Compilers. Parse trees

Transcription:

More on Syntax Judy Stafford Comp 80 Meeting February, 00 Agenda for the Day Administrative Stuff Moodle Classlist at without waiting list More on Syntax InClass Exercise Using parse trees

Last time Syntax Problem: how to precisely describe what a properly formed program looks like Must cover all possible programs Solution: formal grammars Inspired by work in linguistics Notation: BackusNaur Form (BNF) Contextfree grammar # Production rule nonterminal terminals or nonterminals Formally: contextfree grammar is G = (s, N, T, P) T : set of terminals (the words ) N : set of nonterminals (parts of speech) P : N (N T)* : production rules (sentence structure) s N : start or goal symbol Note: In a complete grammar all nonterminals appear on the lefthand side of at least one production

Using a BNF Two ways: Generate strings called derivation Idea: Use productions as rewrite rules Start with the start symbol (a nonterminal) Apply productions: Choose a nonterminal and expand it to the righthand side of one of its productions When only terminal symbols remain, we have a legal string Recognize strings called parsing Start with a string of terminals (e.g., a program) Try to figure out if it can be derived from the grammar Topic of comp8 Compilers Applied to programming A real grammar Arithmetic essions Numbers and variables (terminals called num and id ) Binary operators: +,, *, / For now, no parentheses Examples: + + * / 9 * F x * y

BNF for Expressions Note: Special terminals number and identifier Categories of terminals Examples: <num, > <id, foo > How are terminals specified? Typically, using regular essions We probably won t cover that topic # Production rule 7 op number identifier op + * / Language of essions What s in this language? # Production rule 7 op number identifier op + * / Rule Sentential form op <id,x> op <id,x> <id,x> op <id,x> <num,> op <id,x> <num,> * <id,x> <num,> * <id,y> We can build the string x * y This string is in the language

More on derivations Different derivations are possible At each step we can choose any nonterminal Rightmost derivation:» Always choose rightmost nonterminal Leftmost derivation:» Always choose leftmost nonterminal What other derivations are possible?» random derivations not of interest Question: Does it matter? Left vs right derivations Two derivations of x * y Rule Sentential form op <id, x> op <id,x> <id,x> op <id,x> <num,> op <id,x> <num,> * <id,x> <num,> * <id,y> Leftmost derivation Rule Sentential form op op <id,y> * <id,y> op * <id,y> op <num,> * <id,y> <num,> * <id,y> <id,x> <num,> * <id,y> Rightmost derivation

Derivations and parse trees Two different derivations Both are correct Let s look carefully at the differences Represent derivation as a parse tree Leaves are terminal symbols Inner nodes are nonterminals To depict production α β γ δ show nodes β,γ,δ as children of α β α γ δ Tree is often used to represent semantics We want the structure of the parse tree to capture the meaning of the sentence Example (I) Rule Sentential form Rightmost derivation op op <id,y> * <id,y> op * <id,y> op <num,> * <id,y> <num,> * <id,y> <id,x> <num,> * <id,y> Parse tree op op * y x Concrete syntax tree Captures the exact grammatical structure Often has lots of extraneous information

Concrete vs abstract Concrete syntax The exact symbols used to write a program The precise grammatical structure Abstract syntax An abstraction of the program syntax Eliminates uninteresting details of the derivation Closer to the meaning of the program Often something used inside the compiler or interpreter Examples Infix: + ( * ) Postfix: * + Prefix: (+ (* )) + * Concrete vs abstract Another example: while i < N do begin i := i + end Pascal while (i < N) { i = i + ; } C/C++ What are some differences? Note: these programs do the same thing Different concrete syntax Same (or similar) abstract syntax Identical semantics 7

Abstract syntax tree Turn concrete syntax into abstract syntax Eliminate extra junk (intermediate nodes) Move operators up to parent nodes Result: abstract syntax tree * op y op * y x x Back to derivations Leftmost derivation Parse tree Rule Sentential form op <id, x> op <id,x> <id,x> op <id,x> <num,> op <id,x> <num,> * <id,x> <num,> * <id,y> op x op * y 8

Derivations Two different abstract syntax trees for x * y Which one do I want? * x * y y x Leftmost derivation Rightmost derivation Derivations and semantics Problem: Two different valid derivations One captures meaning we want (Arithmetic precedence rules) Key idea: shape of tree implies its meaning Can we ess precedence in grammar? 9

Derivations and precedence Question: Parse tree Which operations are evaluated first in the abstract syntax tree? Operations deeper in tree op Solution: add an intermediate production New production isolates different levels of precedence Force higher precedence deeper in the grammar op * y x Adding precedence Two levels: Level : lower precedence higher in the tree Level : higher precedence deeper in the tree # Production rule Observations: Larger: requires more rewriting to reach terminals But, produces same abstract parse tree under both left and right derivations 7 8 + term term term term term * factor term / factor factor factor number identifier 0

Expression example Rightmost derivation Parse tree Rule Sentential form 8 7 8 term term * factor term * <id,y> factor * <id,y> <num,> * <id,y> term <num,> * <id,y> factor <num,> * <id,y> <id,x> <num,> * <id,y> term fact x op term fact term op fact * y Now right derivation yields x ( * y) With precedence op term * op * y term term * fact x fact fact y x

InClass Assignment Find a partner and get out a piece of paper Draw both an concrete and abstract syntax tree for: * A + 9 B * # Production rule 7 8 + term term term term term * factor term / factor factor factor number identifier Exchange with neighbors grade Send over to Chris A quick look at BNF for C++ http://www.nongnu.org/hcb/

Next time A bit more on Syntax Introduction to Scheme