COMP3131/9102: Programming Languages and Compilers

Similar documents
COMP3131/9102: Programming Languages and Compilers

A Simple Syntax-Directed Translator

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Principles of Programming Languages COMP251: Syntax and Grammars

EDA180: Compiler Construc6on. Top- down parsing. Görel Hedin Revised: a

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

COMP3131/9102: Programming Languages and Compilers

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

Parsing II Top-down parsing. Comp 412

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

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

CS 314 Principles of Programming Languages

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

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

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

CMPT 379 Compilers. Parse trees

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

Syntax. A. Bellaachia Page: 1

Last Time. What do we want? When do we want it? An AST. Now!

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

LECTURE 3. Compiler Phases

Syntactic Analysis. The Big Picture Again. Grammar. ICS312 Machine-Level and Systems Programming

Abstract Syntax Trees and Contextual Analysis. Roland Backhouse March 8, 2001

Context-free grammars (CFG s)

CSE 3302 Programming Languages Lecture 2: Syntax

Intermediate Code Generation

Building Compilers with Phoenix

Describing Syntax and Semantics

Principles of Programming Languages COMP251: Syntax and Grammars

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

COMP3131/9102: Programming Languages and Compilers

CPS 506 Comparative Programming Languages. Syntax Specification

Compilers Course Lecture 4: Context Free Grammars

Syntax. In Text: Chapter 3

More Assigned Reading and Exercises on Syntax (for Exam 2)

Chapter 3. Describing Syntax and Semantics ISBN

More on Syntax. Agenda for the Day. Administrative Stuff. More on Syntax In-Class Exercise Using parse trees

EECS 6083 Intro to Parsing Context Free Grammars

2.2 Syntax Definition

Chapter 3. Describing Syntax and Semantics ISBN

CSE 12 Abstract Syntax Trees

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

Chapter 4 :: Semantic Analysis

Chapter 4. Abstract Syntax

Habanero Extreme Scale Software Research Project

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

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

Chapter 3. Describing Syntax and Semantics

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

Chapter 3. Describing Syntax and Semantics

Project Compiler. CS031 TA Help Session November 28, 2011

CA Compiler Construction

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

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

Introduction to Parsing. Lecture 8

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Optimizing Finite Automata

Parsing a primer. Ralf Lämmel Software Languages Team University of Koblenz-Landau

Compiler Code Generation COMP360

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

Introduction to Compiler Construction

Chapter 2 A Quick Tour

Software II: Principles of Programming Languages

Syntax Analysis Part I

Object-oriented Compiler Construction

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

CSE302: Compiler Design

.jj file with actions

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Crafting a Compiler with C (II) Compiler V. S. Interpreter

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

CSCE 314 Programming Languages

Chapter 3. Topics. Languages. Formal Definition of Languages. BNF and Context-Free Grammars. Grammar 2/4/2019

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

EDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised:

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

Lexical Scanning COMP360

Tree Applications. Processing sentences (computer programs or natural languages) Searchable data structures

CMSC 330: Organization of Programming Languages

3. DESCRIBING SYNTAX AND SEMANTICS

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

3. Parsing. Oscar Nierstrasz

ASTs, Objective CAML, and Ocamlyacc

Introduction to Compiler Construction

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

Some Basic Definitions. Some Basic Definitions. Some Basic Definitions. Language Processing Systems. Syntax Analysis (Parsing) Prof.

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

Syntax Intro and Overview. Syntax

This book is licensed under a Creative Commons Attribution 3.0 License

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

Semantic Analysis. Role of Semantic Analysis

Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler)

LL(1) predictive parsing

Introduction to Compiler Construction

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

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

Context-Free Grammars

Transcription:

COMP3131/9102: Programming Languages and Compilers Jingling Xue School of Computer Science and Engineering The University of New South Wales Sydney, NSW 2052, Australia http://www.cse.unsw.edu.au/~cs3131 http://www.cse.unsw.edu.au/~cs9102 Copyright @2018, Jingling Xue COMP3131/9102 Page 305 April 8, 2018

