Projects for Compilers

Similar documents
2. λ is a regular expression and denotes the set {λ} 4. If r and s are regular expressions denoting the languages R and S, respectively

Lexical Analysis (ASU Ch 3, Fig 3.1)

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Chapter 3 Lexical Analysis

3. Context-free grammars & parsing

Parsing. Lecture 11: Parsing. Recursive Descent Parser. Arithmetic grammar. - drops irrelevant details from parse tree

Compiler course. Chapter 3 Lexical Analysis

Chapter 3: Lexical Analysis

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

A simple syntax-directed

Recognition of Tokens

Assignment 1 (Lexical Analyzer)

A Simple Syntax-Directed Translator

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

Intermediate Representations

Lexical and Syntax Analysis

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

Program Analysis ( 软件源代码分析技术 ) ZHENG LI ( 李征 )

Assignment 1 (Lexical Analyzer)

Lexical Analysis. Chapter 1, Section Chapter 3, Section 3.1, 3.3, 3.4, 3.5 JFlex Manual

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

10/5/17. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntax Analysis

CPS 506 Comparative Programming Languages. Syntax Specification

MANIPAL INSTITUTE OF TECHNOLOGY (Constituent Institute of MANIPAL University) MANIPAL

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

CS 403: Scanning and Parsing

Lexical Analysis. Implementing Scanners & LEX: A Lexical Analyzer Tool

PRACTICAL CLASS: Flex & Bison

UNIT II LEXICAL ANALYSIS

CSE302: Compiler Design

PRINCIPLES OF COMPILER DESIGN UNIT II LEXICAL ANALYSIS 2.1 Lexical Analysis - The Role of the Lexical Analyzer

CSE302: Compiler Design

CS 403 Compiler Construction Lecture 3 Lexical Analysis [Based on Chapter 1, 2, 3 of Aho2]

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.

Topic 3: Syntax Analysis I

CSE 3302 Programming Languages Lecture 2: Syntax

Lexical Analysis. Introduction

Lexical Analysis. Sukree Sinthupinyo July Chulalongkorn University

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

UNIT -2 LEXICAL ANALYSIS

Topic of the day: Ch. 4: Top-down parsing

Parsing and Pattern Recognition

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Recursive Descent Parsers

CS321 Languages and Compiler Design I. Winter 2012 Lecture 4

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

CSC 4181 Compiler Construction. Parsing. Outline. Introduction

WARNING for Autumn 2004:

Module 8 - Lexical Analyzer Generator. 8.1 Need for a Tool. 8.2 Lexical Analyzer Generator Tool

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Template Language and Syntax Reference

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

A programming language requires two major definitions A simple one pass compiler

Chapter 4: Syntax Analyzer

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

Compiler Construction

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

Compiler Construction

CIT 3136 Lecture 7. Top-Down Parsing

2068 (I) Attempt all questions.

Formal Languages and Compilers Lecture VI: Lexical Analysis

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

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

1. Lexical Analysis Phase

Lexing, Parsing. Laure Gonnord sept Master 1, ENS de Lyon

It parses an input string of tokens by tracing out the steps in a leftmost derivation.

KU Compilerbau - Programming Assignment

Buffering Techniques: Buffer Pairs and Sentinels

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

CA Compiler Construction

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Chapter 4. Lexical and Syntax Analysis

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

Simple Lexical Analyzer

CSCI312 Principles of Programming Languages!

ASSIGNMENT #3: SYNTAX ANALYSIS

Lab 2 Tutorial (An Informative Guide)

Building lexical and syntactic analyzers. Chapter 3. Syntactic sugar causes cancer of the semicolon. A. Perlis. Chomsky Hierarchy

Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson

Part 5 Program Analysis Principles and Techniques

CSE 401 Midterm Exam Sample Solution 2/11/15

Exercise ANTLRv4. Patryk Kiepas. March 25, 2017

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering

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

LECTURE 3. Compiler Phases

David Griol Barres Computer Science Department Carlos III University of Madrid Leganés (Spain)

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

Dr. D.M. Akbar Hussain

DEMO A Language for Practice Implementation Comp 506, Spring 2018

CS 6353 Compiler Construction Project Assignments

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

UNIT III. The following section deals with the compilation procedure of any program.

1. Consider the following program in a PCAT-like language.

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

QUESTIONS RELATED TO UNIT I, II And III

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Transcription:

Projects for Compilers 1. Project One: Lexical Analysis (Required) (1) Directions Implement a transition-diagram-based lexical analysis for the programming language TINY. (2) Outputs Source code (implemented in Java or C or C++ programming languages) A project report Structure of the project report 1. Objectives (1) Regular expressions (2) Transition diagram based lexical analyzer (3) Error handling 2. Program Designing (1) Lexical specification for the programming language (2) Token scheme (3) Structure of the program 3. Test Cases and Test results (1) For correct input, the program should print the token sequence (2) For error input, the program should report the errors Test cases should cover the ranges as large as possible. 1

