Project 3 - Parsing. Misc Details. Parser.java Recursive Descent Parser... Using Lexer.java

Similar documents
Programming Project #3: Syntax Analysis

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

Context-free grammars (CFG s)

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

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

The SPL Programming Language Reference Manual

The Abstract Syntax Tree Differences Between Tolmach's and Porter's Representations

Lab 2 Tutorial (An Informative Guide)

CSE 340 Fall 2014 Project 4

1 Lexical Considerations

ECE251 Midterm practice questions, Fall 2010

Compilers. Compiler Construction Tutorial The Front-end

The PCAT Programming Language Reference Manual

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

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

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

ASTs, Objective CAML, and Ocamlyacc

Fall Compiler Principles Lecture 4: Parsing part 3. Roman Manevich Ben-Gurion University of the Negev

Syntax-Directed Translation. Lecture 14

Question Points Score

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

Syntax Analysis The Parser Generator (BYacc/J)

Defining syntax using CFGs

Lexical Considerations

Context Free Grammar of KPL

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

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

Lexical Considerations

Modules, Structs, Hashes, and Operational Semantics

Class Information ANNOUCEMENTS

Lecture 14 Sections Mon, Mar 2, 2009

Fall Compiler Principles Lecture 5: Parsing part 4. Roman Manevich Ben-Gurion University

IR Opcodes. Project 8. Output from printir() CS-322 Project 8. File You Will Create: Generator.java. Important Files: Generator0.java IR.

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

A Simple Syntax-Directed Translator

Lexical and Syntax Analysis

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

Last time. What are compilers? Phases of a compiler. Scanner. Parser. Semantic Routines. Optimizer. Code Generation. Sunday, August 29, 2010

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

This class is about understanding how programs work

Semantic actions for declarations and expressions

Topic 3: Syntax Analysis I

A simple syntax-directed

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

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

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

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

cmps104a 2002q4 Assignment 3 LALR(1) Parser page 1

CS 230 Programming Languages

CS 536 Midterm Exam Spring 2013

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

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

Decaf Language Reference

Semantic actions for declarations and expressions

CPS 506 Comparative Programming Languages. Syntax Specification

Principles of Programming Languages COMP251: Syntax and Grammars

Syntax. In Text: Chapter 3

CMPT 379 Compilers. Parse trees

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

Chapter 3. Parsing #1

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Department of Electrical Engineering and Computer Science

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

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

Time : 1 Hour Max Marks : 30

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012

LECTURE 3. Compiler Phases

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Compiler construction in4020 lecture 5

Lecture 16: Static Semantics Overview 1

Principles of Programming Languages COMP251: Syntax and Grammars

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

Compiler Construction

A clarification on terminology: Recognizer: accepts or rejects strings in a language. Parser: recognizes and generates parse trees (imminent topic)

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

The Decaf Language. 1 Lexical considerations

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 5. Exam 1 overview. Type checking basics. HW4 due. HW5 out, due in 2 Tuesdays

Compilers Project 3: Semantic Analyzer

Syntax Errors; Static Semantics

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

Revisit the example. Transformed DFA 10/1/16 A B C D E. Start

Project 2 Interpreter for Snail. 2 The Snail Programming Language

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

CSE3322 Programming Languages and Implementation

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 2. Lexer design. Syntax Analysis: Context-Free Grammars. HW2 (out, due Tues)

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

Decaf Language Reference Manual

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

Semantic Analysis. Lecture 9. February 7, 2018

Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar

Compiler Construction

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

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

CMSC 330: Organization of Programming Languages

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Left to right design 1

CSE302: Compiler Design

CS:3820 Programming Language Concepts

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

Compiler Construction

Transcription:

Project 3 - Parsing Parser.java Recursive Descent Parser Using Lexer.java ParserStarter.java void scan () if nexttoken!= Token.EOF then nexttoken = lexer.gettoken (); endif void syntaxerror (msg) Print message Abort compiler void musthave (token, msg) if nexttoken == token then scan(); else syntaxerror(msg); endif 1 Misc Details Files in../compilers/p3 Create a new directory for each project Two tst directories Files with no syntax errors: tst/ go, run, runall Files with syntax errors: tst2/ go2, run2, runall2 Lexer.class Will use this when testing!!! Use your own Lexer.java but test with my Lexer.class (Should make no difference) Black Box: Main.jar FTP cd /pub/users/harry! ~harry/public_html/compilers 2

Main.java p = new Parser (); p.parseprogram (); p lexer nexttoken Parser Token.INTEGER Parser.java class Parser { Lexer lexer; int nexttoken; } Parser () {} void scan () {} void syntaxerror () {} void musthave () {} void parseprogram () {} void parseifstmt () {} void parseexpr () {} linenumber rvalue svalue In Starter File Lexer 5 ivalue 47 Later, we ll return Abstract Syntax Trees 3 For each non-terminal in the CFG Write a method You may need to modify the grammar some Body " { Decl } begin { Stmt } end Stmt " if Expr " while Expr " write WriteArgs ; " LValue := Expr ; " ID Arguments ; " LValue " ID Zero-or-more 4

Stmts " Stmt Stmts " # Stmt " if Expr " while Expr " write WriteArgs ; " IDStmt " Parse zero-or-more Stmt s method parsestmts() loop if nexttoken == IF then parseifstmt() elseif nexttoken == WHILE then parsewhilestmt() elseif nexttoken == WRITE then parsewritestmt() elseif nexttoken == ID then parseidstmt() else return endif endloop endmethod 5 Body " { Decl } begin { Stmt } end method parsebody() Parses zero-or-more Decl s parsedecls() musthave(begin, message) parsestmts() Parse zero-or-more Stmt s musthave(end, message) endmethod 6

