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

Similar documents
Programming Languages and Compilers (CS 421)

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

Programming Languages and Compilers (CS 421)

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

Two Problems. Programming Languages and Compilers (CS 421) Type Inference - Example. Type Inference - Outline. Type Inference - Example

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421)

n Parsing function takes a lexing function n Returns semantic attribute of 10/27/16 2 n Contains arbitrary Ocaml code n May be omitted 10/27/16 4

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

A Simple Syntax-Directed Translator

Programming Languages and Compilers (CS 421)

Introduction to Lexical Analysis

A lexical analyzer generator for Standard ML. Version 1.6.0, October 1994

Lexical Analysis. Introduction

Topic 3: Syntax Analysis I

CMSC 350: COMPILER DESIGN

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

CS415 Compilers. Lexical Analysis

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

CPS 506 Comparative Programming Languages. Syntax Specification

CS 11 Ocaml track: lecture 6

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

Chapter 3. Describing Syntax and Semantics ISBN

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

Lecture 9 CIS 341: COMPILERS

Lexical Analysis. Chapter 2

Programming Languages and Compilers (CS 421)

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

n Takes abstract syntax trees as input n In simple cases could be just strings n One procedure for each syntactic category

Theory and Compiling COMP360

Introduction to Lexical Analysis

Lexical Analysis. Lecture 3. January 10, 2018

Programming Languages and Compilers (CS 421)

ML 4 A Lexer for OCaml s Type System