Lecture 6: Abstract Syntax Trees (ASTs) 1. Assignment 3 2. Why a physical tree? 3. Parse trees v.s. syntax trees 4. Design of AST classes 5. Use of AST classes 6. Attribute grammar 7. Implementation details specific to Assignment 3 COMP3131/9102 Page 306 April 8, 2018

Packages: Assignment 3 PACKAGE VC.ASTs VC.Parser VC.TreeDrawer VC.TreePrinter VC.UnParser The VC Compiler options: FUNCTIONALITY AST classes for creating tree nodes Parser Draws an AST on the screen Print an ASCII AST Traverses an AST to print a VC program [daniel 3:00pm] java VC.vc Usage: java VC.vc [-options] filename -ast display the AST (without SourcePosition) -astp display the AST (with SourcePosition) -t file print the AST into <file> -u file unparse the AST into <file> COMP3131/9102 Page 307 April 8, 2018

Constructing ASTs in Assignment 3 AST Classes Recogniser: Your Assignment 2 Adding Calls to AST Class Constructors VC Program ASTs-Building Parser AST Only constructors in AST classes are used in Assignment 3. COMP3131/9102 Page 308 April 8, 2018

Programming Environment for Assignment 3 AST (a data structure) TreeDrawer TreePrinter UnParser The AST shown on a window The AST printed to a file The AST unparsed into a file All three packages coded using the Visitor Design Pattern http://www.newthinktank.com/2012/11/visitor-design-pattern-tutorial/ http://www.zzrose.com/tech/pmr_swedesignpatternvisitor.html The pattern to be used in Assignments 4 & 5 Can be understood by examining the codes Tree-walkers like these can be generated automatically using attribute grammars once supporting codes are given COMP3131/9102 Page 309 April 8, 2018

Example Program (ex.vc): i = (1+2)*3; The AST (using option -ast ): COMP3131/9102 Page 310 April 8, 2018

Example (Cont d) Program (ex.vc): i = (1+2)*3; The AST (using option -astp ): COMP3131/9102 Page 311 April 8, 2018

Program (ex.vc): i = (1+2)*3; Example (Cont d) The ASCII AST Using -ast (ex.vct): AssignExpr VarExpr SimpleVar i BinaryExpr BinaryExpr IntExpr 1 + IntExpr 2 * IntExpr 3 COMP3131/9102 Page 312 April 8, 2018

Example (Cont d) The unparsed VC program (ex.vcu): (i=((1+2)*3)); The UnParser.java demonstrates the implementation of a pretty printer or editor Our UnParser is not quite a pretty printer since some information in the original VC is missing in the AST (see Slide 326) UnParser: Will be used for marking Assignment 3 Can be used for debugging your solution (see spec) COMP3131/9102 Page 313 April 8, 2018

Depth-First Left-To-Right Traversal The later phases of a compiler typically involves a depth-first left-to-right traversal of the tree (p 37 Red/p 57 Purple): void traverse(ast n) { 1 visit(n) for (each child of m of n from left to right) traverse(m); 2 visit(n) } In general, a node can be visited or processed 1 before all its children, 2 after all its children, or 3 in between the visits to its children This traversal used in all three tree packages COMP3131/9102 Page 314 April 8, 2018

The Typical Structure of A Compiler (Slide 13) Source Code Front End Back End Scanner Tokens Parser AST Analysis Semantic Analyser (decorated) AST Intermediate Code Generation IR Code Optimisation IR Synthesis Code Generation Target Code Informally, error handling and symbol table management also called phases. (1) Analysis: breaks up the program into pieces and creates an intermediate representation (IR), and (2) Synthesis: constructs the target program from the IR COMP3131/9102 Page 315 April 8, 2018

