CPS2000 Compiler Theory & Practice

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

Syntax-Directed Translation. Lecture 14

CSCI312 Principles of Programming Languages!

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1

CPS 506 Comparative Programming Languages. Syntax Specification

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Winter /22/ Hal Perkins & UW CSE H-1

Semantic Analysis. Compiler Architecture

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Project Compiler. CS031 TA Help Session November 28, 2011

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CSE 401/M501 Compilers

Lab 2 Tutorial (An Informative Guide)

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

The role of semantic analysis in a compiler

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

Chapter 4. Abstract Syntax

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Automated Tools. The Compilation Task. Automated? Automated? Easier ways to create parsers. The final stages of compilation are language dependant

Compiler construction lecture 3

Visitors. Move functionality

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

CS 415 Midterm Exam Spring SOLUTION

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

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

A Simple Syntax-Directed Translator

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

JJTree. The Compilation Task. Automated? JJTree. An easier way to create an Abstract Syntax Tree

Compiler Compiler Tutorial

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Time : 1 Hour Max Marks : 30

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 97 INSTRUCTIONS

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Semantic Analysis. Lecture 9. February 7, 2018

Syntax and Grammars 1 / 21

Syntax Errors; Static Semantics

LECTURE 3. Compiler Phases

Compiler construction in4303 lecture 3

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

.jj file with actions

CS 4120 Introduction to Compilers

Semantic actions for declarations and expressions

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

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Introduction to Compiler Design

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Building Compilers with Phoenix

COP4020 Programming Assignment 2 - Fall 2016

PL Revision overview

CS 406: Syntax Directed Translation

Semantic actions for declarations and expressions. Monday, September 28, 15

Compilers. Compiler Construction Tutorial The Front-end

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

CS164: Midterm I. Fall 2003

Program Representations

Table-Driven Top-Down Parsers

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

Implementing a VSOP Compiler. March 20, 2018

CS 6353 Compiler Construction Project Assignments

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

Semantic analysis. Compiler Construction. An expression evaluator. Examples of computations. Computations on ASTs Aspect oriented programming

COMPILER DESIGN. Intermediate representations Introduction to code generation. COMP 442/6421 Compiler Design

Project 1: Scheme Pretty-Printer

CS 314 Principles of Programming Languages. Lecture 9

Modules, Structs, Hashes, and Operational Semantics

The following presentation is provided under DHMB license. (Do Not Hurt My Brain). Lecturer is not responsible for the financial and moral

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

CA Compiler Construction

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Operator Associativity & Precedence. AST Navigation. HW4 out (due next Thurs)

CSCE 314 Programming Languages. Type System

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

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

Compilers Project 3: Semantic Analyzer

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

CS 6353 Compiler Construction Project Assignments

CSE 401 Midterm Exam Sample Solution 2/11/15

Compiling Regular Expressions COMP360

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

LL(k) Compiler Construction. Choice points in EBNF grammar. Left recursive grammar

LL(k) Compiler Construction. Top-down Parsing. LL(1) parsing engine. LL engine ID, $ S 0 E 1 T 2 3

Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4

1 Lexical Considerations

A Tour of Language Implementation

Compiling Techniques

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Compilers. Predictive Parsing. Alex Aiken

Building Compilers with Phoenix

MIT Top-Down Parsing. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

CSE 431S Final Review. Washington University Spring 2013

A simple syntax-directed

Topic 3: Syntax Analysis I

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

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Compiler Construction I

Introduc)on. Tristan Naumann. Sahar Hasan. Steve Hehl. Brian Lu

Programs as data first-order functional language type checking

Automatic Generation of Graph Models for Model Checking

Transcription:

CPS2000 Compiler Theory & Practice Notes on Handcrafting a Parser Gordon Mangion

Source File Compiler Lexical Analyser Keyword Table Abstract Syntax Tree Parser Symbol Table? Error Module? Abstract Syntax Tree

Parser What is the actual input to the Parser? What is the parser doing? How do we implement it? What is its output?

Parsing Syntax Analysis Hierarchical Analysis The input (Program) conforms to the grammar Builds a data structure Parse Tree AST - Abstract Syntax Tree Side-effect Symbol-table entries? We are considering a Top-down recursive descent parser

Lexer and Parser Connecting the Lexer with the Parser options 1. Parser calls lex.nexttoken() 2. TokenStream a. Read all file and generate Tokens b. Class acts as a stream of tokens c. Parser can move through the stream as it pleases

Grammar Example <Letter> ::= [A-Za-z] <Digit> ::= [0-9] <IntegerLiteral> ::= <Digit> { <Digit> } <Identifier> ::= (<Letter> _ ) { <Letter> _ <Digit> } <Expression> ::= <Identifier> <IntegerLiteral> <Assignment> ::= set <Identifier> <- <Expression> <Statement> ::= <Assignment> ; <Expression> ; <Program> ::= { <Statement> }

Parser 1. Lexer a. Terminals b. Patterns 2. Parser a. Non-Terminals i. Create a procedure to parse its rule ii. Create an AST node to insert in the AST iii. Depending on the rule might need to read/write into the symbol-table b. Start with the Start Non-Terminal and follow the production-rules

