CS 230 Programming Languages

Similar documents
LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Indian Institute of Technology Kharagpur. PERL Part II. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

CS 230 Programming Languages

Classnote for COMS6100

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

ECE251 Midterm practice questions, Fall 2010

CS 106 Introduction to Computer Science I

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

(Refer Slide Time: 01:12)

CPS 506 Comparative Programming Languages. Syntax Specification

CS 106 Introduction to Computer Science I

Outline. CS3157: Advanced Programming. Feedback from last class. Last plug

IT441. Network Services Administration. Perl: File Handles

Learning Perl 6. brian d foy, Version 0.6, Nordic Perl Workshop 2007

Describing Syntax and Semantics

Syntax Intro and Overview. Syntax

CA Compiler Construction

CSE 12 Abstract Syntax Trees

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

COMS 3101 Programming Languages: Perl. Lecture 2

CS101 Introduction to Programming Languages and Compilers

This book is licensed under a Creative Commons Attribution 3.0 License

LECTURE 3. Compiler Phases

CS 106 Introduction to Computer Science I

Chapter 3. Describing Syntax and Semantics ISBN

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

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

SEER AKADEMI LINUX PROGRAMMING AND SCRIPTINGPERL 7

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Chapter 3. Describing Syntax and Semantics

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

EECS 6083 Intro to Parsing Context Free Grammars

First Java Program - Output to the Screen

Principles of Programming Languages COMP251: Syntax and Grammars

CS 106 Introduction to Computer Science I

CS 206 Introduction to Computer Science II

Topic 1: Introduction

Syntax. A. Bellaachia Page: 1

Chapter 3. Describing Syntax and Semantics ISBN

1 Lexical Considerations

Lecture 10 Parsing 10.1

Syntax-Directed Translation. Lecture 14

CS 230 Programming Languages

Programming Language Syntax and Analysis

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

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Chapter 3. Describing Syntax and Semantics

Syntax and Grammars 1 / 21

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

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

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

DEMO A Language for Practice Implementation Comp 506, Spring 2018

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

Lecture 12: Parser-Generating Tools

Chapter 3 (part 3) Describing Syntax and Semantics

Chapter 4. Lexical and Syntax Analysis

Programming Languages and Compilers (CS 421)

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) Programming Languages & Compilers. Major Phases of a Compiler

Chapter 10 Language Translation

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) I. Major Phases of a Compiler. Programming Languages & Compilers

Using Scala for building DSL s

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

A simple syntax-directed

COP4020 Programming Assignment 1 - Spring 2011

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

CHAPTER 4 FUNCTIONS. 4.1 Introduction

PL Revision overview

1. Introduction. 2. Scalar Data

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Defining syntax using CFGs

Intro. Scheme Basics. scm> 5 5. scm>

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

Homework & Announcements

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Time : 1 Hour Max Marks : 30

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Classes, interfaces, & documentation. Review of basic building blocks

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

Programming Basics. Digital Urban Visualization. People as Flows. ia

CSE 3302 Programming Languages Lecture 2: Syntax

Control Structures. Important Semantic Difference

CSCI 4152/6509 Natural Language Processing. Perl Tutorial CSCI 4152/6509. CSCI 4152/6509, Perl Tutorial 1

Programming Languages and Compilers (CS 421)

Semantic Analysis Attribute Grammars

Intermediate Code Generation

