Appendix A The DL Language

Size: px
Start display at page:

Download "Appendix A The DL Language"

Transcription

1 Appendix A The DL Language This appendix gives a description of the DL language used for many of the compiler examples in the book. DL is a simple high-level language, only operating on integer data, with a syntax looking vaguely like a simple C. The syntax of DL is defined by the following BNF grammar: <program> ::= <block> <declarations> <block> <declarations> ::= <declaration> <declaration> <declarations> <declaration> ::= <variabledeclaration> <functiondeclaration> <variabledeclaration> ::= int <vardeflist> ; <vardeflist> ::= <vardec> <vardec>, <vardeflist> <vardec> ::= <identifier> <identifier> [ <constant> ] <functiondeclaration> ::= <identifier> (); <functionbody> <identifier> ( <arglist> ); <functionbody> <functionbody> ::= <variabledeclaration> <block> <block> <arglist> ::= <identifier> <identifier>, <arglist> <block> ::= { <statementlist> } <statementlist> ::= <statement> <statement> ; <statementlist> <statement> ::= <assignment> <ifstatement> <whilestatement> <block> <printstatement> <readstatement> <returnstatement> <empty> <assignment> ::= <identifier> = <expression> <identifier> [ <expression> ] = <expression> <ifstatement> ::= if ( <bexpression> ) <block> else <block> if ( <bexpression> ) <block> <whilestatement> ::= while ( <bexpression> ) <block> <printstatement> ::= print ( <expression> ) <readstatement> ::= read ( <identifier> ) <returnstatement> ::= return <expression> <expression> ::= <expression> <addingop> <term> <term> <addingop> <term> <bexpression> ::= <expression> <relop> <expression> <relop> ::= < <= == >= >!= <addingop> ::= + - <term> ::= <term> <multop> <factor> <factor> <multop> ::= * / <arguments> ::= <expression> <expression>, <arguments> Springer International Publishing AG 2017 D. Watson, A Practical Approach to Compiler Construction, Undergraduate Topics in Computer Science, DOI /

2 248 Appendix A: The DL Language <factor> ::= <constant> <identifier> <identifier> [ <expression>] ( <expression> ) <identifier> ( <arguments> ) <identifier> ( ) <constant> ::= <digit> <digit> <constant> <identifier> ::= <identifier> <letterordigit> <letter> <letterordigit> ::= <letter> <digit> <letter> ::= a b c d e f g h i j k l m n o p q r s t u v w x y z <digit> ::= <empty> has the obvious meaning Names have to be declared before they are used. This implies that mutually recursive functions are not allowed. All functions return a single integer result. Comments (zero or more characters enclosed between the standard C comment brackets /*...*/) can be inserted. DL has rudimentary support for one-dimensional arrays. The declaration int a[3] declares an array of three elements, referenced as a[0], a[1] and a[2]. Names are scoped in the traditional way. A simple program written in this language is as follows: factorial(n); { if (n==0) { return 1 } else { return n*factorial(n-1) } } int x; { x=1; while (x<=10) { print(factorial(x)); x = x + 1 } } The tree generated for this program using the code outlined in this book has two parts. The first is for the factorial function. N_FUNCTIONDEC - function definition chain, this: N_FUNCTION - function def, number 0 N_FUNCTION - function def, arglist: N_PARMS - parameter, runtime offset 0 N_PARMS - parameter, next, index is: N_FUNCTION - function def, block: N_SLIST - Statement list, this statement: N_IF - if, condition: N_EQ - eq, lhs is: N_ID - identifier, runtime offset 0 N_EQ - eq, rhs is: N_CONST - const, value is: 0 N_IF - if, thenpart:

3 Appendix A: The DL Language 249 N_SLIST - Statement list, this statement: N_RETURN - return, value is: N_CONST - const, value is: 1 N_SLIST - Statement list, continued... N_IF - if, elsepart: N_SLIST - Statement list, this statement: N_RETURN - return, value is: N_MUL - mul, lhs is: N_ID - identifier, runtime offset 0 N_MUL - mul, rhs is: N_FNCALL - function call, function 0 (factorial) N_FNCALL - function call, arguments: N_ARG - argument list, expression: N_MINUS - minus, lhs is: N_ID - identifier, runtime offset 0 N_MINUS - minus, rhs is: N_CONST - const, value is: 1 N_ARG - argument list, next argument: N_SLIST - Statement list, continued... N_SLIST - Statement list, continued... N_FUNCTIONDEC - function definition chain, next: The second is for the main program. N_SLIST - Statement list, this statement: N_ASSIGN - Assign to variable at offset 0, expression is: N_CONST - const, value is: 1 N_SLIST - Statement list, continued... N_SLIST - Statement list, this statement: N_WHILE - while, condition: N_LE - le, lhs is: N_ID - identifier, runtime offset 0 N_LE - le, rhs is: N_CONST - const, value is: 10 N_WHILE - while, block: N_SLIST - Statement list, this statement: N_PRINT - print, expression is: N_FNCALL - function call, function 0 (factorial) N_FNCALL - function call, arguments: N_ARG - argument list, expression: N_ID - identifier, runtime offset 0 N_ARG - argument list, next argument: N_SLIST - Statement list, continued... N_SLIST - Statement list, this statement: N_ASSIGN - Assign to variable at offset 0, expression is: N_PLUS - plus, lhs is: N_ID - identifier, runtime offset 0 N_PLUS - plus, rhs is: N_CONST - const, value is: 1 N_SLIST - Statement list, continued... N_SLIST - Statement list, continued...

