Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #5 Lecture #5 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Chapter 2 Syntax. Syntax The syntax of a programming language specifies the structure of the language The lexical structure specifies how words can be.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 4 Lexical and Syntax Analysis Sections
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
ISBN Chapter 3 More Syntax –BNF –Derivations –Practice.
Fall 2007CS 2251 Miscellaneous Topics Cloning Patterns Recursion and Grammars.
Chapter 4 Lexical and Syntax Analysis Sections 1-4.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Slide1 Chapter 4 Lexical and Syntax Analysis. slide2 OutLines: In this chapter a major topics will be discussed : Introduction to lexical analysis, including.
A basis for computer theory and A means of specifying languages
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
1 CSE305 Programming Languages Syntax What is it? How is it specified? Who uses it? Why is it needed?
ISBN Chapter 3 Describing Syntax and Semantics.
1 Note As usual, these notes are based on the Sebesta text. The tree diagrams in these slides are from the lecture slides provided in the instructor resources.
CS 330 Programming Languages 09 / 23 / 2008 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Lexical and Syntax Analysis
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
Chapter 3: Formal Translation Models
ISBN Chapter 3 Describing Syntax and Semantics.
UMBC Introduction to Compilers CMSC 431 Shon Vick 01/28/02.
(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.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
CSI 3120, Grammars, page 1 Language description methods Major topics in this part of the course: –Syntax and semantics –Grammars –Axiomatic semantics (next.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Grammars CPSC 5135.
Chapter 3 Part I Describing Syntax and Semantics.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
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.
Lexical and Syntax Analysis
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
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.
CPS 506 Comparative Programming Languages Syntax Specification.
D Goforth COSC Translating High Level Languages.
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.
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.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Describing Syntax and Semantics Session 2 Course : T Programming Language Concept Year : February 2011.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
C H A P T E R T W O Syntax and Semantic. 2 Introduction Who must use language definitions? Other language designers Implementors Programmers (the users.
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.3-1 Language Specification and Translation Lecture 8.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Context Free Grammars & Parsing CPSC 388 Fall 2001 Ellen Walker Hiram College.
BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Lexical and Syntax Analysis
Chapter 3 – Describing Syntax
CS 363 Comparative Programming Languages
Lexical and Syntax Analysis
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Chapter 4: Lexical and Syntax Analysis Sangho Ha
Chapter 3 Describing Syntax and Semantics.
High-Level Programming Language
Describing Syntax and Semantics
Presentation transcript:

Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars

Fall 2007CS 2252 Other Data Structures Deque - a double-ended queue Balanced Trees There are several ADTs for trees that keep the tree balanced at all times

Fall 2007CS 2253 Deque A deque is a double-ended queue Like a queue except that both insertion and removal can occur from either end Both stacks and queues can be implmented using a deque Can be implemented using either dynamic arrays or doubly-linked lists

Fall 2007CS 2254 Deque Operations Insert at back insert at front remove last remove first peek first peek last

Fall 2007CS 2255 Deques in Java Deque Interface (Java 1.6) ArrayDeque( Java 1.6) LinkedList implements a doubly-linked list so it has all the deque operations

Fall 2007CS 2256 Grammars and Recursion A grammar is a formal description of a language –For natural languages, this is hard –In order to be able to use a program to translate a program, programming languages need to be relatively simple. Regular expressions can be used to specify the simplest grammars BNF is a notation that was invented to describe programming languages

Fall 2007CS 2257 Backus-Naur Form Generally referred to as BNF Most widely known method for describing programming language syntax BNF description of a language consists of a set of rules that can be used to generate statements in the language

Fall 2007CS 2258 BNF Rules Left hand side of a rule is a non-terminals; something that is built from smaller pieces –there may be several rules for a single non-terminal Right hand side of a rule consists of terminals and other non-terminals in the order they need to occur –Terminals are typically keywords and symbols A grammar is a collection of rules

Fall 2007CS 2259 Sample Grammar -> -> aa -> a -> b is known as the start symbol

Fall 2007CS Derivations BNF is a generative device –Use a grammar to generate sentences that belong to the language the grammar describes A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols)

Fall 2007CS Sample Derivations -> -> b -> -> a -> ab -> -> aa -> aaaa -> aaaaa -> aaaaab -> aaaaabb -> aaaaabbb

Fall 2007CS Extended BNF Optional parts are placed in brackets [ ] -> ident [( )] Alternative parts of RHSs are placed inside parentheses and separated via vertical bars → (+|-) const Repetitions (0 or more) are placed inside braces { } → letter {letter|digit}

Fall 2007CS Sample Grammar -> | -> aa | a -> b | b

Fall 2007CS What does this have to do with recursion? One of the common techniques for parsing a program (checking its syntax and putting it into a form that can be translated into an executable format) uses the BNF rules to implement a set of recursive methods

Fall 2007CS Recursive-Descent Parsing There is a method for each nonterminal in the grammar –If there are several rules, the method needs to determine which to use –The method checks that each element in the rule is present EBNF is ideally suited for being the basis for a recursive-descent parser, because EBNF minimizes the number of nonterminals

Fall 2007CS Recursive-Descent Methods For a single rule: –For each terminal symbol in the RHS, compare it with the next input token; if they match, continue, else there is an error –For each nonterminal symbol in the RHS, call its associated parsing subprogram

Fall 2007CS Recursive-Descent Methods A nonterminal that has more than one RHS requires an initial process to determine which RHS it is to parse –The correct RHS is chosen on the basis of the next token of input –The next token is compared with the first token that can be generated by each RHS until a match is found –If no match is found, it is a syntax error

Fall 2007CS Example Our sample grammar would have three methods –S() –A() –B() Algorithm for S() if next is a call A() call B()

Fall 2007CS Algorithm for A() checks for an a if present check for a second a if present call A if next is a get a else fail

Fall 2007CS Algorithm for B() checks for an b if present check for a second b if present call B else fail