CSE 425: Syntax II Context Free Grammars and BNF In context free grammars (CFGs), structures are independent of the other structures surrounding them Backus-Naur.

Slides:



Advertisements
Similar presentations
CSE 425: Semantic Analysis Semantic Analysis Allows rigorous specification of a program’s meaning –Lets (parts of) programming languages be proven correct.
Advertisements

Session 14 (DM62) / 15 (DM63) Recursive Descendent Parsing.
ISBN Chapter 3 Describing Syntax and Semantics.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Context-Free Grammars Lecture 7
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
A basis for computer theory and A means of specifying languages
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
S YNTAX. Outline Programming Language Specification Lexical Structure of PLs Syntactic Structure of PLs Context-Free Grammar / BNF Parse Trees Abstract.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
Syntax and Backus Naur Form
CS 331, Principles of Programming Languages Chapter 2.
Context-Free Grammars
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Context-Free Grammars and Parsing
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Grammars CPSC 5135.
Programming Languages Third Edition Chapter 6 Syntax.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax (cont.) Lecture 4 – Syntax (Cont.), Spring CSE3302 Programming Languages,
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
11 Chapter 4 Grammars and Parsing Grammar Grammars, or more precisely, context-free grammars, are the formalism for describing the structure of.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
CPS 506 Comparative Programming Languages Syntax Specification.
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
ISBN Chapter 3 Describing Syntax and Semantics.
CS 3240 – Chapter 5. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
CS 331, Principles of Programming Languages Chapter 2.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Syntax and Grammars.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
CSE 425: Syntax I Syntax and Semantics Syntax gives the structure of statements in a language –Allowed ordering, nesting, repetition, omission of symbols.
CSE 425: Control Abstraction II Exception Handling Previous discussion focuses on normal control flow –Sometimes program reaches a point where it cannot.
Top-Down Parsing.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Programming Languages Translator
Chapter 3 Context-Free Grammar and Parsing
Chapter 3 – Describing Syntax
Syntax (1).
Compiler Construction (CS-636)
Syntax One - Hybrid CMSC 331.
CSC 4181Compiler Construction Context-Free Grammars
R.Rajkumar Asst.Professor CSE
Iteration Implemented through loop constructs (e.g., in C++)
CSC 4181 Compiler Construction Context-Free Grammars
BNF 9-Apr-19.
COMPILER CONSTRUCTION
Presentation transcript:

CSE 425: Syntax II Context Free Grammars and BNF In context free grammars (CFGs), structures are independent of the other structures surrounding them Backus-Naur form (BNF) notation describes CFGs –Symbols are either tokens or nonterminal symbols –Productions are of the form nonterminal → definition where definition defines the structure of a nonterminal –Rules may be recursive, with nonterminal symbol appearing both on left side of a production and in its own definition –Metasymbols are used to identify the parts of the production (arrow), alternative definitions of a nonterminal (vertical bar) –Next time we’ll extend metasymbols for repeated (braces) or optional (square brackets) structure in a definition (EBNF)

CSE 425: Syntax II Parse Trees and Abstract Syntax Trees Parse trees show derivation of a structure from BNF –E.g., number → DIGIT | DIGIT number Abstract syntax trees (ASTs) encapsulate the details –Very useful for converting between structurally similar forms parse treeabstract syntax tree number DIGIT number DIGIT hornclause body head predicate …

CSE 425: Syntax II Ambiguity, Associativity, Precedence If any statement in the language has more than one distinct parse tree, the language is ambiguous –Ambiguity can be removed implicitly, as in always replacing the leftmost remaining nonterminal (an implementation hack) Recursive production structure also can disambiguate –E.g., adding another production to the grammar to establish precedence (lower in parse tree gives higher precedence) –E.g., replacing exp → exp + exp with alternative productions exp → exp + term or exp → term + exp Recursive productions also define associativity –I.e., left-recursive form exp → exp + term is left-associative, right-recursive form exp → term + exp is right-associative

CSE 425: Syntax II Extended Backus-Naur Form (EBNF) Optional/repeated structure is common in programs –E.g., whether or not there are any arguments to a function –E.g., if there are arguments, how many there are We can extend BNF with metasymbols –E.g., square brackets indicate optional elements, as in the production function → name ‘(‘ [args] ‘)’ –E.g., curly braces to indicate zero or more repetitions of elements, as in the production args → arg {‘,’ arg} –Doesn’t change the expressive power of the grammar A limitation of EBNF is that it obscures associativity –Better to use standard BNF to generate parse/syntax trees

CSE 425: Syntax II Recursive-Descent Parsing Shift-reduce (bottom-up) parsing techniques are powerful, but complex to design/implement manually –Further details about them are in another course (CSE 431) –Still will want to understand how they work, use techniques Recursive-descent (top-down) parsing is often more straightforward, and can be used in many cases –We’ll focus on these techniques somewhat in this course Key idea is to design (potentially recursive) parsing functions based on the productions’ right-hand sides –Then, work through a grammar from more general rules to more specific ones, consuming input tokens upon a match –EBNF helps with left recursion removal (making a loop) and left factoring (making remainder of parse function optional)

CSE 425: Syntax II Lookahead with First and Follow Sets Recursive descent parsing functions are easiest to write if they only have to consider the current token –I.e., the head of a stream or list of input tokens Optional and repeated elements complicate this a bit –E.g., function → name ( [args] ) and arg → 0 |…| 9 and args → arg {, arg} with ( ) 0 |…| 9, as terminal symbols But, EBNF structure helps in handling these two cases –The set of tokens that can be first in a valid sequence, e.g., each digit in 0 |…| 9 is in the first set for arg (and for args) –The set of tokens that can follow a valid sequence of tokens, e.g., ‘)’ is in the follow set for args –A token from the first set gives a parse function permission to start, while one from the follow set directs it to end

CSE 425: Syntax II Today’s Studio Exercises We’ll code up ideas from Scott Chapter 2.3 –Looking at more ideas and mechanisms for parsing, especially ones that are relevant to the lab assignment Today’s exercises are again all in C++ –Please take advantage of the on-line tutorial and reference manual pages that are linked on the course web site –As always, please ask us for help as needed When done, your answers to the course account with “Syntax Studio II” in the subject line