4 250 Appendix A: The DL Language The intermediate representation generated for this program is as follows. factorial(1) if (vl0!=0) goto l1 r0 = 1 return goto l2 l1: r2 = vl0-1 param r2 call factorial r3 = r0 r1 = vl0 * r3 r0 = r1 return l2: r0 = 0 return :!MAIN!(1) vg0 = 1 l3: if (vg0>10) goto l4 param vg0 call factorial r4 = r0 print r4 r5 = vg0 + 1 vg0 = r5 goto l3 l4: r0 = 0 return

5 Index A Abstraction, 2 Abstract syntax tree, 26, 30, 90, 111 Addressing modes, 209 ALGOL 60, 17, 34, 231 ALGOL 68, 20, 34 Aliasing, 219 Ambiguity, 25, 65, 86 Amsterdam Compiler Kit, 239 Analysis phase, 15 Argument passing, 222 call by reference, 223 call by value, 222 Arithmetic expressions, 166 Arrays bound checking, 152 multi-dimensional, 151 storage allocation, 150 Assembler, 1, 14 Assembly languages, 1 Associativity, 19, 121 Attribute grammars, 21, 153 Attributes inherited, 153 synthesised, 153 Autoincrement, autodecrement, 209 B Back-end, 28 Backtracking, 83 Backus Naur Form, see BNF Backus Normal Form, see BNF Basic blocks, , 180 BCPL, 240 bison, , BNF, 17 recursion, 18 specifying context, 20 Boolean expressions, 167 short-circuited evaluation, 167 Bootstrapping, 238 Bottom-up parsing, 27, , C Cache memory, Canonical parse, 88 Chomsky hierarchy, CISC, 207, 227 COBOL, 2 Code generation, 32, Code generator generation, 230 Comments, 40 Common subexpressions, 158, comp.compilers, 11, 72 Compiler, 14, 15 Compiler phases, 29 Conditional statements, 167 Constant folding, 181 Constant propagation, 181 Context-free grammar, 24 Context-sensitive grammar, 23 Control flow graph, Cross-compilation, 237 D Data dependence graphs, 161 Data flow, 188 Dead registers, 187 Debugger, 241 Declarations, Dependence, Springer International Publishing AG 2017 D. Watson, A Practical Approach to Compiler Construction, Undergraduate Topics in Computer Science, DOI /

6 252 Index antidependence, 198 output dependence, 198 true dependence, 197 Derivation, 18, canonical, 76 leftmost, 76, rightmost, 76 DL, 46, 113, 124, Dynamic chain, 220 Dynamic class loading, 5, 242 Dynamic typing, 143 E EBNF, 19 Efficiency, 4 5 Embedded systems, 5, 242 Error correction, 134 Error handling, Error synchronisation, 133 Extended Backus Naur Form, see EBNF F Factoring, Finite-state grammar, 24 Finite-state machine, 24, accepting state, 59 deterministic, 60 non-deterministic, 60 starting state, 59 FIRST and FOLLOW sets, 133 flex, 61 71, 126 FORTRAN, 2, 34, 56 Free grammar, 23 Front-end, 28 Functions, 170 arguments, 165 call and return implementation, 221 implementation, 219 G GCC, 11, 232, 239 GCD test, 200 Global variables, 150, 165 GNU Compiler Collection, see GCC Grammar, ambiguity, 25 attribute, 153 context-free, 24 context-sensitive, 23 finite-state, 24 free, 23 LL(k), 86 LR(k), 88 production rules, 22 regular, 24 sentence, 23 sentential form, 23 starting symbol, 22 unrestricted, 23 Graph colouring, H Handle, 89 Hash table, 134 I Implementation strategies, implementation language, Induction variables, 192 Inline assembly code, 4 Instruction-level parallelism, 195, Instruction scheduler, 224 Instruction selection, Intermediate code, 28, 31, arithmetic expressions, 166 boolean expressions, 167 conditional statements, 167 function arguments, 165 functions, 170 global variables, 165 graph-based, linear, local variables, 165 Interpreter, 7 9, 15 16, 236 J Java Virtual Machine, see JVM JavaCC, 96 Just-in-time compilation, 242 JVM, 5, 34, 156, 173, 209, 242 K Keywords, 40, 43 L Language definition, 17 Left context, 89 Left recursion, 85 86, 100, 117 lex, 61, 103 Lexical analysis, 29, 37 72