n (0 1)*1 n a*b(a*) n ((01) (10))* n You tell me n Regular expressions (equivalently, regular 10/20/ /20/16 4

Motivation was to facilitate development of systems software, especially OS development.

JavaCC: SimpleExamples

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Course Overview. Introduction (Chapter 1) Compiler Frontend: Today. Compiler Backend:

Part 3. Syntax analysis. Syntax analysis 96

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

CMSC 330: Organization of Programming Languages. Operational Semantics

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

Syntax Errors; Static Semantics

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Transcription:

CS 230 Programming Languages 09 / 16 / 2013 Instructor: Michael Eckmann

Today s Topics Questions/comments? Continue Syntax & Semantics Mini-pascal Attribute Grammars More Perl

A more complex grammar Let's take a look at the handout for the mini-pascal language. Let's first randomly generate a valid sentence (program) or two given this description. Then let's in our mind sort of create a parser from this EBNF description and use that to determine if some programs are syntactically correct.

Limitations of CFG and EBNF Do you think that the EBNF for mini-pascal is the complete description for the syntax of the language? Is anything missing? --- think of some syntax errors that you are used to seeing in your favorite language.

Attribute Grammars Hence the creation of attribute grammars. An attribute grammar is an extension to a CFG. There are some rules of programming languages that cannot be specified in BNF (or by a CFG for that matter.) e.g. All variables must be declared before they are used. Also, there are things that are possible, but just too hairy to specify using CFG's, (e.g. Type compatibility) so Attribute Grammars are used. These kinds of things are termed static semantics. This is a bit of a misnomer because they are really still syntax rules not semantics.

Attribute Grammars An attribute grammar is a CFG (S, N, T, P) with the following additions: For each grammar symbol x there is a set A(x) of attribute values Each production (rule) has a set of functions that define certain attributes of the nonterminals in the production Each production has a (possibly empty) set of predicates to check for attribute consistency Proposed by Knuth in 1968. In practice (e.g. Actual compilers) attribute grammars are not generally used in a formal way, but the concepts are most definitely incorporated in compilers.

Attribute Grammars The example on page 136 shows the use of an attribute grammar to enhance the BNF of an assignment statement with rules that specify the allowable types (int / real) that can be assigned to each other. e.g. A real (float) cannot be assigned to a variable whose type is int and an int cannot be assigned to a real. Also, the example shows how one can determine the resulting type of an expression.

Attribute Grammars I'm not concerned with us knowing all the ins and outs of attribute grammars, but what is important are the general concepts involved and the intended purpose of them. Attribute grammars are generally not used in practice for a few reasons. Can you guess them?

Attribute Grammars Attribute grammars are generally not used in practice for a few reasons. Can you guess them? Size and complexity of the grammar will be high for a typical modern programming language The many attributes and rules that need to be added cause the grammar to be difficult to read and write, formally The attribute values during parsing would be costly to evaluate (the way it is described in the text.) So, in practice less formal ways are used to check for static semantics at compile-time but the ideas are the same.

Example of how Perl programmers use this short circuit feature to their advantage to make concise readable code. open(filehand, <, $fname) or die can t open file.\n ; Because of the short circuit, the above works in the following way, the open function returns false if the file can t be opened. If that happens the die function is called (see the or operator) which prints the error to STDERR and kills the program. If the file can be opened, then true is returned (but not captured in anything and hence ignored) and the or part is not evaluated (executed.)

Open and die are two of Perl s built-in functions. STDIN, STDOUT, and STDERR are file handles that are automatically available and open in Perl programs. STDIN is the keyboard and the other 2 are the console. We ll come back to more about opening files later, let s instead continue with our discussion of more operators.

if, elsif, else structures (notice the odd spelling of elsif --- there is no e in it.) Why do you think? As expected, the elsif and else portions of the if structure are optional. You can have an if, followed by zero or more elsif s, followed by zero or one else s. Also, there s an unless that can be used instead of the if (but still can use the elsif s and else portions.) unless reverses the test if you had used if

if ($count < 10) { # do something here } Is the same as: unless ($count >= 10) { # do something here }

(example of if/elsif/else) if ($count < 10) { # do something here } elsif ($count >100) { # do something here } else { # do something here }

Previous slides showed how to use if and unless on blocks of code. Interestingly, if, unless, while, until and foreach can all be used as modifiers to a simple statement. Examples: print Hello unless $printing_is_off; $total++ if $increase_total;

while, until, foreach and for are looping structures in Perl. while and for act as you d expect from knowing C++ or Java. until executes its loop until the condition becomes true, whereas while executes its loop until the condition becomes false. It s redundant. So much of Perl is. There's more than one way to do it.

foreach works on list data (e.g. arrays.) Example: foreach $element (@people) { print $element is a person in the array\n ; } # foreach iterates through all values of the array in the parens and uses the variable just after the word foreach to temporarily store the value. Then the code in the { } s executes once for every element of the array. Note: $element and @people are user-defined names (not special to Perl) Create a perl program now that has an array and prints it using code similar to above.

open( INDATA, <, datafile.txt ) or die can t open datafile.txt ; Alternatively, one can combine the mode with the file name in one string: e.g. <datafile.txt Modes: < is input, > is output (writing), >> is append, +< is read-write (assumes file exists) and >+ is read-write (file might not exist).

Another thing to notice about Perl is that for calling the built-in functions we can use parentheses around the arguments or not use the parentheses. e.g. open(fh, <data.txt ); open FH, <data.txt ; # both are valid and do the same thing. But be careful with something like: print ( 7 + 3 ) * 2; # this will cause the parens to enclose the args # and therefore it would print 10 not 20. We would want this instead: print (( 7 + 3 ) * 2);

Use the angle brackets to read a line from a file handle. Use print to write to a filehandle. e.g. print FH A line to be written to file\n ;

When reading lines from STDIN or a file, the line will contain a \n at the end. It is often the case that you wish to get rid of it. Use chomp function to do this. $inline = <STDIN>; chomp($inline); Or chomp($inline = <STDIN>); Chomp removes the end of record marker and returns the # of chars removed. chop is also a function. It removes the last character regardless if it is \n or not and returns the character.

Perl allows chaining of assignments like: $num1 = $num2 = $num3 = 0; # 0 is assigned to num3, then num3 to num2 An interesting thing about return values: The assignment operators are interesting in that they return the variable on the LHS of the assignment as an lvalue. An lvalue is something that can have a value assigned to it. ($temp -= 32) *= 5/9; # the -= in parens returns the $temp as an lvalue which is assigned # a new value with the *= assignment.

Try the following exercise: read each line of input from the file from the text file posted on our notes page. until the line that has eof is read store each line in an element of an array (with the exception of the last line eof.) store the array into a hash (can be done with one line of code) Then, in a loop, ask the user for input (from STDIN) for a product and output its price by looking it up in the hash. Do this until user enters some sentinel that you make him/her aware of at the beginning. Note: the format of the file consists of a product name on one line and its price on the next line, then another product name on the next line and it's price on the next line and so on until the last line of the file which contains the word: eof