(3) Lexical Specification of Programming Language TINY Tokens type lexeme Token Name (code) Attribute if if (261) - then then (262) - else else (263) - keywords end end (264) - repeat repeat (265) - until until (266) - read read (267) - write write (268) - < relop (270) LT (271) <= relop LE (272) relation == relop EQ (273) operators <> relop NE (274) > relop GT (275) >= relop GE (276) arithmetical operators + addop(280) ADD (281) - addop(280) MINUS (282) * mulop(285) MUL(286) / mulop(285) DIV(287) ( ( (294) - ) ) (295) - assignment := := (296) - Segment ; ; (297) - numbers Such as 12342 num (298) Symbol Entry identifiers Such as student1 id (299) Symbol Entry white spaces blank, tab, and (ws) newline - - Comments {bla, bala, bala } - - Note: all white spaces and comments should be removed. Regular Definitions delim [ \t\n] ws delim+ letter [A-Za-z] digit [0-9] id letter(letter digit)* num digit+ 2 Table Table

Example code of TINY { Sample program in TINY language - computes factorial} read x; { input an integer } if x > 0 then { do not compute if x <= 0 } fact := 1; repeat fact := fact * x; x := x - 1 until x = 0; write fact { output factorial of x } end 2. Project Two: Syntax Analysis (Required) (1) Directions Use either LL(1) or LR(1) techniques to implement a syntax analyzer for the programming language TINY. LL (1) can implemented as either Recursive-Descent parsing or nonrecursive Predictive Parsing. (2) Outputs Source code (implemented in Java or C or C++ programming languages) A project report Structure of the project report 1. Objectives (1) Context-free grammar (2) LL(1) or LR(1) parsing (3) Error handling 2. Program Designing (1) Context-free grammar for the programming language (2) FIRST and FOLLOW for LL(1) (Sets of Items for LR(1)) (3) Structure of the program 3. Test Cases and Test results (1) For correct input, the program should print a syntax tree (2) For error input, the program should report the errors Test cases should cover the ranges as large as possible. 3

(3) Context-free of Programming Language TINY 1) program stmt-sequence 2) stmt-sequence stmt-sequence ; statement statement 3) statement if-stmt repeat-stmt assign-stmt read-stmt write-stmt 4) if-stmt if exp then stmt-sequence end if exp then stmt-sequence else stmt-sequence end 5) repeat-stmt repeat stmt-sequence until exp 6) assign-stmt id := exp 7) read-stmt read id 8) write-stmt write exp 9) exp simple-exp relop simple-exp simple-exp 10) simple-exp simple-exp addop term term 11) term term mulop factor factor 12) factor (exp) num id LL(1) Grammar of TINY 1) program stmt-sequence 2) stmt-sequence statement stmt-sequence-p 3) stmt-sequence-p ; statement stmt-sequence-p ε 4) statement if-stmt repeat-stmt assign-stmt read-stmt write-stmt 5) if-stmt if exp then stmt-sequence if-rear 6) if-rear end else stmt-sequence end 7) repeat-stmt repeat stmt-sequence until exp 8) assign-stmt id := exp 9) read-stmt read id 10) write-stmt write exp 11) exp simple-exp exp-rear 12) exp-rear relop simple-exp ε 13) simple-exp term simple-exp-p 14) simple-exp-p addop term simple-exp-p ε 15) term factor term-p 16) term-p mulop factor term-p ε 17) factor (exp) num id 4

FIRST(program) = FRIST (stmt-sequence) = {if repeat id read write} FIRST(stmt-sequence) = FIRST(statement stmt-sequence-p) = {if repeat id read write} FIRST(stmt-sequence-p) = { ; ε} FIRST(statement) = FIRST (if-stmt) U FIRST (repeat-stmt) U FIRST (assign-stmt) U FIRST (read-stmt) U FIRST (write-stmt) = {if repeat id read write} FIRST(if-stmt) = {if} FIRST(if-rear) = FIRST(end) U FIRST(else stmt-sequence end) ={end else} FIRST(repeat-stmt) = {repeat} FIRST(assign-stmt) = {id} FIRST(read-stmt) = {read} FIRST(write-stmt) = {write} FIRST(exp) = FIRST(simple-exp exp-rear)={( num id} FIRST(exp-rear) = {relop ε} FIRST(simple-exp) = FIRST(term simple-exp-p) = {( num id} FIRST(simple-exp-p) = {addop ε} FIRST(term) = FIRST(factor term-p) = {( num id} FIRST(term-p) = {mulop ε} FIRST(factor) = FIRST ((exp)) U FIRST (num) U FIRST (id) ={( num id} FOLLOW(program) ={$} FOLLOW (stmt-sequence)={$ end else until} FOLLOW (stmt-sequence-p)={$ end else until} FOLLOW (statement) ={$ ; end else until} FOLLOW (if-stmt)={$ ; end else until} FOLLOW (if-rear)= {$ ; end else until} FOLLOW (repeat-stmt)={$ ; end else until} FOLLOW (assign-stmt)={$ ; end else until} FOLLOW (read-stmt)={$ ; end else until} FOLLOW (write-stmt)={$ ; end else until} FOLLOW (exp)={ $ ; ) end else until then} FOLLOW (exp-rear)={ $ ; ) end else until then} FOLLOW (simple-exp)={ relop $ ; ) end else until then } FOLLOW (simple-exp-p)={ relop $ ; ) end else until then } FOLLOW (term)={ addop relop $ ; ) end else until then } 5

FOLLOW (term-p)={addop relop $ ; ) end else until then} FOLLOW (factor)={addop mulop relop $ ; ) end else until then} FIRST(stmt-sequence-p) FOLLOW (stmt-sequence-p)={} FIRST(exp-rear) FOLLOW(exp-rear)={} FIRST(simple-exp-p) FOLLOW (simple-exp-p)={} FIRST(term-p) FOLLOW (term-p) ={} The grammar is an LL(1) grammar. 6