A pass Passes 1. reads the source program or output from a previous pass, 2. makes some transformations, and 3. then writes output to a file or an internal data structure Traditionally, a pass is the process of reading a file from the disk and writing a file to the disk. This concept is getting murky now. COMP3131/9102 Page 316 April 8, 2018

One-Pass Compilers source code Scanner Parser Code Generator machine code Code generation done as soon as a construct is recognised Easy to implement Code inefficient because optimisations are hardly done Difficult to implement for some languages such as PL/1 where variables are used before defined An example: Wirth s first Pascal Compilers COMP3131/9102 Page 317 April 8, 2018

Two-Pass Compilers Most production compilers are multi-pass compilers The typical structure of a two-pass compiler: source code Front End errors Back End An example: Ritchie and Johnson s C compilers IR Many assemblers work in two passes Why (Intermediate Representation) IR? Simplify retargeting Sophisticated optimisations possible on IR machine code IR can be processed in any order without being constrained by the parsing as in one-pass compilers COMP3131/9102 Page 318 April 8, 2018

Why a Physical (or Explicit) Tree? Tree is one of intermediate representations (IR) The syntactic structure represented explicit The semantics (e.g., types, addresses, etc.) attached to the nodes The question then becomes: why IR? COMP3131/9102 Page 319 April 8, 2018

