CSE 341 Lecture 19 parsing / Homework 7 slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Compilers and Language Translation
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
CPSC Compiler Tutorial 9 Review of Compiler.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
PZ02A - Language translation
Context-Free Grammars Lecture 7
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Environments and Evaluation
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule Substitute Equivalent grammar.
1 Compilers. 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v.
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 3 Part II Describing Syntax and Semantics.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Programming Languages
1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
CSE 413 Languages & Implementation Hal Perkins Autumn 2012 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
MiniJava Compiler A multi-back-end JIT compiler of Java.
1 Compiler Construction Vana Doufexi office CS dept.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Objective of the course Understanding the fundamentals of the compilation technique Assist you in writing you own compiler (or any part of compiler)
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
A Simple Syntax-Directed Translator
PROGRAMMING LANGUAGES
Bison: Parser Generator
Course supervisor: Lubna Siddiqui
Compiler Design 4. Language Grammars
Syntax-Directed Definition
Compilers B V Sai Aravind (11CS10008).
slides created by Marty Stepp
R.Rajkumar Asst.Professor CSE
Lecture 4: Lexical Analysis & Chomsky Hierarchy
6.001 SICP Interpretation Parts of an interpreter
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Presentation transcript:

CSE 341 Lecture 19 parsing / Homework 7 slides created by Marty Stepp

2 Looking ahead We will complete a 2-part assignment related to analyzing and interpreting BASIC source code.  HW7: BASIC expression parser  HW8: BASIC interpreter To complete this assignment, it is helpful to have some background about how compilers and interpreters work.  HW8 will be an interpreter that performs REPL (read, eval, print) on BASIC source code.  HW7 is a parser that reads BASIC math expressions. –HW8 will make use of HW7's code to eval expressions.

3 How does a compiler work? A typical compiler or interpreter consists of many steps: 1. lexical analysis: break apart the code into tokens 2. syntax analysis (parsing): examine sequences of tokens based on the language's syntax 3. semantic analysis: reason about the meaning of the token sequences (particularly pertaining to types) 4. code generation: generate executable code in some format (native, bytecode, etc.) 5. optimization (optional): improve the generated code

4 1. Lexical analysis (tokenizing) Suppose you are writing a Java interpreter or compiler.  The source code you want to read contains this: for (int i=2*3/ ; i*x <= 3.7 * y; i = i*3+7)  The first task is to split apart the input into tokens based on the language's token syntax and delimiters: for(inti=2*3/4+2+7;i *x<=3.7*y;i=i*3+7)

5 A tokenizer in Scheme If our Java interpreter is written in Scheme, we convert: for (int i=2*3/ ; i*x <= 3.7 * y; i = i*3+7)  into the following Scheme list: (for ( int i = 2 * 3 / ; i * x <= 3.7 * y ; i = i * ) ) –if typed in as Scheme source, it would have been: (list 'for '( 'int 'i '= 2 '* 3 '/ 4 '+ 2 '+ 7 '; 'i '* 'x '<= 3.7 '* 'y '; 'i '= 'i '* 3 '+ 7 ') )  ( and ) are hard to process as symbols; so we'll use: (for lparen int i = 2 * 3 / ; i * x <= 3.7 * y ; i = i * rparen )

6 2. Syntax analysis (parsing) Now that we have a list of tokens, we will walk across that list to see how the tokens relate to each other.  Example: Suppose we've processed the source code up to: (for lparen int i = 2 * 3 / ; i * x <= 3.7 * y ^ ; i = i * rparen )  From parser's perspective, the list of upcoming tokens is: 2 * 3 / ; i * x <= 3.7 * y ; i =... ^

7 Parsing expressions The list of upcoming tokens contains expressions: 2 * 3 / ; i * x <= 3.7 * y ; i =... Parsers process the code they read:  a compiler builds a syntax tree  an interpreter evaluates the code 10 ; i * x <= 3.7 * y ; i =...

8 Grammars ::= ::= " " | " =" | "=" | "<>" ::= {("+" | "-") } ::= {("*" | "/") } ::= {"^" } ::= | ("+" | "-") | "(" ")" | "(" ")" ::= SIN | COS | TAN | ATN | EXP | ABS | LOG | SQR | RND | INT grammar: set of structural rules for a language  often described in terms of themselves (recursive) – ; TERMINAL; "literal token"; –{repeated 0--* times}; or: (a | b)

9 Procedures you'll write (1) parse-factor  ::= | ("+" | "-") | "(" ")" | "(" ")" > (parse-factor '( * 7.2)) ( * 7.2) > (parse-factor '(lparen rparen + 3.4)) ( ) > (parse-factor '(SQR lparen * rparen)) (5) > (parse-factor '(- lparen rparen * 4.5)) (-4 * 4.5)

10 Procedures you'll write (2) parse-element  ::= {"^" } > (parse-element '(2 ^ 2 ^ 3 THEN 450)) (64 THEN 450) > (parse-element '(2 ^ 2 ^ -3 THEN 450)) ( THEN 450 > (parse-element '(2.3 ^ 4.5 * 7.3)) ( * 7.3) > (parse-element '( )) ( )

11 The grammar is the code! ::= | ("+" | "-") | "(" ")" | "(" ")" (define (parse-factor lst) ; 1) if I see a number, then... ; 2) if I see a + or -, then... ; 3) if I see a (, then... ; 4) else it is an, so... How do you know which of the four cases you are in?

12 Recall: Checking types (type? expr) tests whether the expression/var is of the given type  (integer? 42) → #t  (rational? 3/4) → #t  (real? 42.4) → #t  (number? 42) → #t  (procedure? +) → #t  (string? "hi") → #t  (symbol? 'a) → #t  (list? '(1 2 3)) → #t  (pair? (42. 17)) → #t

13 Exact vs. inexact numbers You'll encounter problems with Scheme's rational type:  Scheme thinks 3/2 is 1 1 / 2 (a rational)  the interpreter wants 3/2 to be 1.5(a real) Scheme differentiates exact numbers (integers, fractions) from inexact numbers (real numbers).  (A complex number can be exact or inexact.)  Round-off errors can occur only with inexact numbers.

14 Managing exact/inexact numbers exact?, inexact? procedures see if a number is exact:  (exact? 42) → #t  (inexact? 3.25) → #t Scheme has procedures to convert between the two:  (exact->inexact 13/4) → 3.25  (inexact->exact 3.25) → 3 1 / 4 –(May want floor, ceiling, truncate,... in some cases.) (In general, conversion procedure names are type1->type2.)

15 Parsing math functions ::= SIN | COS | TAN | ATN | EXP | ABS | LOG | SQR | RND | INT grammar has tokens representing various math functions  must map from these to equivalent Scheme procedures  could use a giant nested if or cond expression, but... (define functions '((SIN. sin) (COS. cos) (TAN. tan) (ATN. atan) (EXP. exp) (ABS. abs) (LOG. log) (SQR. sqrt) (RND. rand) (INT. trunc)))

16 Associative lists (maps) with pairs Recall: a map associates keys with values  can retrieve a value later by supplying the key in Scheme, a map is stored as a list of key/value pairs: (define phonebook (list '(Marty ) '(Stuart ) '(Jenny ))) look things up in a map using the assoc procedure: > (assoc 'Stuart phonebook) (Stuart ) > (cdr (assoc 'Jenny phonebook)) ; get value