Top Banner
Compiler CompilerTutorial CSA2010 Compiler Techniques Gordon Mangion
28

Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Mar 18, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Compiler Compiler TutorialCSA2010 – Compiler Techniques

Gordon Mangion

Page 2: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Topics

Prerequisites

Compiler modules

Javacc

Semantic Analysis

Code generation

Examples

The assignment (VSL)

Page 3: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Prerequisites

Java

Regular Expressions

◦ ? + * …

Production rules and EBNF

Semantics

Page 4: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Compilers

What is a compiler?

Architecture of a Compiler

Where to start from?

Page 5: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Tombstone Diagram

Implementation

Language

Source Target

Language Language

Page 6: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Program Generators?

Source-to-Executable

Reverse Polish Notation

Source-to-Source

◦ Preprocessors

Page 7: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Javacc

A compiler that creates Parsers / Compilers

The source code is a definition file

◦ Grammar definition

◦ Inline Java code

The target is java-based parser

Recognizer?

Page 8: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Javacc

Javacclangdef.jj

*.java*.java

*.java*.java

Page 9: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

JJTree

From a recognizer to a parser

Preprocessor to Javacc

Produces Parse Tree building actions

Creates “.jj” files from “.jjt” files

Page 10: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

JJTree

Javacc

langdef.jjt

*.java*.java

*.java*.java

JJTree langdef.jj

Page 11: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Installing Javacc

https://javacc.dev.java.net/

◦ Javacc.zip / Javacc.tar.gz

Eclipse plugin

◦ Preferences->Install/Update->Add

http://eclipse-javacc.sourceforge.net/

◦ Help->New Software

Page 12: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

.jjt Grammar Files

Four (4) sections

◦ Options

◦ Parser

◦ Tokens

◦ Production Rules

Page 13: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

.jjt Options

options

{

BUILD_NODE_FILES=false;

STATIC=false;

MULTI=true;

}

Page 14: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

.jjt Parser block

PARSER_BEGIN(parser_name)

. . .

public class parser_name

{

}

PARSER_END(parser_name)

Page 15: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

.jjt Tokens

Lexeme to ignore

SKIP :

{

“ ”

| “\t”

| “\n”

| “\r”

| <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")>

}

Page 16: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

.jjt Tokens

Tokens that are to be returned

TOKEN :

{

<PLUS: “+”>

|<TRUE: "true" >

| <FALSE: "false" >

| <LETTER: (["A"-"Z"] | ["a"-"z"]) >

| <STRING_LITERAL: "\"" (~["\"","\r","\n"])* "\"" >

| <#DIGIT: [“0”-”9”]>

}

Page 17: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

.jjt Production Rules

Assume the following:

Header “Script” <STRING_LITERAL>

or

Header ::= “Script” <STRING_LITERAL>

Page 18: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

.jjt Production Rules

Assume the following:

Header ::= “Script” <STRING_LITERAL>

void Header():

{

<SCRIPT> <STRING_LITERAL>

}

Page 19: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

.jjt Production Rules

Header ::= “Script” <STRING_LITERAL>

void Header(): { int n = 0; n++;}

{

<SCRIPT> <STRING_LITERAL>

{ n++; }

}

Page 20: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

.jjt Production Rules

Calling Non-Terminals

void Integer(): {}

{ … }

void Header(): { }

{

<SCRIPT>

Integer()

}

Page 21: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

.jjt Production Rules

Token matching - Actions

void Header(): { int n = 0; n++; }

{

<SCRIPT>{ n = 1; }

<STRING_LITERAL>

{ n++; }

}

Page 22: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

.jjt Production Rules

Getting Token value

void Number()

{ Token t;

int n = 0; }

{

t=<DIGIT>{ n = integer.parseInt(t.image); }

}

Page 23: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Lookahead

Consider the following java statements

◦ public int name;

◦ public int name[];

◦ public int name() { … }

Page 24: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Building the AST

options

{

STATIC=false;

MULTI=true;

BUILD_NODE_FILES=true;

NODE_USES_PARSER=false;

NODE_PREFIX=“”;

}

Page 25: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Building the AST

SimpleNode Start() : {}

{

Expression()

“;”

<EOF>

{

return jjtThis;

}

}

Page 26: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Parsing

PARSER_BEGIN(MyParser)

...

MyParser parser = new MyParser(System.in);

try {

SimpleNode n = parser.Start();

System.out.println(“Parsed!");

} catch (Exception e) {

System.out.println("Oops!");

System.out.println(e.getMessage());

}

...

PARSER_END(MyParser)

Page 27: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

WorksheetDigit ::= [“0” - “9”]

Number ::= Digit+

Plus ::= “+”

Minus ::= “-”

Op ::= Plus | Minus

Expression ::= Number { Op Number }

Page 28: Compiler CompilerTutorial - Faculty of ICT Maltasspi3/CSA2010_Tutorials-Part1.pdf · 2010-03-17 · Javacc A compiler that creates Parsers / Compilers The source code is a definition

Example

Digit ::= [“0” - “9”]

Number ::= Digit+

Factor ::= Expression | Number

Term ::= Factor [ “*” | “/” Factor ]

Expression ::= Term { “+” | “-” Term }

Start ::= Expression