Modern Optimising Compilers Optimising the program in multiple passes IR IR IR opt pass 1 opt pass n IR Examples: Java bytecode optimisers (http://www.bearcave.com/software/java/comp_java.html) Common optimisations (covered earlier in COMP4133) Loop optimisation Software pipelining Locality optimisation Inter-procedural analysis and optimisation etc. COMP3131/9102 Page 320 April 8, 2018

Lecture 6: Abstract Syntax Trees (ASTs) 1. Assignment 3 2. Why a physical tree? 3. Parse trees v.s. syntax trees 4. Design of AST classes 5. Use of AST classes 6. Attribute grammar 7. Implementation details specific to Assignment 3 COMP3131/9102 Page 321 April 8, 2018

Phrases A phrase of a grammargis a string of terminals labelling the terminal nodes (from left to right) of a parse tree An A-phrase ofgis a string of terminals labelling the terminal nodes of the subtree whose root is labelled A. Formally, giveng = (V T,V N,P,S), if S = uwv uwv is a sentential form S = uav for some A V N, and A = + w for some w V + T, and then w is a phrase (which is in fact ana-phrase) Examples: An if-phrase has 3 subphrases: an expression and two statements An while-phrase has 2 subphrases: an expression and a statement COMP3131/9102 Page 322 April 8, 2018

An if-phrase has 3 subphrases COMP3131/9102 Page 323 April 8, 2018

An while-phrase has 2 subphrases COMP3131/9102 Page 324 April 8, 2018

Parse Trees (or Concrete Syntax Trees) Specifies the syntactic structure of the input The underlying grammar is a concrete syntax for the language Used for parsing (i.e., deciding if a sentence is syntactically legal) Has one leaf for every token in the input and one interior node for every production used during the parse COMP3131/9102 Page 325 April 8, 2018

Syntax Trees or (Abstract Syntax Trees) Specifies the phrase structure of the input More compressed representation of parse tree Nonterminals used for defining operators precedence and associativity should be confined to the parsing phase Separators (punctuation) tokens are redundant in later phases Keywords implicit in tree nodes Abstract syntax can be specified using an attribute grammar Used in type checking, code optimisation and generation COMP3131/9102 Page 326 April 8, 2018

The Expression Grammars The grammar with left recursion: Grammar 1: E E +T E T T T T F T/F F F INT (E) The transformed grammar without left recursion: Grammar 2: E TQ Q +TQ TQ ǫ T FR R FR /FR ǫ F INT (E) An expression grammar (Slide 150): Grammar 3: E E+E E E E/E E E (E) INT COMP3131/9102 Page 327 April 8, 2018

T F INT 1 Parse Trees for 1 (2+3) Grammar 1 Grammar 2 E T * F ( E E T F INT 2 ) + T F INT 3 F INT 1 F INT 2 T T * F ( E R ǫ E R Q + T INT 3 A depth-first traversal yields the expression being analysed Grammar 1 unsuitable for top-down parsing (due to left recursion) The tree for Grammar 2 is unnatural COMP3131/9102 Page 328 April 8, 2018 ) Q F ǫ R ǫ Q ǫ

Parse Tree for 1 (2+3) Using Grammar 3 E INT 1 E * E ( E E INT 2 + E INT 3 The parse tree is unique for this expression But more than one parse tree exist in general (Lecture 3) The (correct) parse trees look more natural but Grammar 3 is ambiguous! ) COMP3131/9102 Page 329 April 8, 2018

The AST in VC for 1 (2+3) The separators ( and ) are not needed, because the meaning of the expression in the AST is clear Nonterminals such as term and factor are not needed, because the operator precedence in the AST is clear COMP3131/9102 Page 330 April 8, 2018

The Parse Tree for a VC If Statement The If statement: void main(){ if (x) x = 0; else x = 1; } The parse tree if IF ( expr ) stmt ELSE stmt x subtree for x = 0; subtree for x = 1; COMP3131/9102 Page 331 April 8, 2018

The AST for a VC If Statement The separators (, ) and ; are not needed Keyword if and else implicit in the AST nodes COMP3131/9102 Page 332 April 8, 2018

Lecture 6: Abstract Syntax Trees (ASTs) 1. Assignment 3 2. Why a physical tree? 3. Parse trees v.s. syntax trees 4. Design of AST classes 5. Use of AST classes 6. Attribute grammar 7. Implementation details specific to Assignment 3 COMP3131/9102 Page 333 April 8, 2018

Design of AST Classes Can be formally specified using a grammar http://pdf. aminer.org/000/161/377/the_zephyr_abstract_syntax_description_ language.pdf Then the AST classes can be generated automatically The structure of AST classes in a compiler: AST.java is the top-level abstract class In general, one abstract class for a nonterminal and one concrete class for each of its production alternatives In VC, the EmptyXYZ AST classes introduced to avoid the use of null (nothing fundamental but a design decision made here) Package TreeDrawer assumes no null references COMP3131/9102 Page 334 April 8, 2018

SourcePosition pos = new SourcePosition(); Stmt s1 = new BreakStmt(pos); Stmt s2 = new ContinueStmt(pos); IntLiteral il = new IntLiteral("1", pos); IntExpr ie = new IntExpr(il, pos); Stmt s3 = new ReturnStmt(ie, pos); Use of AST Classes List sl = new StmtList(s3, new EmptyStmtList(pos), pos); sl = new StmtList(s2, sl, pos); sl = new StmtList(s1, sl, pos); break; continue; return 1; COMP3131/9102 Page 335 April 8, 2018

import VC.TreeDrawer.Drawer; import VC.ASTs.*; import VC.Scanner.SourcePosition; public class ASTMaker { private static Drawer drawer; ASTMaker() { } List createast() { SourcePosition pos = new SourcePosition(); Stmt s1 = new BreakStmt(pos); Stmt s2 = new ContinueStmt(pos); IntLiteral il = new IntLiteral("1", pos); IntExpr ie = new IntExpr(il, pos); Stmt s3 = new ReturnStmt(ie, pos); How to Test AST Classes List sl = new StmtList(s3, new EmptyStmtList(pos), pos); sl = new StmtList(s2, sl, pos); sl = new StmtList(s1, sl, pos); return sl; } } public static void main(string args[]) { ASTMaker o = new ASTMaker(); AST theast = o.createast(); Drawer drawer = new Drawer(); drawer.draw(theast); } COMP3131/9102 Page 336 April 8, 2018

Understanding the Visior Design Pattern (Lecture 7) Read Visitor.java the visitor interface Every Visitor class must implement the Visitor interface Read AST.java for the abstract visit method Every concrete AST A implements the visit method by simply calling the visitor method VisitA in the interface A understanding of the pattern unnecessary for Assignment 3 but critical for Assignments 4 & 5 COMP3131/9102 Page 337 April 8, 2018

Understanding the Visior Design Pattern (Cont d) The free pattern book: http://www.freejavaguide.com/java-design-patterns.pdf Understand the visitor pattern under the Behavioural Patterns before Lecture 7 Read this and study the implementation of TreeDrawer COMP3131/9102 Page 338 April 8, 2018

An attribute grammar is a triple: where G is a CFG, Attribute Grammars A = (G,V,F) V is a finite set of distinct attributes, and F is a finite set of semantic rules (semantic computation and predicate) functions about the attributes. Note: Each attribute is associated with a grammar symbol Each semantic rule is associated with a production that makes reference only to the attributes associated with the symbols in the production COMP3131/9102 Page 339 April 8, 2018

Attributes Associated with a Grammar Symbol A attribute can represent anything we choose: a string a number a type a memory location etc. COMP3131/9102 Page 340 April 8, 2018

An Attribute Grammar for Converting Infix to Postfix PRODUCTION SEMANTIC RULE E T [E.t = T.t] E 1 + T [E.t = E 1.t T.t + ] E 1 - T [E.t = E 1.t T.t - ] T F [T.t = F.t] T 1 F [T.t = T 1.t F.t * ] T 1 / F [T.t = T 1.t F.t / ] F INT [F.t = int.string-value] F ( E ) [F.t = E.t] A single string-valued attribute t : string concatenation COMP3131/9102 Page 341 April 8, 2018

An Attribute Grammar for Converting Infix to Postfix PRODUCTION SEMANTIC RULE E T [E.t = T.t] ( + T [E.t = E.t T.t + ] - T [E.t = E.t T.t - ] ) T F [T.t = F.t] ( * F [T.t = T.t F.t * ] / F [T.t = T.t F.t / ] ) F INT F ( E ) A single string-valued attribute t : string concatenation [F.t = int.string-value] [F.t = E.t] COMP3131/9102 Page 342 April 8, 2018

/* * Expr.java * */ The Driver for the Parser in Slide 345 import VC.Scanner.Scanner; import VC.Scanner.SourceFile; import VC.ErrorReporter; public class Expr { private static Scanner scanner; private static ErrorReporter reporter; private static Parser parser; public static void main(string[] args) { if (args.length!= 1) { System.out.println("Usage: java Compiler filename"); System.exit(1); } String sourcename = args[0]; System.out.println("*** " + "The Expression compiler " + " ***"); SourceFile source = new SourceFile(sourceName); COMP3131/9102 Page 343 April 8, 2018

reporter = new ErrorReporter(); scanner = new Scanner(source, reporter); parser = new Parser(scanner, reporter); parser.parsegoal(); if (reporter.numerrors == 0) System.out.println ("Compilation was successful."); else System.out.println ("Compilation was unsuccessful."); } } COMP3131/9102 Page 344 April 8, 2018

A Parser Implementing the Attribute Grammar in Slide 342 public void parsegoal() { String Et = parsee(); if (currenttoken.kind!= Token.EOF) { syntacticerror("\"%\" invalid expression", currenttoken.spelling); } else System.out.println("postfix expression is: " + Et); } public String parsee() { String Tt = parset(); String Et = Tt; while (currenttoken.kind == Token.PLUS currenttoken.kind == Token.MINUS) { String op = currenttoken.spelling; accept(); Tt = parset(); Et = Et + Tt + op; } return Et; } String parset() { String Ft = parsef(); String Tt = Ft; while (currenttoken.kind == Token.MULT currenttoken.kind == Token.DIV) { String op = currenttoken.spelling; accept(); Ft = parsef(); Tt = Tt + Ft + op; COMP3131/9102 Page 345 April 8, 2018

} return Tt; } } String parsef() { String Ft = null; switch (currenttoken.kind) { case Token.INTLITERAL: Ft = currenttoken.spelling; accept(); break; case Token.LPAREN: accept(); String Et = parsee(); Ft = Et; match(token.rparen); break; default: syntacticerror("\"%\" cannot start F", currenttoken.spelling); break; } return Ft; } COMP3131/9102 Page 346 April 8, 2018

