Parsing & Scanning Lecture 1 COMP 202 10/25/2004 Derek Ruths Office: DH Rm #3010.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Advertisements

Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Parsing & Scanning Lecture 2 COMP /25/2004 Derek Ruths Office: DH Rm #3010.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Lexing & Parsing (the intro version). What you have Input is string (via JTextField.getText() ) “(Lane OR Brown) AND (CS351 OR Java)” Useful to transform.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: , 5.3 self-check: Ch. 6 #1-6.
Lecture 13: Keyboard Input and Text Files Yoni Fridman 7/23/01 7/23/01.
File I/O in Java CS 311, Winter File Basics Recall that a file is block structured. What does this mean? What happens when an application opens.
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
1 CMSC 132: Object-Oriented Programming II Java Constructs Department of Computer Science University of Maryland, College Park.
CS 536 Spring Learning the Tools: JLex Lecture 6.
Chapter 15: Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Use JFileChooser F Text Input.
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Two Ways to Store Data in a File Text format Binary format.
Getting Started with ANTLR Chapter 1. Domain Specific Languages DSLs are high-level languages designed for specific tasks DSLs include data formats, configuration.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
CS 153: Concepts of Compiler Design August 24 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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.
Java Packages and Libraries M Taimoor Khan
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Chapter 17 Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams  Use JFileChooser F Text Input.
Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.
OOP with Java, David J. Barnes Input-Output1 A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
Tokenizers 29-Nov-15. Tokens A tokenizer is a program that extracts tokens from an input stream A token is a “word” or a significant punctuation mark.
Introduction to Computation and Problem Solving Class 29: Introduction to Streams Prof. Steven R. Lerman and Dr. V. Judson Harward.
CS 11 java track: lecture 2 This week: more on object-oriented programming (OOP) objects vs. primitive types creating new objects with new calling methods.
A stream is a sequence of data. A stream is a flowing sequence of characters.
Chapter 1 Introduction Major Data Structures in Compiler
CS 321 Programming Languages and Compilers VI-a. Recursive Descent Parsing.
Lab 2 Point List. OVERVIEW In this laboratory, you explore lists in which each element is a two-dimensional point or (x,y) pair. We refer to this type.
19-Dec-15 Tokenizers. Tokens A tokenizer is a program that extracts tokens from an input stream A token has two parts: Its value—this is just the characters.
– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Chapter 15: Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Parsing Text Files F Random.
Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
Chapter 15: Input and Output
ANU comp2110 Software Design lecture 10 COMP2110 Software Design in 2004 lecture 10 Software Architecture 2 of 2 design lecture 5 of 6 Goal of this small.
CSC 4181 Compiler Construction
Language Implementation Overview John Keyser Spring 2016.
Duke CPS Problem: reading, reading efficiently: C++ and Java l Many C++ stream implementations are slow, sometimes reading a file needs to be.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
©SoftMoore ConsultingSlide 1 Lexical Analysis (a.k.a. Scanning)
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
1 Input-Output A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated as transparently as.
Chapter 17 Input and Output
Streams, File I/O and Scanner class
Lexical Analysis (a.k.a. Scanning)
Introduction to programming in java
CS 614: Theory and Construction of Compilers
PROGRAMMING LANGUAGES
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Computer Programming Methodology File Input
CSc 453 Compilers & Systems Software 00. Background
Basic Program Analysis: AST
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Introduction to Java Programming
Parsing & Scanning Lecture 2
Building Java Programs
Tokenizers 25-Feb-19.
The Recursive Descent Algorithm
Chapter 1 Object-Oriented Programming Using Java
Tokenizers 26-Apr-19.
Tokenizers 3-May-19.
Zorah Fung University of Washington, Winter 2016
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Presentation transcript:

Parsing & Scanning Lecture 1 COMP /25/2004 Derek Ruths Office: DH Rm #3010

IDEs Speech-Recognition Compilers/Interpreters Games

What is Parsing? parse (v.) to break down into its component parts of speech with an explanation of the form, function, and syntactical relationship of each part. --The American Heritage Dictionary Scanning Parsing

High-Level View Scanner Text, Source Code, Speech Parser Structured Representation (e.g. Abstract Syntax Tree)

