INF5110: Mandatory Exercise 1 Eyvind W. Axelsen @eyvindwa Slides are partly based on.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

1 JavaCUP JavaCUP (Construct Useful Parser) is a parser generator Produce a parser written in java, itself is also written in Java; There are many parser.
Exercise 1: Balanced Parentheses Show that the following balanced parentheses grammar is ambiguous (by finding two parse trees for some input sequence)
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Context-Free Grammars Lecture 7
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
The Structure of the GNAT Compiler. A target-independent Ada95 front-end for GCC Ada components C components SyntaxSemExpandgigiGCC AST Annotated AST.
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
Compiler Construction Parsing I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
INF5110: Mandatory Exercise 2 Eyvind W. Axelsen @eyvindwa Slides are partly based on.
Parser construction tools: YACC
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
1 Chapter 2 A Simple Compiler. 2 Outlines 2.1 The Structure of a Micro Compiler 2.2 A Micro Scanner 2.3 The Syntax of Micro 2.4 Recursive Descent Parsing.
贺天行.  First please download to appetitzer in our webpage  This tutorial will be mainly about codes provided in the appetizer.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Language Translators - Lee McCluskey LANGUAGE TRANSLATORS: WEEK 21 LECTURE: Using JavaCup to create simple interpreters
Automated Parser Generation (via CUP)CUP 1. High-level structure JFlexjavac Lexer spec Lexical analyzer text tokens.java CUPjavac Parser spec.javaParser.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
CPS 506 Comparative Programming Languages Syntax Specification.
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
Compiler Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
ISBN Chapter 3 Describing Syntax and Semantics.
Syntax and Grammars.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Context-Free Languages. Regular Languages Context-Free Languages.
©SoftMoore ConsultingSlide 1 Context-Free Grammars.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
MiniJava Compiler A multi-back-end JIT compiler of Java.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
CUP: An LALR Parser Generator for Java
Parsing & Context-Free Grammars
G. Pullaiah College of Engineering and Technology
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
Java CUP.
What does it mean? Notes from Robert Sebesta Programming Languages
CPSC 388 – Compiler Design and Construction
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Syntax-Directed Translation
Subject Name:Sysytem Software Subject Code: 10SCS52
R.Rajkumar Asst.Professor CSE
Context–free Grammar CFG is also called BNF (Backus-Naur Form) grammar
CS 3304 Comparative Languages
Syntax-Directed Translation
CS 3304 Comparative Languages
Chapter 3 Describing Syntax and Semantics.
BNF 9-Apr-19.
High-Level Programming Language
Compiler Construction
INF3110: Mandatory 1 and Exercises Part 1 Comments and Solutions
Parsing & Context-Free Grammars Hal Perkins Autumn 2005
Faculty of Computer Science and Information System
Presentation transcript:

INF5110: Mandatory Exercise 1 Eyvind W. Axelsen @eyvindwa Slides are partly based on material from previous years, made by Henning Berg, Fredrik Sørensen, and others.

Main goal Determine if programs written in the language Compila15 are syntactically valid. – Write a scanner – And a parser – Compila15 is described in detail in a separate document available on the course page.

Learning outcomes Using tools for scanner and parser generation – JFlex and CUP Variants of a grammar for the same language – Transforming from one form (extended BNF) to another (BNF for the tools we will be using). – Controlling precedence and associativity Defining ASTs as node classes in Java – Using the parsing tools to build such trees – Pretty-printing ASTs.

The Compila15 language at a glance program { class Complex { var Real : float; var Imag : float; } proc Add : Complex (a : Complex, b : Complex) { var retval : Complex; retval := new Complex; retval.Real := a.Real + b.Real; retval.Imag := a.Imag + b.Imag; return retval; } proc Main() { var c1 : Complex; var c2 : Complex; var result : Complex; … result := Add ( c1, c2 ); … return; } } The language supports very simple “classes”, but no real OO (inheritance, polymorphism, etc) Programs are written enclosed in program { … } Procedures are declared within programs (but not within classes). They perform calculations and create new objects. Execution starts in the Main method.

PROGRAM -> "program" "{" { DECL } "}" DECL -> VAR_DECL | PROC_DECL | CLASS_DECL VAR_DECL -> "var" NAME ":" TYPE ";" PROC_DECL -> "proc" NAME [ ":" TYPE ] "(" [ PARAM_DECL { "," PARAM_DECL } ] ")” "{" { DECL } { STMT } "}" CLASS_DECL -> "class" NAME "{" { VAR_DECL } "}" PARAM_DECL -> [ "ref" ] NAME ":" TYPE EXP -> EXP LOG_OP EXP | "not" EXP | EXP REL_OP EXP | EXP ARIT_OP EXP | "(" EXP ")” | LITERAL | CALL_STMT | "new" NAME | VAR VAR -> NAME | EXP "." NAME LOG_OP -> "&&" | "||" REL_OP -> " " | ">=" | "=" | "<>" ARIT_OP -> "+" | "-" | "*" | "/" | "#" LITERAL -> FLOAT_LITERAL | INT_LITERAL | STRING_LITERAL | "true" | "false" | "null" STMT -> ASSIGN_STMT ";” | IF_STMT | WHILE_STMT | RETURN_STMT ";” | CALL_STMT ";" ASSIGN_STMT -> VAR ":=" EXP IF_STMT -> "if" EXP "then" "{" { STMT } "}” [ "else" "{" { STMT } "}" ] WHILE_STMT -> "while" EXP "do" "{" { STMT } "}" RETURN_STMT -> "return" [ EXP ] CALL_STMT -> NAME "(" [ ACTUAL_PARAM { "," ACTUAL_PARAM } ] ")" ACTUAL_PARAM -> "ref" VAR | EXP TYPE -> "float" | "int" | "string" | "bool" | NAME Compila15 grammar “terminal” NON-TERMINAL [ optional] { repetition } Alternative1 | Alternative2