Grammar Example <Letter> ::= [A-Za-z] <Digit> ::= [0-9] <IntegerLiteral> ::= <Digit> { <Digit> } <Identifier> ::= (<Letter> _ ) { <Letter> _ <Digit> } <Expression> ::= <Identifier> <IntegerLiteral> <Assignment> ::= set <Identifier> <- <Expression> <IfStatement> ::= if ( <Expression> ) <Block> [ else <Block>] Block ::= { { <Statement> } } <Statement> ::= <Assignment> ; <IfStatement> ; <Program> ::= { <Statement> }

if ( expr ) Block [ else Block ] bool Parse_If( ) { if( nexttoken().kind!= LParen ) { Error return false; }

if ( expr ) Block [ else Block ] if(!parse_expression(...) ) { Error return false; }

if ( expr ) Block [ else Block ] if( nexttoken().kind!= tk_rparen ) { Error return false; } if(!parse_block(...) ) { Error return false; }

if ( expr ) Block [ else Block ] if( nexttoken().kind!= tk_else ) { return true; } if(!parse_block(...) ) { Error return false; } return true; } // Parse_If

Grammar Example <Letter> ::= [A-Za-z] <Digit> ::= [0-9] <IntegerLiteral> ::= <Digit> { <Digit> } <Identifier> ::= (<Letter> _ ) { <Letter> _ <Digit> } <Expression> ::= <Identifier> <IntegerLiteral> <Assignment> ::= set <Identifier> <- <Expression> <IfStatement> ::= if ( <Expression> ) <Block> [ else <Block>] <Statement> ::= <Assignment> ; <IfStatement> ; <Program> ::= { <Statement> }

<Statement> ::= <Assignment> ; <IfStatement> bool Parse_Statement(...) { if( nexttoken().kind == tk_set ) { if(!parse_assignment(...)) { error return false; } } return (nexttoken().kind == tk_semicolon) } else if( nexttoken().kind == tk_if ) return Parse_If(...);

<Program> ::= { <Statement> } bool Parser::Parse(...) { while( currenttoken.kind!= tk_eof ) { if(!parse_statement(...)) { Error return false; } } return true; }

Abstract Syntax Tree ASTNode Data List of Children (Node) Operations void AddChild(Node) int NumOfChildren() Node GetChild(n) Node NODES NODES NODES

Parse Method ASTNode Parser::Parse_If(...) { ASTNode *pnode = new IfNode() } return pnode;

Abstract Syntax Tree if ( expr ) Block [ else Block ] IF EXPR THEN BLOCK ELSE BLOCK

Creating a sub-tree example ASTNode Parser::Parse_If(...) {... ASTNode *pifnode = new IfNode() pifnode->addchild( Parse_Expr(...) ) pifnode->addchild( Parse_Block(...) ) if(...) pifnode->addchild( Parse_Block(...) ) return pnode; }

Abstract Syntax Tree set <identifier> <- <expr> Assign Identifier EXPR

Creating a sub-tree example ASTNode Parser::Parse_Assign(...) { ASTNode *pasgnnode = new AssignNode() pasgnnode->addchild( Parse_Ident(...) ) pasgnnode->addchild( Parse_Expr(...) ) } return pnode;

Traversing the AST Generic Traverse Algorithm Do Something with current Node Foreach child Do Something with current Node call Traverse on child Do Something with current Node Do Something with current Node

Traversing the AST Stages traverse the AST Semantic Analysis / Type Checker Backend Stages Optimisation Code Generation Interpreters / Execution

Interface (Java) public interface IAdder { int add(int n, int m); } public interface IMinus { int subtract(int n, int m); }

Interface (Java) public class SimpleMath implements IAdder, IMinus { public int add(int n, int m) { return n+m; } } public int subtract(int n, int m) { return n-m; }

Interface (Java) public static void main( ) { SimpleMath sm = new SimpleMath() IAdder a = sm; IMinus s = sm; a.add(1,2); s.subtract(4,2); }

Interface (C++) abstract class IAdder { int add(int n, int m) = 0; }; abstract class IMinus { int subtract(int n, int m) = 0; };

AST Traversal AST T r a v e r s a l TypeChecker Pass 1 TypeChecker Pass 2... CodeGenerator

Visitor Design Pattern ASTNode Traverse() Traverse Algo IfNode Traverse() Traverse Algo

Visitor Design Pattern ASTNode Accept() IfNode Accept() Visitor Class Visit(ASTNode *pnode ) Traverse Algorithm Visit( IfNode *pnode ) Traverse Algorithm

Visitor Design Pattern ASTNode Accept(Visitor v) v.visit(this); IfNode Accept(Visitor v) v.visit(this) Visitor Class Visit(ASTNode *pnode ) Traverse Algorithm Visit( IfNode *pnode ) Traverse Algorithm

Visitor Design Pattern ASTNode Accept(IVisitor v) v.visit(this); Visitor Class : IVisitor Visit(ASTNode *pnode ) Traverse Algorithm Interface IVisitor Visit(ASTNode *pnode );

SymbolTable Set of Symbols (definitions) Tracks current bindings of identifiers Stack of symbols: Push(x) Add a symbol x to table Pop Remove top symbol Find(x) Find a symbol x in the table starting from the topmost entry

SymbolTable Stack of scopes: enter_scope() find_symbol(x) add_symbol(x) check_scope(x) exit_scope() Start a new scope Find current x (or null) Add a symbol x to table true if x defined in current scope Exit current scope