7 Index 253 error recovery, 53 Lexical tokens, 29, character constants, 40 comments, 40, 43, 52 identifiers, 40 42, 50, 54 keywords, 40 numerical constants, 40, 42, 50, 55 reserved words, 40, 43, 50, 54 string constants, 40 white space, 40, 43, 47 Library, 3 Linkage information, 220 Linker, 241 lint, 10 Live range, 214 Live variable analysis, 190 LL(k) grammar, 86 LLVM, 11, 239 Load/store architecture, 207 Local variables, 147, 165 Lookahead, 46, 83 84, Loop optimisation, Loop unrolling, 193 Loop-invariant code, Loosely coupled systems, 195 LR(k) grammar, 88 M Machine code, 1 Machine-dependent optimisation, 32 Machine-independent optimisation, 31 Memory hierarchy, 227 Metalanguage, 17 BNF, 17 EBNF, 19 metasymbol, 18 Multicore processors, 194 N Non-local optimisation, Non-terminal symbols, 17 Numerical calculator, 107 O Old stack pointer, 220 Operator overloading, 144 Operator precedence, 19 Optimisation, 32 machine-dependent, 32, machine-independent, 31, P Panic-mode error recovery, 134 Parallelism, Parse tree, 26, 30, 76 77, 110 flattening, 31 Parsing, 24, 30, 75, bottom-up, 27, deterministic, 84 precedence, 102 predictive, 84 recursive descent, 84 shift-reduce, top down, top-down, 27 Pascal, 19, 20, 221 Peephole optimisation, 201, Pipeline, 194, 224 PL/I, 2, 56 Portability, Precedence, 19 Precedence parsing, 102 Predictive parser, 84, 100 Production rules, 22 Program dependence graph, 161 Program development environment, 241 R Recursive descent parser, 84 Reduce/reduce conflict, 107 Reduced instruction set computers, see RISC Reduction, 25 Redundant code elimination, Redundant variables, 190 Register allocation, Register interference graph, 215 Register-memory machines, 212 Register-register machines, 213 Register spilling, 213 Regular expression, 24, 45, directed graph, 58 Regular grammar, 24 Reserved words, 39, 40 Return address, 220 RISC, 6, 207 Runtime library, 240 S Scope rules, 135 Semantic analysis, 31, Semantics, 14 definition, 16 natural language, 21

8 254 Index reference implementation, 21 specification, 21 Sentence, 23 Sentential form, 23 Shift/reduce conflict, 107 Shift-reduce parsing, Software engineering, 7 Stack-based storage, 148 Stack frame, 149 Stack-based storage, 219 Starting symbol, 22, 106 Static chain, 221 Static single assignment form (SSA), Static typing, 143 Storage allocation arrays, 150 display, 150 dynamic allocation, 149 nested functions, 150 stack, 148 static allocation, 147 static chain, 150 structures and unions, 152 Storage management, Strength reduction, 193 Superoptimisation, Superscalar processors, 224 Symbol table, 33, 45, 114, 122, stack, 114 Syntax definition, 16 Syntax analysis, 30, 75, error recovery, 89 90, Syntax diagrams, 20 Syntax-directed translation, Synthesis phase, 15 T T-diagrams, 34 Terminal symbols, 17 Testing, 241 Three-address code, 156, 162 Tokens, see lexical tokens Top-down parsing, 27, , Transition diagram, 59 Transition table, 60 Tree generation, 90 91, , , Tree pattern matching, 210 Tree printing, 123 Tree tiling, 211 Two-level grammar, 20 Type checking, Typedef declaration, 42, 136 Type equivalence, 144 name equivalence, 144 structural equivalence, 144 Types, 142, 173 U Unrestricted grammar, 23 User-defined types, 142 V Vector instructions, 194 Virtual machine, 7, 14 16, Virtual registers, 162, 213 Y yacc, 103

Appendix Set Notation and Concepts

Appendix Set Notation and Concepts Appendix Set Notation and Concepts In mathematics you don t understand things. You just get used to them. John von Neumann (1903 1957) This appendix is primarily a brief run-through of basic concepts from

More information

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1 Table of Contents About the Authors... iii Introduction... xvii Chapter 1: System Software... 1 1.1 Concept of System Software... 2 Types of Software Programs... 2 Software Programs and the Computing Machine...

More information

CS5363 Final Review. cs5363 1

CS5363 Final Review. cs5363 1 CS5363 Final Review cs5363 1 Programming language implementation Programming languages Tools for describing data and algorithms Instructing machines what to do Communicate between computers and programmers