Tool: JFlex A tool to easily (YMMV) generate scanners – Input: lexical specification – Output: scanner program written in Java The lexical specification is written in a.lex file – Consists of three separate parts User code Options and macros Lexical rules

package oblig1parser; import java_cup.runtime.*; % %class Lexer %unicode %cup %{ private Symbol symbol(int type) { return new Symbol(type, yyline, yycolumn); } %} LineTerminator = \r|\n|\r\n % { "program” { return symbol(sym.PROGRAM); } "class” { return symbol(sym.CLASS); } "{” { return symbol(sym.LBRACK); } "}” { return symbol(sym.RBRACK); } “var” { return symbol(sym.VAR); } … } User code Options/ macros Lexical rules Copied to the generated class, before the class definition Options (class name, unicode support, CUP integration) Inserted into generated class Macros, defined as regular expressions The following rules are applicable from the initial state Lexical rules Defined in package java_cup.runtime. Variables holding current line/column

Tool: CUP – Construction of Useful Parsers - for Java A tool to easily (YMMV) generate parsers – Reads tokens from the scanner using next_token() The %cup option (prev. slide) makes this work – Input: Grammar defined as BNF with action code var_decl ::= VAR ID:name COLON ID:type SEMI {: RESULT = new VarDecl(name, type); :}; – Output: a parser program written in Java Build AST with user defined node classes (java code) Assign names to parts of production so we can reuse them in action code

Symbol list package oblig1parser; import java_cup.runtime.*; import syntaxtree.*; parser code {: :}; terminal PROGRAM, CLASS; terminal LBRACK, RBRACK; … terminal String ID; terminal String STRING_LITERAL; non terminal Program program; non terminal List decl_list; non terminal ClassDecl class_decl, decl; precedence left AND; program := PROGRAM LBRACK decl_list:dl RBRACK {: RESULT = new Program(dl); :} ; decl_list ::= decl:d {: List l = new LinkedList (); l.add(d); RESULT = l; :} ; decl ::= class_decl:sd {: RESULT = sd; :} ; class_decl ::= CLASS ID:name LBRACK RBRACK {: RESULT = new ClassDecl(name); :} ; Package/ imports User code Precedence Grammar Package name for generated code and imports of packages we need Code between {: and :} is inserted directly into the generated class (parser.java) Terminals and non-terminals are defined here. They can also be given a Java type for the “value” that they carry, e.g. a node in the AST Precedence declarations are listed in ascending order oblig1.cup The syntaxtree package contains our own AST classes AST is built during parsing

AST ASTNode Decl ClassDecl ProcDecl VarDecl Expr Statement … … … Make a reasonable structure This slide is an EXAMPLE Do not copy it verbatim without thinking

Tool: A Java-based build tool – Configuration in build.xml Can contain different targets, for instance test, clean, build, run, etc – The supplied configuration takes care of calling jflex, cup and javac for you. Note that ant might continue even if jflex or cup encounter errors!

Provided source code expression-eval.cup/lex Example expression language expression-par.cup/lex Example language that handles parentheses oblig1.cup/lex Starting point for your grammars in this exercise compila.ast Example showing how your pretty-printed AST could (should) look compila.cmp Compila source file; this is the file you need to parse in this exercise ClassDecl.java, Starting point for AST node implementations in Java Compiler.java The main entry point for the compiler. You do not necessarily have to change this

Putting it all together And more AST classes The provided ant build file takes care of this interaction

DEADLINE March 15th, 23:59 Don’t miss the deadline! – Extensions are only possible if you have an agreement with the student administration (studadm) – Contact them if you are sick, etc. Even if you are not 100% finished, deliver what you have before the deadline

Deliverables Working parser for Compila15 – Parse the supplied example program – Printout of the resulting AST Two grammars – One ambiguous, with ambiguities resolved through precedence declarations – One inherently unambiguous grammar Report – Front page with your name(s) and UiO user name(s) Work alone or in pairs. Groups of three can be allowed after an application. – Discussion of your solution – A comparison of the two grammars The code you supply must build with “ant” – Test your delivery on a UiO computer Deliver a zipped folder by to – Feel free to send questions at any time! – Read the exercise description thoroughly!