An Attribute Grammar for Constructing ASTs PRODUCTION SEMANTIC RULE E T [E.ast = T.ast] ( + T [E.ast = new BinaryExpr (E.ast, +, T.ast)] - T [E.ast = new BinaryExpr (E.ast, -,T.ast)] ) T F [T.ast = F.ast] ( * F [T.ast = new BinaryExpr (T.ast, *,F.ast)] / F [T.ast = new BinaryExpr (T.ast, /,F.ast)] ) F INT F ( E ) [F.ast = new IntExpr ( new IntLiteral (int. str ))] [F.ast = E.ast] A single attribute ast denoting a reference to a node object BinaryExpr, IntExpr, IntLiteral are AST constructors A parser for building the AST can be written similarly as the one in Slide 345 except that t is replaced with ast! COMP3131/9102 Page 347 April 8, 2018

Parsing Method for A α private AST A parsea() { } AST A itsast; parse α and constructs itsast; return itsast; where AST A is the abstract class for the nonterminal A. parsea parses a A-phrase and returns its AST as a result The body of parsea constructs the A-phrase s AST by combining the ASTs of its subphrases (or by creating terminal nodes) COMP3131/9102 Page 348 April 8, 2018

The Parsing Method for Statement Stmt parsestmt() throws SyntaxError { Stmt sast = null; switch (currenttoken.kind) { } case Token.LCURLY: sast = parsecompoundstmt(); break; case Token.IF: sast = parseifstmt(); break;... parsecompoundstmt, parseifstmt,... return concrete nodes or objects, which are instances of concrete AST classes, CompoundStmt, IfStmt,... The return type Stmt is abstract; Stmt is the abstract class for the nonterminal stmt in the VC grammar COMP3131/9102 Page 349 April 8, 2018

Implementation Details Specific to Assignment 3 1. ASTs must reflect correctly operator precedence and associativity 2. All lists (StmtList, DeclList, ArgList and ParaList) implemented as: EBNF: StmtList -> ( something ) BNF (with right recursion): StmtList 1 -> ǫ Stmt Stmt StmtList 2 The Attrbute Grammar: StmtList 1 -> ǫ StmtList 1.AST = new EmptyStmtList() Stmt StmtList 1.AST = new StmtList(Stmt.AST, new EmptyStmtList) Stmt StmtList 2 StmtList 1.AST = new StmtList(Stmt.AST, StmtList 2.AST) See parsestmtlist in Parser.java See the supplied test cases for Assignment 3 COMP3131/9102 Page 350 April 8, 2018

Implementation Details Specific to Assignment 3 (Cont d) StmtList S1; S2; S3 = S1 S2 StmtList StmtList S3 EmptyStmtList 3. Create EmptyExpr nodes for empty expressions: Expr-Stmt -> Expr? ";" when ExprStmt = ";" 4. All references must not be null Use EmptyXYZ COMP3131/9102 Page 351 April 8, 2018

Reading Assignment 3 spec Syntax trees: 5.2 of Red Dragon ( 5.3.1 of Purple Dragon) Attribute grammar (or syntax-directed translation): Pages 279 287 of (Red) / 5.1 (Purple) Section 2.3 (Red & Purple) Next Class: Attribute Grammars COMP3131/9102 Page 352 April 8, 2018