n What is its running time? 9/18/17 2 n poor_rev [1,2,3] = n (poor_rev [1] = n ((poor_rev [1] =

n <exp>::= 0 1 b<exp> <exp>a n <exp>m<exp> 11/7/ /7/17 4 n Read tokens left to right (L) n Create a rightmost derivation (R)

Programming Languages and Compilers (CS 421)

Lexical Analysis. Lecture 2-4

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

Compilers CS S-01 Compiler Basics & Lexical Analysis

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 3

Programming Languages and Compilers (CS 421)

Week 2: Syntax Specification, Grammars

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

A simple syntax-directed

CS S-01 Compiler Basics & Lexical Analysis 1

Implementation of Lexical Analysis

ECE251 Midterm practice questions, Fall 2010

Lexical and Syntax Analysis

The Front End. The purpose of the front end is to deal with the input language. Perform a membership test: code source language?

Alternation. Kleene Closure. Definition of Regular Expressions

Lexical Analysis. Lecture 3-4

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

1. INTRODUCTION TO LANGUAGE PROCESSING The Language Processing System can be represented as shown figure below.

Cunning Plan. Informal Sketch of Lexical Analysis. Issues in Lexical Analysis. Specifying Lexers

Semantic Analysis. Lecture 9. February 7, 2018

Lexical Analysis - An Introduction. Lecture 4 Spring 2005 Department of Computer Science University of Alabama Joel Jones

CS202 Compiler Construction. Christian Skalka. Course prerequisites. Solid programming skills a must.

Syntax. In Text: Chapter 3

CSE302: Compiler Design

CSCI 2041: Advanced Language Processing

Part 5 Program Analysis Principles and Techniques

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

Compilers CS S-01 Compiler Basics & Lexical Analysis

Compiling Regular Expressions COMP360

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Lexical Scanning COMP360

Example. Programming Languages and Compilers (CS 421) Disambiguating a Grammar. Disambiguating a Grammar. Steps to Grammar Disambiguation

1 Lexical Considerations

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

MP 3 A Lexer for MiniJava

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

Building Compilers with Phoenix

Implementation of Lexical Analysis

Formal Languages and Compilers Lecture VI: Lexical Analysis

10/4/18. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntactic Analysis

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

Programming Language Syntax and Analysis

LECTURE 3. Compiler Phases

Chapter 4. Lexical and Syntax Analysis

Lexical Analysis. Finite Automata

Lexical Analysis. Finite Automata

CSCE 314 Programming Languages

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Introduction to Lexing and Parsing

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

ICOM 4036 Spring 2004

CMSC 330: Organization of Programming Languages. Context Free Grammars

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

MP 3 A Lexer for MiniJava

CSE 401 Midterm Exam Sample Solution 2/11/15

CSCI312 Principles of Programming Languages!

Syntax. A. Bellaachia Page: 1

COMP455: COMPILER AND LANGUAGE DESIGN. Dr. Alaa Aljanaby University of Nizwa Spring 2013

Lecture Outline. COMP-421 Compiler Design. What is Lex? Lex Specification. ! Lexical Analyzer Lex. ! Lex Examples. Presented by Dr Ioanna Dionysiou

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions

Transcription:

Programming Languages & Compilers Programming Languages and Compilers (CS 421) Three Main Topics of the Course I II III Sasa Misailovic 4110 SC, UIUC https://courses.engr.illinois.edu/cs421/fa2017/cs421a New Programming Paradigm Language Translation Language Semantics Based in part on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, and Elsa L Gunter 10/24/2017 1 10/24/2017 2 Programming Languages & Compilers Type Systems II : Language Translation Lexing and Parsing Interpretation Major Phases of a Compiler Source Program Lex Tokens Parse Abstract Syntax Semantic Analysis Symbol Table Translate Intermediate Representation Optimize Optimized IR Instruction Selection Unoptimized Machine- Specific Assembly Language Optimize Optimized Machine-Specific Assembly Language Emit code Assembly Language Assembler Relocatable Object Code Linker Machine Code 10/24/2017 3 Modified from Modern Compiler Implementation in ML, by Andrew Appel Where We Are Going Next? We want to turn strings (code) into computer instructions Done in phases Break the big strings into tokens (lex) Turn tokens into abstract syntax trees (parse) Translate abstract syntax trees into executable instructions (interpret or compile) Meta-discourse Language Syntax and Semantics Syntax - Regular Expressions, DFSAs and NDFSAs - Grammars Semantics - Natural Semantics - Transition Semantics 10/24/2017 5 10/24/2017 6 1

Language Syntax Syntax is the description of which strings of symbols are meaningful expressions in a language It takes more than syntax to understand a language; need meaning (semantics) too Syntax is the entry point Syntax of English Language Pattern 1 Pattern 2 10/24/2017 7 10/24/2017 8 Elements of Syntax Character set previously always ASCII, now often 64 character sets Keywords usually reserved Special constants cannot be assigned to Identifiers can be assigned to Operator symbols Delimiters (parenthesis, braces, brackets) Blanks (aka white space) Elements of Syntax Expressions if... then begin... ;... end else begin... ;... end Type expressions typexpr 1 -> typexpr 2 Declarations (in functional languages) let pattern = expr Statements (in imperative languages) a = b + c Subprograms let pattern 1 = expr 1 in expr 10/24/2017 9 10/24/2017 10 Elements of Syntax Modules Interfaces Classes (for object-oriented languages) Lexing and Parsing Converting strings to abstract syntax trees done in two phases Lexing: Converting string (or streams of characters) into lists (or streams) of tokens (the words of the language) Specification Technique: Regular Expressions Parsing: Convert a list of tokens into an abstract syntax tree Specification Technique: BNF Grammars 10/24/2017 11 10/24/2017 12 2

Formal Language Descriptions Regular expressions, regular grammars, finite state automata Context-free grammars, BNF grammars, syntax diagrams Whole family more of grammars and automata covered in automata theory Grammars Grammars are formal descriptions of which strings over a given character set are in a particular language Language designers write grammar Language implementers use grammar to know what programs to accept Language users use grammar to know how to write legitimate programs 10/24/2017 13 10/24/2017 14 Regular Expressions - Review Start with a given character set a, b, c Each character is a regular expression It represents the set of one string containing just that character Regular Expressions If x and y are regular expressions, then xy is a regular expression It represents the set of all strings made from first a string described by x then a string described by y If x={a,ab} and y={c,d} then xy ={ac,ad,abc,abd}. If x and y are regular expressions, then x y is a regular expression It represents the set of strings described by either x or y If x={a,ab} and y={c,d} then x y={a,ab,c,d} 10/24/2017 15 10/24/2017 16 Regular Expressions If x is a regular expression, then so is (x) It represents the same thing as x If x is a regular expression, then so is x* It represents strings made from concatenating zero or more strings from x If x = {a,ab} then x* ={,a,ab,aa,aab,abab, } It represents { }, set containing the empty string Φ It represents { }, the empty set Example Regular Expressions (0 1)*1 The set of all strings of 0 s and 1 s ending in 1, {1, 01, 11, } a*b(a*) The set of all strings of a s and b s with exactly one b ((01) (10))* You tell me Regular expressions (equivalently, regular grammars) important for lexing, breaking strings into recognized words 10/24/2017 17 10/24/2017 18 3

Regular Grammars Example Subclass of BNF (covered in detail sool) Only rules of form <nonterminal>::=<terminal><nonterminal> or <nonterminal>::=<terminal> or <nonterminal>::=ε Defines same class of languages as regular expressions Important for writing lexers (programs that convert strings of characters into strings of tokens) Close connection to nondeterministic finite state automata nonterminals = states; rule = edge 19 Regular grammar: <Balanced> ::= <Balanced> ::= 0<OneAndMore> <Balanced> ::= 1<ZeroAndMore> <OneAndMore> ::= 1<Balanced> <ZeroAndMore> ::= 0<Balanced> Generates even length strings where every initial substring of even length has same number of 0 s as 1 s 10/24/2017 20 Example: Lexing Regular expressions good for describing lexemes (words) in a programming language Identifier = (a b z A B Z) (a b z A B Z 0 1 9)* Digit = (0 1 9) Number = 0 (1 9)(0 9)* - (1 9)(0 9)* Keywords: if = if, while = while, 10/24/2017 21 Implementing Regular Expressions Regular expressions reasonable way to generate strings in language Not so good for recognizing when a string is in language Problems with Regular Expressions which option to choose, how many repetitions to make Answer: finite state automata Should have seen in CS374 10/24/2017 22 Lexing Different syntactic categories of words : tokens Example: Convert sequence of characters into sequence of strings, integers, and floating point numbers. "asd 123 jkl 3.14" will become: [String "asd"; Int 123; String "jkl"; Float 3.14] Lex, ocamllex Could write the reg exp, then translate to DFA by hand A lot of work Better: Write program to take reg exp as input and automatically generates automata Lex is such a program ocamllex version for ocaml 10/24/2017 23 10/24/2017 24 4

How to do it To use regular expressions to parse our input we need: Some way to identify the input string call it a lexing buffer Set of regular expressions, Corresponding set of actions to take when they are matched. How to do it The lexer will take the regular expressions and generate a state machine. The state machine will take our lexing buffer and apply the transitions... If we reach an accepting state from which we can go no further, the machine will perform the appropriate action. 10/24/2017 25 10/24/2017 26 Mechanics Put table of reg exp and corresponding actions (written in ocaml) into a file <filename>.mll Call ocamllex <filename>.mll Produces Ocaml code for a lexical analyzer in file <filename>.ml Sample Input rule main = parse ['0'-'9']+ { print_string "Int\n"} ['0'-'9']+'.'['0'-'9']+ { print_string "Float\n"} ['a'-'z']+ { print_string "String\n"} _ { main lexbuf } { let newlexbuf = (Lexing.from_channel stdin) in print_string "Ready to lex.\n"; main newlexbuf } 10/24/2017 27 10/24/2017 28 General Input { header } let ident = regexp... rule entrypoint [arg1... argn] = parse regexp { action }... regexp { action } and entrypoint [arg1... argn] = parse...and... { trailer } Ocamllex Input header and trailer contain arbitrary ocaml code put at top an bottom of <filename>.ml let ident = regexp... Introduces ident for use in later regular expressions 10/24/2017 29 10/24/2017 30 5

Ocamllex Input <filename>.ml contains one lexing function per entrypoint Name of function is name given for entrypoint Each entry point becomes an Ocaml function that takes n+1 arguments, the extra implicit last argument being of type Lexing.lexbuf arg1... argn are for use in action Ocamllex Regular Expression Single quoted characters for letters: a _: (underscore) matches any letter Eof: special end_of_file marker Concatenation same as usual string : concatenation of sequence of characters e 1 e 2 : choice - what was e 1 e 2 10/24/2017 31 10/24/2017 32 Ocamllex Regular Expression [c 1 - c 2 ]: choice of any character between first and second inclusive, as determined by character codes [^c 1 - c 2 ]: choice of any character NOT in set e*: same as before e+: same as e e* e?: option - was e 1 Ocamllex Regular Expression e 1 # e 2 : the characters in e 1 but not in e 2 ; e 1 and e 2 must describe just sets of characters ident: abbreviation for earlier reg exp in let ident = regexp e 1 as id: binds the result of e 1 to id to be used in the associated action 10/24/2017 33 10/24/2017 34 Ocamllex Manual More details can be found at http://caml.inria.fr/pub/docs/manualocaml/lexyacc.html Example : test.mll { type result = Int of int Float of float String of string } let digit = ['0'-'9'] let digits = digit + let lower_case = ['a'-'z'] let upper_case = ['A'-'Z'] let letter = upper_case lower_case let letters = letter + 10/24/2017 35 10/24/2017 36 6

Example : test.mll Example rule main = parse (digits)'.'digits as f { Float (float_of_string f) } digits as n { Int (int_of_string n) } letters as s { String s} _ { main lexbuf } { let newlexbuf = (Lexing.from_channel stdin) in print_string "Ready to lex."; print_newline (); main newlexbuf } # #use "test.ml";; val main : Lexing.lexbuf -> result = <fun> val ocaml_lex_main_rec : Lexing.lexbuf -> int -> result = <fun> Ready to lex. hi there 234 5.2 - : result = String "hi" What happened to the rest?!? 10/24/2017 37 10/24/2017 38 Example # let b = Lexing.from_channel stdin;; # main b;; hi 673 there - : result = String "hi" # main b;; - : result = Int 673 # main b;; - : result = String "there" Problem How to get lexer to look at more than the first token at one time? Answer: action has to tell it to -- recursive calls Side Benefit: can add state into lexing Note: already used this with the _ case 10/24/2017 39 10/24/2017 41 Example rule main = parse (digits) '.' digits as f { Float (float_of_string f) :: main lexbuf} digits as n { Int (int_of_string n) :: main lexbuf } letters as s { String s :: main lexbuf} eof { [] } _ { main lexbuf } Example Results Ready to lex. hi there 234 5.2 - : result list = [String "hi"; String "there"; Int 234; Float 5.2] # Used Ctrl-d to send the end-of-file signal 10/24/2017 42 10/24/2017 43 7

Dealing with comments First Attempt let open_comment = "(*" let close_comment = "*)" rule main = parse (digits) '.' digits as f { Float (float_of_string f) :: main lexbuf} digits as n { Int (int_of_string n) :: main lexbuf } letters as s { String s :: main lexbuf} Dealing with comments open_comment { comment lexbuf} eof { [] } _ { main lexbuf } and comment = parse close_comment { main lexbuf } _ { comment lexbuf } 10/24/2017 44 10/24/2017 45 Dealing with nested comments Dealing with nested comments rule main = parse open_comment { comment 1 lexbuf} eof { [] } _ { main lexbuf } and comment depth = parse open_comment { comment (depth+1) lexbuf } close_comment { if depth = 1 then main lexbuf else comment (depth - 1) lexbuf } _ { comment depth lexbuf } rule main = parse (digits) '.' digits as f { Float (float_of_string f) :: main lexbuf} digits as n { Int (int_of_string n) :: main lexbuf } letters as s { String s :: main lexbuf} open_comment { (comment 1 lexbuf} eof { [] } _ { main lexbuf } 10/24/2017 46 10/24/2017 47 Dealing with nested comments and comment depth = parse open_comment { comment (depth+1) lexbuf } close_comment { if depth = 1 then main lexbuf else comment (depth - 1) lexbuf } _ { comment depth lexbuf } Types of Formal Language Descriptions Regular expressions, regular grammars Context-free grammars, BNF grammars, syntax diagrams Finite state automata Whole family more of grammars and automata covered in automata theory 10/24/2017 48 10/24/2017 49 8

Sample Grammar Language: Parenthesized sums of 0 s and 1 s <Sum> ::= 0 <Sum >::= 1 <Sum> ::= <Sum> + <Sum> <Sum> ::= (<Sum>) BNF Grammars Start with a set of characters, a,b,c, We call these terminals Add a set of different characters, X,Y,Z, We call these nonterminals One special nonterminal S called start symbol 10/24/2017 50 10/24/2017 51 BNF Grammars BNF rules (aka productions) have form X ::= y where X is any nonterminal and y is a string of terminals and nonterminals BNF grammar is a set of BNF rules such that every nonterminal appears on the left of some rule 10/24/2017 52 Sample Grammar Terminals: 0 1 + ( ) Nonterminals: <Sum> Start symbol = <Sum> <Sum> ::= 0 <Sum >::= 1 <Sum> ::= <Sum> + <Sum> <Sum> ::= (<Sum>) Can be abbreviated as <Sum> ::= 0 1 <Sum> + <Sum> (<Sum>) 10/24/2017 53 BNF Deriviations Given rules X::= yzw and Z::=v we may replace Z by v to say X => yzw => yvw Sequence of such replacements called derivation Derivation called right-most if always replace the right-most non-terminal Start with the start symbol: <Sum> => 10/24/2017 54 10/24/2017 55 9

Pick a non-terminal <Sum> => Pick a rule and substitute: <Sum> ::= <Sum> + <Sum> 10/24/2017 56 10/24/2017 57 Pick a non-terminal: Pick a rule and substitute: <Sum> ::= ( <Sum> ) 10/24/2017 58 10/24/2017 59 Pick a non-terminal: Pick a rule and substitute: <Sum> ::= <Sum> + <Sum> 10/24/2017 60 10/24/2017 61 10

Pick a non-terminal: Pick a rule and substitute: <Sum >::= 1 => ( <Sum> + 1 ) + <Sum> 10/24/2017 62 10/24/2017 63 Pick a non-terminal: => ( <Sum> + 1 ) + <Sum> Pick a rule and substitute: <Sum >::= 0 => ( <Sum> + 1 ) + <Sum> => ( <Sum> + 1 ) + 0 10/24/2017 64 10/24/2017 65 Pick a non-terminal: => ( <Sum> + 1 ) + <Sum> => ( <Sum> + 1 ) + 0 Pick a rule and substitute <Sum> ::= 0 => ( <Sum> + 1 ) + <Sum> => ( <Sum> + 1 ) 0 => ( 0 + 1 ) + 0 10/24/2017 66 10/24/2017 67 11

( 0 + 1 ) + 0 is generated by grammar <Sum> ::= 0 1 <Sum> + <Sum> (<Sum>) <Sum> => => ( <Sum> + 1 ) + <Sum> => ( <Sum> + 1 ) + 0 => ( 0 + 1 ) + 0 10/24/2017 68 10/24/2017 69 12