More information

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised: EDAN65: Compilers, Lecture 06 A LR parsing Görel Hedin Revised: 2017-09-11 This lecture Regular expressions Context-free grammar Attribute grammar Lexical analyzer (scanner) Syntactic analyzer (parser)

More information

Syntax. In Text: Chapter 3

Syntax. In Text: Chapter 3 Syntax In Text: Chapter 3 1 Outline Syntax: Recognizer vs. generator BNF EBNF Chapter 3: Syntax and Semantics 2 Basic Definitions Syntax the form or structure of the expressions, statements, and program

More information

Compiler Construction Using

Compiler Construction Using Compiler Construction Using Java, JavaCC, and Yacc ANTHONY J. DOS REIS Stale University ofnew York at New Pallz IEEE computer society WILEY A JOHN WILEY & SONS, INC., PUBLICATION Preface xv Chapter 1 Strings,

More information

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI UNIT I - LEXICAL ANALYSIS 1. What is the role of Lexical Analyzer? [NOV 2014] 2. Write

More information

CPS 506 Comparative Programming Languages. Syntax Specification

CPS 506 Comparative Programming Languages. Syntax Specification CPS 506 Comparative Programming Languages Syntax Specification Compiling Process Steps Program Lexical Analysis Convert characters into a stream of tokens Lexical Analysis Syntactic Analysis Send tokens

More information

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation Language Implementation Methods The Design and Implementation of Programming Languages Compilation Interpretation Hybrid In Text: Chapter 1 2 Compilation Interpretation Translate high-level programs to

More information

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

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield CMPS 3500 Programming Languages Dr. Chengwei Lei CEECS California State University, Bakersfield Chapter 3 Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing

More information

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

COP 3402 Systems Software Top Down Parsing (Recursive Descent) COP 3402 Systems Software Top Down Parsing (Recursive Descent) Top Down Parsing 1 Outline 1. Top down parsing and LL(k) parsing 2. Recursive descent parsing 3. Example of recursive descent parsing of arithmetic

More information

Chapter 4. Lexical and Syntax Analysis

Chapter 4. Lexical and Syntax Analysis Chapter 4 Lexical and Syntax Analysis Chapter 4 Topics Introduction Lexical Analysis The Parsing Problem Recursive-Descent Parsing Bottom-Up Parsing Copyright 2012 Addison-Wesley. All rights reserved.

More information

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

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units Syntax - the form or structure of the expressions, statements, and program units Semantics - the meaning of the expressions, statements, and program units Who must use language definitions? 1. Other language

More information

Wednesday, September 9, 15. Parsers

Wednesday, September 9, 15. Parsers Parsers What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

More information

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs: What is a parser Parsers A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

More information

2068 (I) Attempt all questions.

2068 (I) Attempt all questions. 2068 (I) 1. What do you mean by compiler? How source program analyzed? Explain in brief. 2. Discuss the role of symbol table in compiler design. 3. Convert the regular expression 0 + (1 + 0)* 00 first

More information

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics ISBN Chapter 3 Describing Syntax and Semantics ISBN 0-321-49362-1 Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Copyright 2009 Addison-Wesley. All

More information

4. An interpreter is a program that

4. An interpreter is a program that 1. In an aboslute loading scheme, which loader function is accomplished by programmer? A. Allocation B. LInking C. Reallocation D. both (A) and (B) 2. A compiler program written in a high level language

More information

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done What is a compiler? What is a compiler? Traditionally: Program that analyzes and translates from a high level language (e.g., C++) to low-level assembly language that can be executed by hardware int a,

More information

CST-402(T): Language Processors

CST-402(T): Language Processors CST-402(T): Language Processors Course Outcomes: On successful completion of the course, students will be able to: 1. Exhibit role of various phases of compilation, with understanding of types of grammars