Overview Lecture 1: Intro to scanning Intro to parsing Basics of building a scanner in Java Lab: Implementing a scanner Lecture 2: Basic Parsing Theory Design of an Object-Oriented Parser

High-Level View Scanner Text, Source Code, Speech Parser Structured Representation (e.g. Abstract Syntax Tree)

Scanning: Breaking Things Down Scanner Token TypeToken Value “3 + x” Character Stream, Tokens Token List

Scanning: Token List Token ListTolkien List Token Type Tolkien Type = Wizard Tolkien Descriptor = Magical Powers Token Descriptor

High-Level View (you saw this earlier) Scanner Text, Source Code, Speech Parser Structured Representation (e.g. Abstract Syntax Tree)

Parsing: Organizing Things Parser, Tokens x + 3 Abstract Syntax Tree Grammar

Manual Scanning in Java Scanner Token TypeToken Value “3 + x” Character Stream, Tokens Token List

Tokenizing Example public static final int PLUS = ‘+’; public void tokenize(String str) { StreamTokenizer stok = new StreamTokenizer(new StringReader(str)); int token; stok.ordinaryChar(PLUS); stok.parseNumbers(); while((token = stok.nextToken()) != StreamTokenizer.TT_EOF) { switch(token) { case TT_WORD: System.out.println(”WORD = “ + stok.sval); break; case TT_NUMBER: System.out.println(”NUM = “ + stok.nval); break; case PLUS: System.out.printlN(”PLUS”); break; } Initialization Configuration Scanning

Tokenizing Example public static final int PLUS = ‘+’; public void tokenize(String str) { StreamTokenizer stok = new StreamTokenizer(new StringReader(str)); int token; stok.ordinaryChar(PLUS); stok.parseNumbers(); while((token = stok.nextToken()) != StreamTokenizer.TT_EOF) { switch(token) { case TT_WORD: System.out.println(”WORD = “ + stok.sval); break; case TT_NUMBER: System.out.println(”NUM = “ + stok.nval); break; case PLUS: System.out.printlN(”PLUS”); break; } Initialization Configuration Scanning

java.io. StreamTokenizer Configuration: It’s like programming a VCR! Token Type (int) Token Desc. How to Customize StreamTokenizer.TT_WORD a word (no spaces) void wordChars(int low, int high) int qch e.g. :hello there: is a quoted string a string quoted by ‘qch’ void quoteChar(int qch) e.g. quoteChar(’:’) StreamTokenizer.TT_NUMB ER Numbersvoid parseNumbers() int ch e.g. (int) ‘+’ the character value of ch void ordinaryChar(int ch) e.g. ordinaryChar(’+’)

Tokenizing Example public static final int PLUS = ‘+’; public void tokenize(String str) { StreamTokenizer stok = new StreamTokenizer(new StringReader(str)); int token; stok.ordinaryChar(PLUS); stok.parseNumbers(); while((token = stok.nextToken()) != StreamTokenizer.TT_EOF) { switch(token) { case TT_WORD: System.out.println(”WORD = “ + stok.sval); break; case TT_NUMBER: System.out.println(”NUM = “ + stok.nval); break; case PLUS: System.out.printlN(”PLUS”); break; } Initialization Configuration Scanning

Call int StreamTokenizer.nextToken() to get the next token. String StreamTokenizer.sval (public field) holds the token value for TT_WORD and quote token types double StreamTokenizer.nval (public field) holds the token value for TT_NUMBER token type java.io. StreamTokenizer Scanning: It’s like calling “nextToken” over and over!

Tokenizing Example public static final int PLUS = ‘+’; public void tokenize(String str) { StreamTokenizer stok = new StreamTokenizer(new StringReader(str)); int token; stok.ordinaryChar(PLUS); stok.parseNumbers(); while((token = stok.nextToken()) != StreamTokenizer.TT_EOF) { switch(token) { case TT_WORD: System.out.println(”WORD = “ + stok.sval); break; case TT_NUMBER: System.out.println(”NUM = “ + stok.nval); break; case PLUS: System.out.printlN(”PLUS”); break; } Initialization Configuration Scanning

Constructor: StreamTokenizer(Reader r) java.io. Reader - class for reading bytes FileReader - read bytes from a File StringReader - read bytes from a String java.io. StreamTokenizer Initialization: It’s like using Java I/O!

Questions?