WriteArgs " ( WriteExpr {, WriteExpr } ) " ( ) method parsewriteargs() musthave(lparen, Expecting ( ) if nexttoken == RPAREN then scan() return endif parsewriteexpr() while (nexttoken == COMMA) do scan() parsewriteexpr() endwhile musthave(rparen, Expecting, or ) ) endmethod 7 Statements That Start With ID Problem: Stmt " " LValue := Expr ; " ID Arguments ; " Arguments " ( Expr {, Expr } ) " ( ) Call Statement: foo(x,y,z); bar(); 8

Statements That Start With ID Problem: Stmt " " LValue := Expr ; " ID Arguments ; " Arguments " ( Expr {, Expr } ) " ( ) Solution: Stmt " " IDStmt " IDStmt " ID LValueMods := Expr ; " ID ( Arguments ; Arguments " Expr {, Expr } ) " ) Call Statement: foo(x,y,z); bar(); 9 a := MyArr { 1, 1, 2, 3, 5, 8, 13 }; a := MyArr { 1000 of -1 }; a := MyArr { 300 of -1, 150 of -2, -3, -4, x+y }; 10

a := MyArr {{ 1, 1, 2, 3, 5, 8, 13 }}; a := MyArr {{ 1000 of -1 }}; a := MyArr {{ 300 of -1, 150 of -2, -3, -4, x+y }}; A change to the PCAT language (see e-mail) 11 a := MyArr {{ 1, 1, 2, 3, 5, 8, 13 }}; a := MyArr {{ 1000 of -1 }}; a := MyArr {{ 300 of -1, 150 of -2, -3, -4, x+y }}; Runtime View: Arrays are stored in the heap! All array variables are 1 word in size. a: 0 1 2 3 4 5 6 7 1 1 2 3 5 8 13 The Array Size The Heap 12

a := MyArr {{ 1, 1, 2, 3, 5, 8, 13 }}; a := MyArr {{ 1000 of -1 }}; a := MyArr {{ 300 of -1, 150 of -2, -3, -4, x+y }}; Problem: ArrayValues " { { ArrayValue {, ArrayValue } } } ArrayValue " [ Expr of ] Expr Count Expression (optional) Value Expression (not optional) 13 a := MyArr {{ 1, 1, 2, 3, 5, 8, 13 }}; a := MyArr {{ 1000 of -1 }}; a := MyArr {{ 300 of -1, 150 of -2, -3, -4, x+y }}; Problem: ArrayValues " { { ArrayValue {, ArrayValue } } } ArrayValue " [ Expr of ] Expr Count Expression (optional) Value Expression (not optional) Expr VAL Expr CNT of Expr VAL 14

a := MyArr {{ 1, 1, 2, 3, 5, 8, 13 }}; a := MyArr {{ 1000 of -1 }}; a := MyArr {{ 300 of -1, 150 of -2, -3, -4, x+y }}; Problem: ArrayValues " { { ArrayValue {, ArrayValue } } } ArrayValue " [ Expr of ] Expr Count Expression (optional) Value Expression (not optional) Expr VAL Expr CNT of Expr VAL Solution: ArrayValue " Expr [ of Expr ] 15 LValue " ID " LValue [ Expr ] " LValue. ID Examples: x x[4] x[4][j+1][k*foo(y)] x.name x.next.next.next x.next.addr[3].street L-Values in PCAT Array References Field Accessing in Records next name addr next name addr 16

LValue " ID " LValue [ Expr ] " LValue. ID Examples: x x[4] x[4][j+1][k*foo(y)] x.name x.next.next.next x.next.addr[3].street Solution: L-Values in PCAT next name addr LValue " ID LValueMods LValueMods " { [ Expr ]. ID } Array References Field Accessing in Records next name addr 17 Expr UnaryOp BinaryOp Expressions in PCAT " INTEGER " REAL " LValue " ( Expr ) " UnaryOp Expr " Expr BinaryOp Expr " ID Arguments " ID FieldInits " ID ArrayValues " + - not " + - * / div mod or and " < <= > >= = <> All Binary ops are left-associative. Precedence should be foo() (Expr) unary+ unary- NOT * / MOD DIV AND + - OR < <= > >= = <> Highest Examples: 47 123.56 x x.field x[4] -(x+(y*4-k)) foo() MyRec{f1:=100; f2:=x} MyArr{1,1,2,3,5,6} Lowest 18

Solution Expr " Expr2 { ( < <= > >= = <> ) Expr2 } Expr2 " Expr3 { ( + - or ) Expr3 } Expr3 " Expr4 { ( * / mod div and ) Expr4 } Expr4 " + Expr4 - Expr4 not Expr4 Expr5 Expr5 " ( Expr ) " INTEGER " REAL " IDMods IDMods Methods: parseexpr () parseexpr2 () parseexpr3 () parseexpr4 () parseexpr5 () parseidmods () " ID Arguments " ID ArrayValues " ID FieldInits " ID LValueMods We know the FIRSTs of these: ( { { { < anything besides { > < anything else> 19 Checking Your Parser Add print statements Source: x := ( 1 + y ) div ( 2.3 < z ); Output: x ASSIGN 1 y PLUS 2.3 z LESS DIV ENDASSIGN Error Recovery: Very hard! Do the basic assignment first! Questions: ASK! Project 4: Build the AST Modify methods to build and return AST REQUIRES Project 3 to be working! 20