More information

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573 What is a compiler? Xiaokang Qiu Purdue University ECE 573 August 21, 2017 What is a compiler? What is a compiler? Traditionally: Program that analyzes and translates from a high level language (e.g.,

More information

Syntax. 2.1 Terminology

Syntax. 2.1 Terminology Syntax 2 Once you ve learned to program in one language, learning a similar programming language isn t all that hard. But, understanding just how to write in the new language takes looking at examples

More information

SYLLABUS UNIT - I UNIT - II UNIT - III UNIT - IV CHAPTER - 1 : INTRODUCTION CHAPTER - 4 : SYNTAX AX-DIRECTED TRANSLATION TION CHAPTER - 7 : STORA

SYLLABUS UNIT - I UNIT - II UNIT - III UNIT - IV CHAPTER - 1 : INTRODUCTION CHAPTER - 4 : SYNTAX AX-DIRECTED TRANSLATION TION CHAPTER - 7 : STORA Contents i SYLLABUS UNIT - I CHAPTER - 1 : INTRODUCTION Programs Related to Compilers. Translation Process, Major Data Structures, Other Issues in Compiler Structure, Boot Strapping and Porting. CHAPTER

More information

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design PSD3A Principles of Compiler Design Unit : I-V 1 UNIT I - SYLLABUS Compiler Assembler Language Processing System Phases of Compiler Lexical Analyser Finite Automata NFA DFA Compiler Tools 2 Compiler -

More information

Programming Language Syntax and Analysis

Programming Language Syntax and Analysis Programming Language Syntax and Analysis 2017 Kwangman Ko (http://compiler.sangji.ac.kr, kkman@sangji.ac.kr) Dept. of Computer Engineering, Sangji University Introduction Syntax the form or structure of

More information

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics Chapter 3 Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings of Programs:

More information

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview n Tokens and regular expressions n Syntax and context-free grammars n Grammar derivations n More about parse trees n Top-down and

More information

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program. COMPILER DESIGN 1. What is a compiler? A compiler is a program that reads a program written in one language the source language and translates it into an equivalent program in another language-the target

More information

CS 415 Midterm Exam Spring 2002

CS 415 Midterm Exam Spring 2002 CS 415 Midterm Exam Spring 2002 Name KEY Email Address Student ID # Pledge: This exam is closed note, closed book. Good Luck! Score Fortran Algol 60 Compilation Names, Bindings, Scope Functional Programming

More information

Context-free grammars

Context-free grammars Context-free grammars Section 4.2 Formal way of specifying rules about the structure/syntax of a program terminals - tokens non-terminals - represent higher-level structures of a program start symbol,

More information

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 20th May 2016 Time: 14:00-16:00

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 20th May 2016 Time: 14:00-16:00 Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE Compilers Date: Friday 20th May 2016 Time: 14:00-16:00 Please answer any THREE Questions from the FIVE Questions provided This is a CLOSED

More information

Principles of Programming Languages [PLP-2015] Detailed Syllabus

Principles of Programming Languages [PLP-2015] Detailed Syllabus Principles of Programming Languages [PLP-2015] Detailed Syllabus This document lists the topics presented along the course. The PDF slides published on the course web page (http://www.di.unipi.it/~andrea/didattica/plp-15/)

More information

Compilers and Interpreters

Compilers and Interpreters Overview Roadmap Language Translators: Interpreters & Compilers Context of a compiler Phases of a compiler Compiler Construction tools Terminology How related to other CS Goals of a good compiler 1 Compilers

More information

Compiler Construction: Parsing

Compiler Construction: Parsing Compiler Construction: Parsing Mandar Mitra Indian Statistical Institute M. Mitra (ISI) Parsing 1 / 33 Context-free grammars. Reference: Section 4.2 Formal way of specifying rules about the structure/syntax

More information

Describing Syntax and Semantics

Describing Syntax and Semantics Describing Syntax and Semantics Introduction Syntax: the form or structure of the expressions, statements, and program units Semantics: the meaning of the expressions, statements, and program units Syntax

More information

INSTITUTE OF AERONAUTICAL ENGINEERING

INSTITUTE OF AERONAUTICAL ENGINEERING INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad - 00 043 INFORMATION TECHNOLOGY TUTORIAL QUESTION BANK Name AUTOMATA AND COMPILER DESIGN Code A03 Class III B. Tech I Semester Branch

More information

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview Tokens and regular expressions Syntax and context-free grammars Grammar derivations More about parse trees Top-down and bottom-up

More information

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc. Syntax Syntax Syntax defines what is grammatically valid in a programming language Set of grammatical rules E.g. in English, a sentence cannot begin with a period Must be formal and exact or there will

More information

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F). CS 2210 Sample Midterm 1. Determine if each of the following claims is true (T) or false (F). F A language consists of a set of strings, its grammar structure, and a set of operations. (Note: a language

More information

ICOM 4036 Spring 2004

ICOM 4036 Spring 2004 Language Specification and Translation ICOM 4036 Spring 2004 Lecture 3 Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-1 Language Specification and Translation Topics Structure of a Compiler

More information

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468 Parsers Xiaokang Qiu Purdue University ECE 468 August 31, 2018 What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure

More information

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing ICOM 4036 Programming Languages Lexical and Syntax Analysis Lexical Analysis The Parsing Problem Recursive-Descent Parsing Bottom-Up Parsing This lecture covers review questions 14-27 This lecture covers

More information

COP 3402 Systems Software Syntax Analysis (Parser)

COP 3402 Systems Software Syntax Analysis (Parser) COP 3402 Systems Software Syntax Analysis (Parser) Syntax Analysis 1 Outline 1. Definition of Parsing 2. Context Free Grammars 3. Ambiguous/Unambiguous Grammars Syntax Analysis 2 Lexical and Syntax Analysis

More information

4. Lexical and Syntax Analysis

4. Lexical and Syntax Analysis 4. Lexical and Syntax Analysis 4.1 Introduction Language implementation systems must analyze source code, regardless of the specific implementation approach Nearly all syntax analysis is based on a formal

More information

CSE 3302 Programming Languages Lecture 2: Syntax

CSE 3302 Programming Languages Lecture 2: Syntax CSE 3302 Programming Languages Lecture 2: Syntax (based on slides by Chengkai Li) Leonidas Fegaras University of Texas at Arlington CSE 3302 L2 Spring 2011 1 How do we define a PL? Specifying a PL: Syntax:

More information

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer Torben./Egidius Mogensen Introduction to Compiler Design ^ Springer Contents 1 Lexical Analysis 1 1.1 Regular Expressions 2 1.1.1 Shorthands 4 1.1.2 Examples 5 1.2 Nondeterministic Finite Automata 6 1.3

More information

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design i About the Tutorial A compiler translates the codes written in one language to some other language without changing the meaning of the program. It is also expected that a compiler should make the target

More information

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

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis Chapter 4 Lexical and Syntax Analysis Introduction - Language implementation systems must analyze source code, regardless of the specific implementation approach - Nearly all syntax analysis is based on

More information

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3 Programming Language Specification and Translation ICOM 4036 Fall 2009 Lecture 3 Some parts are Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-1 Language Specification and Translation Topics

More information

4. Lexical and Syntax Analysis

4. Lexical and Syntax Analysis 4. Lexical and Syntax Analysis 4.1 Introduction Language implementation systems must analyze source code, regardless of the specific implementation approach Nearly all syntax analysis is based on a formal

More information

VIVA QUESTIONS WITH ANSWERS

VIVA QUESTIONS WITH ANSWERS VIVA QUESTIONS WITH ANSWERS 1. What is a compiler? A compiler is a program that reads a program written in one language the source language and translates it into an equivalent program in another language-the

More information

CJT^jL rafting Cm ompiler

CJT^jL rafting Cm ompiler CJT^jL rafting Cm ompiler ij CHARLES N. FISCHER Computer Sciences University of Wisconsin Madison RON K. CYTRON Computer Science and Engineering Washington University RICHARD J. LeBLANC, Jr. Computer Science

More information

List of Figures. About the Authors. Acknowledgments

List of Figures. About the Authors. Acknowledgments List of Figures Preface About the Authors Acknowledgments xiii xvii xxiii xxv 1 Compilation 1 1.1 Compilers..................................... 1 1.1.1 Programming Languages......................... 1

More information

INSTITUTE OF AERONAUTICAL ENGINEERING (AUTONOMOUS)

INSTITUTE OF AERONAUTICAL ENGINEERING (AUTONOMOUS) Name Code Class Branch INSTITUTE OF AERONAUTICAL ENGINEERING (AUTONOMOUS) Dundigal, Hyderabad - 500 043 Year 0-0 INFORMATION TECHNOLOGY ASSIGNMENT QUESTIONS AUTOMATA AND COMPILER DESIGN A50513 III B. Tech

More information

Compiler Design Aug 1996

Compiler Design Aug 1996 Aug 1996 Part A 1 a) What are the different phases of a compiler? Explain briefly with the help of a neat diagram. b) For the following Pascal keywords write the state diagram and also write program segments

More information

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs Chapter 2 :: Programming Language Syntax Programming Language Pragmatics Michael L. Scott Introduction programming languages need to be precise natural languages less so both form (syntax) and meaning

More information

Syntax. A. Bellaachia Page: 1

Syntax. A. Bellaachia Page: 1 Syntax 1. Objectives & Definitions... 2 2. Definitions... 3 3. Lexical Rules... 4 4. BNF: Formal Syntactic rules... 6 5. Syntax Diagrams... 9 6. EBNF: Extended BNF... 10 7. Example:... 11 8. BNF Statement

More information

Building Compilers with Phoenix

Building Compilers with Phoenix Building Compilers with Phoenix Syntax-Directed Translation Structure of a Compiler Character Stream Intermediate Representation Lexical Analyzer Machine-Independent Optimizer token stream Intermediate

More information

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

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised: EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing Görel Hedin Revised: 2017-09-04 This lecture Regular expressions Context-free grammar Attribute grammar

More information

CS 406/534 Compiler Construction Putting It All Together

CS 406/534 Compiler Construction Putting It All Together CS 406/534 Compiler Construction Putting It All Together Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy

More information

Principles of Compiler Design

Principles of Compiler Design Principles of Compiler Design Intermediate Representation Compiler Lexical Analysis Syntax Analysis Semantic Analysis Source Program Token stream Abstract Syntax tree Unambiguous Program representation

More information

Question Bank. 10CS63:Compiler Design

Question Bank. 10CS63:Compiler Design Question Bank 10CS63:Compiler Design 1.Determine whether the following regular expressions define the same language? (ab)* and a*b* 2.List the properties of an operator grammar 3. Is macro processing a

More information

Context-free grammars (CFG s)

Context-free grammars (CFG s) Syntax Analysis/Parsing Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax tree (AST) AST: captures hierarchical structure of

More information

CSCI312 Principles of Programming Languages!

CSCI312 Principles of Programming Languages! CSCI312 Principles of Programming Languages! Chapter 2 Syntax! Xu Liu Review! Principles of PL syntax, naming, types, semantics Paradigms of PL design imperative, OO, functional, logic What makes a successful

More information

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

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input. flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input. More often than not, though, you ll want to use flex to generate a scanner that divides

More information

Syntactic Analysis. Syntactic analysis, or parsing, is the second phase of compilation: The token file is converted to an abstract syntax tree.

Syntactic Analysis. Syntactic analysis, or parsing, is the second phase of compilation: The token file is converted to an abstract syntax tree. Syntactic Analysis Syntactic analysis, or parsing, is the second phase of compilation: The token file is converted to an abstract syntax tree. Compiler Passes Analysis of input program (front-end) character

More information

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics ISBN Chapter 3 Describing Syntax and Semantics ISBN 0-321-49362-1 Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the

More information

LANGUAGE PROCESSORS. Introduction to Language processor:

LANGUAGE PROCESSORS. Introduction to Language processor: LANGUAGE PROCESSORS Introduction to Language processor: A program that performs task such as translating and interpreting required for processing a specified programming language. The different types of

More information

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram CS6660 COMPILER DESIGN Question Bank UNIT I-INTRODUCTION TO COMPILERS 1. Define compiler. 2. Differentiate compiler and interpreter. 3. What is a language processing system? 4. List four software tools

More information

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

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer: Theoretical Part Chapter one:- - What are the Phases of compiler? Six phases Scanner Parser Semantic Analyzer Source code optimizer Code generator Target Code Optimizer Three auxiliary components Literal

More information

ECE251 Midterm practice questions, Fall 2010

ECE251 Midterm practice questions, Fall 2010 ECE251 Midterm practice questions, Fall 2010 Patrick Lam October 20, 2010 Bootstrapping In particular, say you have a compiler from C to Pascal which runs on x86, and you want to write a self-hosting Java

More information

CSCI312 Principles of Programming Languages!

CSCI312 Principles of Programming Languages! CSCI312 Principles of Programming Languages!! Chapter 3 Regular Expression and Lexer Xu Liu Recap! Copyright 2006 The McGraw-Hill Companies, Inc. Clite: Lexical Syntax! Input: a stream of characters from

More information

Compiler Passes. Syntactic Analysis. Context-free Grammars. Syntactic Analysis / Parsing. EBNF Syntax of initial MiniJava.

Compiler Passes. Syntactic Analysis. Context-free Grammars. Syntactic Analysis / Parsing. EBNF Syntax of initial MiniJava. Syntactic Analysis Syntactic analysis, or parsing, is the second phase of compilation: The token file is converted to an abstract syntax tree. Compiler Passes Analysis of input program (front-end) character

More information

A simple syntax-directed

A simple syntax-directed Syntax-directed is a grammaroriented compiling technique Programming languages: Syntax: what its programs look like? Semantic: what its programs mean? 1 A simple syntax-directed Lexical Syntax Character

More information

Homework & Announcements

Homework & Announcements Homework & nnouncements New schedule on line. Reading: Chapter 18 Homework: Exercises at end Due: 11/1 Copyright c 2002 2017 UMaine School of Computing and Information S 1 / 25 COS 140: Foundations of

More information

Compiler Construction

Compiler Construction Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/ Seminar Analysis and Verification of Pointer Programs (WS

More information

Programming Languages, Summary CSC419; Odelia Schwartz

Programming Languages, Summary CSC419; Odelia Schwartz Programming Languages, Summary CSC419; Odelia Schwartz Chapter 1 Topics Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation Criteria Influences on Language Design

More information

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1 Review of CFGs and Parsing II Bottom-up Parsers Lecture 5 1 Outline Parser Overview op-down Parsers (Covered largely through labs) Bottom-up Parsers 2 he Functionality of the Parser Input: sequence of

More information

Compiler Code Generation COMP360

Compiler Code Generation COMP360 Compiler Code Generation COMP360 Students who acquire large debts putting themselves through school are unlikely to think about changing society. When you trap people in a system of debt, they can t afford

More information

Syntax Intro and Overview. Syntax

Syntax Intro and Overview. Syntax Syntax Intro and Overview CS331 Syntax Syntax defines what is grammatically valid in a programming language Set of grammatical rules E.g. in English, a sentence cannot begin with a period Must be formal

More information

Chapter 2 Compilers and Interpreters

Chapter 2 Compilers and Interpreters Chapter 2 Compilers and Interpreters Before looking at the details of programming language implementation, we need to examine some of the characteristics of programming languages to find out how they are

More information

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing Parsing Wrapup Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing LR(1) items Computing closure Computing goto LR(1) canonical collection This lecture LR(1) parsing Building ACTION

More information

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics Chapter 3 Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings of Programs:

More information

SYNTAX ANALYSIS 1. Define parser. Hierarchical analysis is one in which the tokens are grouped hierarchically into nested collections with collective meaning. Also termed as Parsing. 2. Mention the basic

More information

Undergraduate Compilers in a Day

Undergraduate Compilers in a Day Question of the Day Backpatching o.foo(); In Java, the address of foo() is often not known until runtime (due to dynamic class loading), so the method call requires a table lookup. After the first execution

More information

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR Pune Vidyarthi Griha s COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR By Prof. Anand N. Gharu (Assistant Professor) PVGCOE Computer Dept.. 22nd Jan 2018 CONTENTS :- 1. Role of lexical analysis 2.

More information

Monday, September 13, Parsers

Monday, September 13, Parsers Parsers Agenda Terminology LL(1) Parsers Overview of LR Parsing Terminology Grammar G = (Vt, Vn, S, P) Vt is the set of terminals Vn is the set of non-terminals S is the start symbol P is the set of productions

More information

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail. Code No: R05320502 Set No. 1 1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail. 2. Construct predictive parsing

More information

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax Susan Eggers 1 CSE 401 Syntax Analysis/Parsing Context-free grammars (CFG s) Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax

More information

PRINCIPLES OF COMPILER DESIGN

PRINCIPLES OF COMPILER DESIGN PRINCIPLES OF COMPILER DESIGN 2 MARK QUESTIONS WITH ANSWERS UNIT I 1. What is a Complier? A Complier is a program that reads a program written in one language-the source language-and translates it in to

More information

Lexical and Syntax Analysis. Top-Down Parsing

Lexical and Syntax Analysis. Top-Down Parsing Lexical and Syntax Analysis Top-Down Parsing Easy for humans to write and understand String of characters Lexemes identified String of tokens Easy for programs to transform Data structure Syntax A syntax

More information

CS 132 Compiler Construction

CS 132 Compiler Construction CS 132 Compiler Construction 1. Introduction 2 2. Lexical analysis 31 3. LL parsing 58 4. LR parsing 110 5. JavaCC and JTB 127 6. Semantic analysis 150 7. Translation and simplification 165 8. Liveness

More information

Software II: Principles of Programming Languages

Software II: Principles of Programming Languages Software II: Principles of Programming Languages Lecture 4 Language Translation: Lexical and Syntactic Analysis Translation A translator transforms source code (a program written in one language) into

More information

PL Revision overview

PL Revision overview PL Revision overview Course topics Parsing G = (S, P, NT, T); (E)BNF; recursive descent predictive parser (RDPP) Lexical analysis; Syntax and semantic errors; type checking Programming language structure

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Context Free Grammars and Parsing 1 Recall: Architecture of Compilers, Interpreters Source Parser Static Analyzer Intermediate Representation Front End Back

More information

Compiler Construction

Compiler Construction Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/ Seminar Analysis and Verification of Pointer Programs (WS

More information

KINGS COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING ACADEMIC YEAR / EVEN SEMESTER

KINGS COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING ACADEMIC YEAR / EVEN SEMESTER KINGS COLLEGE OF ENGINEERING PUNALKULAM DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING ACADEMIC YEAR 2010-2011 / EVEN SEMESTER SUBJECT CODE\SUBJECT NAME: CS1352 \ PRINCIPLES OF COMPILER DESIGN QUESTION BANK

More information

High Level Languages. Java (Object Oriented) This Course. Jython in Java. Relation. ASP RDF (Horn Clause Deduction, Semantic Web) Dr.

High Level Languages. Java (Object Oriented) This Course. Jython in Java. Relation. ASP RDF (Horn Clause Deduction, Semantic Web) Dr. 10 High Level Languages This Course Java (Object Oriented) Jython in Java Relation ASP RDF (Horn Clause Deduction, Semantic Web) Dr. Philip Cannata 1 Dr. Philip Cannata 2 Programming Languages Lexical

More information

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages. Context Free Grammars CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

More information

Review. Pat Morin COMP 3002

Review. Pat Morin COMP 3002 Review Pat Morin COMP 3002 What is a Compiler A compiler translates from a source language S to a target language T while preserving the meaning of the input 2 Structure of a Compiler program text syntactic

More information