Abstract Syntax. Leonidas Fegaras. CSE 5317/4305 L5: Abstract Syntax 1

Size: px
Start display at page:

Download "Abstract Syntax. Leonidas Fegaras. CSE 5317/4305 L5: Abstract Syntax 1"

Transcription

1 Abstract Syntax Leonidas Fegaras CSE 5317/4305 L5: Abstract Syntax 1

2 Abstract Syntax Tree (AST) A parser typically generates an Abstract Syntax Tree (AST): source file get next character scanner get token token parser AST A parse tree is not an AST E T E + F T E F T x * F id(x) + id(y) * id(z) y z CSE 5317/4305 L5: Abstract Syntax 2

3 Building Abstract Syntax Trees in Java abstract class Exp { class IntegerExp extends Exp { public int value; public IntegerExp ( int n ) { value=n; class TrueExp extends Exp { public TrueExp () { class FalseExp extends Exp { public FalseExp () { class VariableExp extends Exp { public String value; public VariableExp ( String n ) { value=n; CSE 5317/4305 L5: Abstract Syntax 3

4 Exp (cont.) class BinaryExp extends Exp { public String operator; public Exp left; public Exp right; public BinaryExp ( String o, Exp l, Exp r ) { operator=o; left=l; right=r; class UnaryExp extends Exp { public String operator; public Exp operand; public UnaryExp ( String o, Exp e ) { operator=o; operand=e; class ExpList { public Exp head; public ExpList next; public ExpList ( Exp h, ExpList n ) { head=h; next=n; CSE 5317/4305 L5: Abstract Syntax 4

5 Exp (cont.) class CallExp extends Exp { public String name; public ExpList arguments; public CallExp ( String nm, ExpList s ) { name=nm; arguments=s; class ProjectionExp extends Exp { public Exp value; public String attribute; public ProjectionExp ( Exp v, String a ) { value=v; attribute=a; CSE 5317/4305 L5: Abstract Syntax 5

6 Exp (cont.) class RecordElements { public String attribute; public Exp value; public RecordElements next; public RecordElements ( String a, Exp v, RecordElements el ) { attribute=a; value=v; next=el; class RecordExp extends Exp { public RecordElements elements; public RecordExp ( RecordElements el ) { elements=el; CSE 5317/4305 L5: Abstract Syntax 6

7 The AST for the input new BinaryExp("+", Examples (x-2)+3 new BinaryExp("-", new IntegerExp(3)) The AST for the input f(x.a,true) new CallExp( f, new VariableExp("x"), new IntegerExp(2)), new ExpList(new ProjectionExp(new VariableExp("x"), A ), new ExpList(new TrueExp(),null))) CSE 5317/4305 L5: Abstract Syntax 7

8 Gen A Java package for constructing and manipulating ASTs You are required to use Gen for your project It is basically a Java preprocessor that adds syntactic constructs to the Java language to make the task of handling ASTs easier uses a universal class Tree to capture any kind of AST supports easy construction of ASTs using the #<...> syntax supports pattern matching, editing, pretty-printing, etc includes a symbol table class Architecture: file.gen file.java file.class Gen javac CSE 5317/4305 L5: Abstract Syntax 8

9 The Gen Tree Class abstract class Tree { class LongLeaf extends Tree { public long value; public LongLeaf ( long n ) { value = n; class DoubleLeaf extends Tree { public double value; public DoubleLeaf ( double n ) { value = n; class VariableLeaf extends Tree { public String value; public VariableLeaf ( String s ) { value = s; class StringLeaf extends Tree { public String value; public StringLeaf ( String s ) { value = s; CSE 5317/4305 L5: Abstract Syntax 9

10 AST Nodes are Instances of Tree class Node extends Tree { public String name; public Trees args; public Node ( String n, Trees a ) { tag = n; args = a; class Trees { public Tree head; public Trees tail; public Trees ( Tree h, Trees t ); public final static Trees nil; public Trees append ( Tree e ); CSE 5317/4305 L5: Abstract Syntax 10

11 Example To construct Binop(Plus,x,Binop(Minus,y,z)) in Java, use: new Node("Binop", Trees.nil.append(new VariableLeaf("Plus")).append(new VariableLeaf("x")).append(new Node("Binop", Binop Trees.nil.append(new VariableLeaf("Minus")).append(new VariableLeaf("y")) Plus x Binop.append(new VariableLeaf("z"))))) Ugly! You should never use this kind of code in your project Minus y z CSE 5317/4305 L5: Abstract Syntax 11

12 The #< > Brackets When you write #<Binop(Plus,x,Binop(Minus,y,z))> in your Gen file, it generates the following Java code: new Node("Binop", Trees.nil.append(new VariableLeaf("Plus")).append(new VariableLeaf("x")).append(new Node("Binop", which represents the AST: Binop(Plus,x,Binop(Minus,y,z)) Trees.nil.append(new VariableLeaf("Minus")).append(new VariableLeaf("y")).append(new VariableLeaf("z")))))new CSE 5317/4305 L5: Abstract Syntax 12

13 Escaping a Value Using Backquote Objects of the class Tree can be included into the form generated by the #< > brackets by escaping them with a backquote (`) The operand of the escape operator is expected to be an object of class Tree that provides the value to fill in the hole in the bracketed text at that point actually, an escaped string/long/double value is also lifted to a Tree For example Tree x = #<join(a,b,p)>; Tree y = #<select(`x,q)>; Tree z = #<project(`y,a)>; are equivalent to: Tree x = #<join(a,b,p)>; Tree y = #<select(join(a,b,p),q)>; Tree z = #<project(select(join(a,b,p),q),a)>; CSE 5317/4305 L5: Abstract Syntax 13

14 BNF of #< > bracketed ::= "#<" expr ">" construct an AST (instance of Tree) "#[" arg ","... "," arg "]" construct a list of ASTs (instance of Trees) expr ::= name the representation of a variable name long the repr. of a long integer double the repr. of a double number string the repr. of a string "`" name escaping to the value of name "`(" code ")" escaping to the value of code name "(" arg ","... "," arg ") the repr. of an AST node "`" name "(" arg ","... "," arg ")" the repr. of an AST node with escaped name arg ::= expr the repr. of an expression "..." name escaping to a list of ASTs bound to name "...(" code ")" escaping to a list of ASTs returned by code CSE 5317/4305 L5: Abstract Syntax 14

15 ... is for Trees The three dots (...) construct is used to indicate a list of children in an AST node name in...name must be an instance of the class Trees For example, in Trees r = #[join(a,b,p),select(c,q)]; Tree z = #<project(...r)>; z will be bound to #<project(join(a,b,p),select(c,q))> CSE 5317/4305 L5: Abstract Syntax 15

16 For example, Example #<`f(6,...r,g("ab",`(k(x))),`y)> is equivalent to the following Java code: new Node(f, Trees.nil.append(new LongLeaf(6)).append(r).append(new Node("g",Trees.nil.append(new StringLeaf("ab")).append(k(x)))).append(y) If f="h", r=#[2,z], y=#<m(1,"a")>, and k(x) returns the value #<8>, then the above term is equivalent to #<h(6,2,z,g("ab",8),m(1,"a"))> CSE 5317/4305 L5: Abstract Syntax 16

17 Pattern Matching Gen provides a match statement syntax for pattern matching Patterns match the Tree representations with similar shape Escape operators applied to variables inside these patterns represent variable patterns, which bind to corresponding subterms upon a successful match This capability makes it particularly easy to write functions that perform source-to-source transformations CSE 5317/4305 L5: Abstract Syntax 17

18 Example A function that simplifies arithmetic expressions: Tree simplify ( Tree e ) { match e { case plus(`x,0): return x; case times(`x,1): return x; case times(`x,0): return #<0>; case _: return e; where the _ pattern matches any value. For example, simplify(#<times(z,1)>) returns #<z> CSE 5317/4305 L5: Abstract Syntax 18

19 BNF case_stmt ::= "match" code { case... case "" case ::= "case" expr ":" code expr ::= name exact match with a variable name long exact match with a long integer double exact match with a double number string exact match with a string "`" name match with the value of name name "(" arg ","... "," arg ") match an AST node "`" name "(" arg ","... "," arg ")" match an AST node with escaped name "_" match any Ast arg ::= expr match an Ast "..." name match a list of ASTs bound to name "..." match the rest of the arguments CSE 5317/4305 L5: Abstract Syntax 19

20 Examples The pattern `f(...r) matches any Node when it is matched with #<join(a,b,c)>, it binds 1) f to the string "join" 2) r to the Arguments #[a,b,c] The following function adds the terms #<8> and #<9> as children to any Node e: Tree add_arg ( Tree e ) { match e { case `f(...r): return #<`f(8,9,...r)>; case `x: return x; CSE 5317/4305 L5: Abstract Syntax 20

21 Another Example The following function switches the inputs of a binary join found as a parameter to a Node e: Tree switch_join_args ( Tree e ) { match e { case `f(...r,join(`x,`y),...s): return #<`f(...r,join(`y,`x),...s)>; case `x: return x; CSE 5317/4305 L5: Abstract Syntax 21

22 Misc To iterate over Trees, use Java's for-loop: for ( Tree v: #[a,b,c] ) System.out.println(v); For conditional pattern matching, use if-then-else with a fail: match e { Case `f(`x): if (x instanceof VariableLeaf) fail; return #<`f(y)>; case `z: return z; CSE 5317/4305 L5: Abstract Syntax 22

23 Adding Semantic Actions to a Parser Right-associative grammar: E ::= T + E T - E T ::= num After left factoring: E ::= T E' E' ::= + E - E T ::= num Recursive descent parser: int E () { int left = T(); if (current_token == '+') { read_next_token(); return left + E(); else if (current_token == '-') { read_next_token(); return left - E(); else error(); ; int T () { if (current_token=='num') { int n = num_value; read_next_token(); return n; else error(); CSE 5317/4305 L5: Abstract Syntax 23 ;

24 Adding Semantic Actions to a Parser Left-associative grammar: E ::= E + T E - T T ::= num After left recursion elimination: E ::= T E' E' ::= + T E' - T E' T ::= num Recursive descent parser: int E () { return Eprime(T()); ; int Eprime ( int left ) { if (current_token=='+') { read_next_token(); return Eprime(left + T()); else if (current_token=='-') { read_next_token(); return Eprime(left - T()); else return left; ; int T () { if (current_token=='num') { int n = num_value; read_next_token(); return n; else error(); CSE 5317/4305 L5: Abstract Syntax 24 ;

25 Table-Driven Predictive Parsers Use the parse stack to push/pop both actions and symbols but they use a separate semantic stack to execute the actions push(s); read_next_token(); repeat X = pop(); if (X is a terminal or '$') if (X == current_token) read_next_token(); else error(); else if (X is an action) perform the action; else if (M[X,current_token] == "X ::= Y1 Y2... Yk") { push(yk);... push(y1); else error(); until X == '$'; CSE 5317/4305 L5: Abstract Syntax 25

26 Example Need to embed actions { code; in the grammar rules Suppose that pushv and popv are the functions to manipulate the semantic stack The following is the grammar of an interpreter that uses the semantic stack to perform additions and subtractions: E ::= T E' $ { print(popv()); E' ::= + T { pushv(popv() + popv()); E' - T { pushv(-popv() + popv()); E' T ::= num { pushv(num); For example, for 1+5-2, we have the following sequence of actions: pushv(1); pushv(5); pushv(popv()+popv()); pushv(2); pushv(-popv()+popv()); print(popv()); CSE 5317/4305 L5: Abstract Syntax 26

27 Bottom-Up Parsers can only perform an action after a reduction We can only have rules of the form X ::= Y1... Yn { action where the action is always at the end of the rule; this action is evaluated after the rule X ::= Y1... Yn is reduced How? In addition to state numbers, the parser pushes values into the parse stack If we want to put an action in the middle of the right-hand-side of a rule, we use a dummy non-terminal, called a marker For example, X ::= a { action b is equivalent to X ::= M b M ::= a { action CSE 5317/4305 L5: Abstract Syntax 27

28 CUP Both terminals and non-terminals are associated with typed values these values are instances of the Object class (or of some subclass of the Object class) the value associated with a terminal is in most cases an Object, except for an identifier which is a String, for an integer which is an Integer, etc the typical values associated with non-terminals in a compiler are ASTs, lists of ASTs, etc You can retrieve the value of a symbol s at the right-hand-side of a rule by using the notation s:x, where x is a variable name that hasn't appeared elsewhere in this rule The value of the non-terminal defined by a rule is called RESULT and should always be assigned a value in the action eg if the non-terminal E is associated with an Integer object, then E ::= E:n PLUS E:m {: RESULT = n+m; : CSE 5317/4305 L5: Abstract Syntax 28

29 Machinery The parse stack elements are of type struct( state: int, value: Object ) int is the state number Object is the value When a reduction occurs, the RESULT value is calculated from the values in the stack and is pushed along with the GOTO state Example: after the reduction by E ::= E:n PLUS E:m {: RESULT = n+m; : the RESULT value is stack[top-2].value + stack[top].value which is the new value pushed in the stack along with the GOTO state CSE 5317/4305 L5: Abstract Syntax 29

30 ASTs in CUP Need to associate each non-terminal symbol with an AST type non terminal Ast exp; non terminal Arguments expl; exp ::= exp:e1 PLUS exp:e2 {: RESULT = new Node(plus_exp,e1,e2); : exp:e1 MINUS exp:e2 {: RESULT = new Node(minus_exp,e1,e2); : id:nm LP expl:el RP {: RESULT = new Node(call_exp,el.reverse().cons(new VariableLeaf(nm))); : INT:n {: RESULT = new LongValue(n.intValue()); : ; expl ::= expl:el COMMA exp:e {: RESULT = el.cons(e); : exp:e {: RESULT = nil.cons(e); : ; CSE 5317/4305 L5: Abstract Syntax 30

Parsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1

Parsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1 Parsing #1 Leonidas Fegaras CSE 5317/4305 L3: Parsing #1 1 Parser source file get next character scanner get token parser AST token A parser recognizes sequences of tokens according to some grammar and

More information

Semantic Analysis. Leonidas Fegaras. CSE 5317/4305 L6: Semantic Analysis 1

Semantic Analysis. Leonidas Fegaras. CSE 5317/4305 L6: Semantic Analysis 1 Semantic Analysis Leonidas Fegaras CSE 5317/4305 L6: Semantic Analysis 1 Type Checking source file get next character scanner get token token parser AST type checking AST symbol table type errors Checking

More information

Chapter 3. Parsing #1

Chapter 3. Parsing #1 Chapter 3 Parsing #1 Parser source file get next character scanner get token parser AST token A parser recognizes sequences of tokens according to some grammar and generates Abstract Syntax Trees (ASTs)

More information

CSE 3302 Programming Languages Lecture 2: Syntax

CSE 3302 Programming Languages Lecture 2: Syntax CSE 3302 Programming Languages Lecture 2: Syntax (based on slides by Chengkai Li) Leonidas Fegaras University of Texas at Arlington CSE 3302 L2 Spring 2011 1 How do we define a PL? Specifying a PL: Syntax:

More information

A Simple Syntax-Directed Translator

A Simple Syntax-Directed Translator Chapter 2 A Simple Syntax-Directed Translator 1-1 Introduction The analysis phase of a compiler breaks up a source program into constituent pieces and produces an internal representation for it, called

More information

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! Any questions about the syllabus?! Course Material available at www.cs.unic.ac.cy/ioanna! Next time reading assignment [ALSU07]

More information

Syntax-Directed Translation. Lecture 14

Syntax-Directed Translation. Lecture 14 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik) 9/27/2006 Prof. Hilfinger, Lecture 14 1 Motivation: parser as a translator syntax-directed translation stream of tokens parser ASTs,

More information

Compiler Construction

Compiler Construction Compiler Construction Important facts: Name: Prof. Dr. Peter Thiemann Email: thiemann@informatik.uni-freiburg.de Office: 079-00-015 Exercises: Name: Dipl.-Inform. Manuel Geffken Email: geffken@informatik.uni-freiburg.de

More information

CS 352: Compilers: Principles and Practice

CS 352: Compilers: Principles and Practice CS 352: Compilers: Principles and Practice Important facts: Name: Dr. Xiangyu Zhang Email: xyzhang@cs.purdue.edu Office: LWSN 3154K Basis for grades: 15% midterm 1 15% midterm 2 25% final 30% project 15%

More information

Fundamentals of Compilation

Fundamentals of Compilation PART ONE Fundamentals of Compilation 1 Introduction A compiler was originally a program that compiled subroutines [a link-loader]. When in 1954 the combination algebraic compiler came into use, or rather

More information

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

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors

More information

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

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Statically vs. Dynamically typed languages

More information

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

Compiler Theory. (Semantic Analysis and Run-Time Environments) Compiler Theory (Semantic Analysis and Run-Time Environments) 005 Semantic Actions A compiler must do more than recognise whether a sentence belongs to the language of a grammar it must do something useful

More information

Semantic Analysis. Leonidas Fegaras. CSE 5317/4305 L6: Semantic Analysis 1

Semantic Analysis. Leonidas Fegaras. CSE 5317/4305 L6: Semantic Analysis 1 Semantic Analysis Leonidas Fegaras CSE 5317/4305 L6: Semantic Analysis 1 Type Checking source file get next character scanner get token token parser AST type checking AST symbol table type errors Checking

More information

The role of semantic analysis in a compiler

The role of semantic analysis in a compiler Semantic Analysis Outline The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors

More information

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

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF) Chapter 3: Describing Syntax and Semantics Introduction Formal methods of describing syntax (BNF) We can analyze syntax of a computer program on two levels: 1. Lexical level 2. Syntactic level Lexical

More information

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

A programming language requires two major definitions A simple one pass compiler A programming language requires two major definitions A simple one pass compiler [Syntax: what the language looks like A context-free grammar written in BNF (Backus-Naur Form) usually suffices. [Semantics:

More information

More On Syntax Directed Translation

More On Syntax Directed Translation More On Syntax Directed Translation 1 Types of Attributes We have productions of the form: A X 1 X 2 X 3... X n with semantic rules of the form: b:= f(c 1, c 2, c 3,..., c n ) where b and the c s are attributes

More information

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

Last Time. What do we want? When do we want it? An AST. Now! Java CUP 1 Last Time What do we want? An AST When do we want it? Now! 2 This Time A little review of ASTs The philosophy and use of a Parser Generator 3 Translating Lists CFG IdList -> id IdList comma

More information

How do LL(1) Parsers Build Syntax Trees?

How do LL(1) Parsers Build Syntax Trees? How do LL(1) Parsers Build Syntax Trees? So far our LL(1) parser has acted like a recognizer. It verifies that input token are syntactically correct, but it produces no output. Building complete (concrete)

More information

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang CSE 341 Section 7 Eric Mullen Spring 2017 Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang Outline Interpreting LBI (Language Being Implemented) Assume Correct Syntax Check for Correct

More information

CSE 413 Final Exam Spring 2011 Sample Solution. Strings of alternating 0 s and 1 s that begin and end with the same character, either 0 or 1.

CSE 413 Final Exam Spring 2011 Sample Solution. Strings of alternating 0 s and 1 s that begin and end with the same character, either 0 or 1. Question 1. (10 points) Regular expressions I. Describe the set of strings generated by each of the following regular expressions. For full credit, give a description of the sets like all sets of strings

More information

Table-Driven Top-Down Parsers

Table-Driven Top-Down Parsers Table-Driven Top-Down Parsers Recursive descent parsers have many attractive features. They are actual pieces of code that can be read by programmers and extended. This makes it fairly easy to understand

More information

CSE P 501 Exam 11/17/05 Sample Solution

CSE P 501 Exam 11/17/05 Sample Solution 1. (8 points) Write a regular expression or set of regular expressions that generate the following sets of strings. You can use abbreviations (i.e., name = regular expression) if it helps to make your

More information

CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen CSE 341 Section 7 Ethan Shea Autumn 2018 Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen Outline Interpreting MUPL Assume Correct Syntax Check for Correct Semantics Evaluating

More information

CPS 506 Comparative Programming Languages. Syntax Specification

CPS 506 Comparative Programming Languages. Syntax Specification CPS 506 Comparative Programming Languages Syntax Specification Compiling Process Steps Program Lexical Analysis Convert characters into a stream of tokens Lexical Analysis Syntactic Analysis Send tokens

More information

shift-reduce parsing

shift-reduce parsing Parsing #2 Bottom-up Parsing Rightmost derivations; use of rules from right to left Uses a stack to push symbols the concatenation of the stack symbols with the rest of the input forms a valid bottom-up

More information

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis CS 1622 Lecture 2 Lexical Analysis CS 1622 Lecture 2 1 Lecture 2 Review of last lecture and finish up overview The first compiler phase: lexical analysis Reading: Chapter 2 in text (by 1/18) CS 1622 Lecture

More information

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

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012 Predictive Parsers LL(k) Parsing Can we avoid backtracking? es, if for a given input symbol and given nonterminal, we can choose the alternative appropriately. his is possible if the first terminal of

More information

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

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis The Compiler So Far Overview of Semantic Analysis Adapted from Lectures by Profs. Alex Aiken and George Necula (UCB) Lexical analysis Detects inputs with illegal tokens Parsing Detects inputs with ill-formed

More information

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

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing Syntax/semantics Program program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing Meta-models 8/27/10 1 Program program execution Syntax Semantics

More information

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

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation Concepts Introduced in Chapter 2 A more detailed overview of the compilation process. Parsing Scanning Semantic Analysis Syntax-Directed Translation Intermediate Code Generation Context-Free Grammar A

More information

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

SEMANTIC ANALYSIS TYPES AND DECLARATIONS SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether

More information

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler. More detailed overview of compiler front end Structure of a compiler Today we ll take a quick look at typical parts of a compiler. This is to give a feeling for the overall structure. source program lexical

More information

ECE251 Midterm practice questions, Fall 2010

ECE251 Midterm practice questions, Fall 2010 ECE251 Midterm practice questions, Fall 2010 Patrick Lam October 20, 2010 Bootstrapping In particular, say you have a compiler from C to Pascal which runs on x86, and you want to write a self-hosting Java

More information

CSE 413 Final Exam. December 13, 2012

CSE 413 Final Exam. December 13, 2012 CSE 413 Final Exam December 13, 2012 Name The exam is closed book, closed notes, no electronic devices, signal flags, tin-can telephones, or other signaling or communications apparatus. Style and indenting

More information

Alternatives for semantic processing

Alternatives for semantic processing Semantic Processing Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies

More information

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

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised: EDAN65: Compilers, Lecture 06 A LR parsing Görel Hedin Revised: 2017-09-11 This lecture Regular expressions Context-free grammar Attribute grammar Lexical analyzer (scanner) Syntactic analyzer (parser)

More information

Question Points Score

Question Points Score CS 453 Introduction to Compilers Midterm Examination Spring 2009 March 12, 2009 75 minutes (maximum) Closed Book You may use one side of one sheet (8.5x11) of paper with any notes you like. This exam has

More information

CS 132 Compiler Construction

CS 132 Compiler Construction CS 132 Compiler Construction 1. Introduction 2 2. Lexical analysis 31 3. LL parsing 58 4. LR parsing 110 5. JavaCC and JTB 127 6. Semantic analysis 150 7. Translation and simplification 165 8. Liveness

More information

A simple syntax-directed

A simple syntax-directed Syntax-directed is a grammaroriented compiling technique Programming languages: Syntax: what its programs look like? Semantic: what its programs mean? 1 A simple syntax-directed Lexical Syntax Character

More information

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam Compilers Intermediate representations and code generation Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Today Intermediate representations and code generation Scanner Parser Semantic

More information

Postfix Notation is a notation in which the operator follows its operands in the expression (e.g ).

Postfix Notation is a notation in which the operator follows its operands in the expression (e.g ). Assignment 5 Introduction For this assignment, you will write classes to evaluate arithmetic expressions represented as text. For example, the string "1 2 ( * 4)" would evaluate to 15. This process will

More information

CSE au Final Exam Sample Solution

CSE au Final Exam Sample Solution CSE 413 12au Final Exam Sample Solution Question 1. (10 points) Regular expressions I. Describe the set of strings generated by each of the following regular expressions. For full credit, give a description

More information

Syntax and Grammars 1 / 21

Syntax and Grammars 1 / 21 Syntax and Grammars 1 / 21 Outline What is a language? Abstract syntax and grammars Abstract syntax vs. concrete syntax Encoding grammars as Haskell data types What is a language? 2 / 21 What is a language?

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

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

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria CS152 Programming Language Paradigms Prof. Tom Austin, Fall 2014 Syntax & Semantics, and Language Design Criteria Lab 1 solution (in class) Formally defining a language When we define a language, we need

More information

CSE P 501 Exam 8/5/04

CSE P 501 Exam 8/5/04 Name There are 7 questions worth a total of 65 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. You may refer to the following references: Course

More information

LECTURE 3. Compiler Phases

LECTURE 3. Compiler Phases LECTURE 3 Compiler Phases COMPILER PHASES Compilation of a program proceeds through a fixed series of phases. Each phase uses an (intermediate) form of the program produced by an earlier phase. Subsequent

More information

CS 406: Syntax Directed Translation

CS 406: Syntax Directed Translation CS 406: Syntax Directed Translation Stefan D. Bruda Winter 2015 SYNTAX DIRECTED TRANSLATION Syntax-directed translation the source language translation is completely driven by the parser The parsing process

More information

COP4020 Programming Assignment 2 - Fall 2016

COP4020 Programming Assignment 2 - Fall 2016 COP4020 Programming Assignment 2 - Fall 2016 To goal of this project is to implement in C or C++ (your choice) an interpreter that evaluates arithmetic expressions with variables in local scopes. The local

More information

CSE 413 Final Exam. June 7, 2011

CSE 413 Final Exam. June 7, 2011 CSE 413 Final Exam June 7, 2011 Name The exam is closed book, except that you may have a single page of hand-written notes for reference plus the page of notes you had for the midterm (although you are

More information

Parsing II Top-down parsing. Comp 412

Parsing II Top-down parsing. Comp 412 COMP 412 FALL 2018 Parsing II Top-down parsing Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled

More information

Lexical and Syntax Analysis. Top-Down Parsing

Lexical and Syntax Analysis. Top-Down Parsing Lexical and Syntax Analysis Top-Down Parsing Easy for humans to write and understand String of characters Lexemes identified String of tokens Easy for programs to transform Data structure Syntax A syntax

More information

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

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square) CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square) Introduction This semester, through a project split into 3 phases, we are going

More information

COMP3131/9102: Programming Languages and Compilers

COMP3131/9102: Programming Languages and Compilers 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

More information

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

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Parsing III (Top-down parsing: recursive descent & LL(1) ) (Bottom-up parsing) CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper,

More information

COP4020 Programming Assignment 2 Spring 2011

COP4020 Programming Assignment 2 Spring 2011 COP4020 Programming Assignment 2 Spring 2011 Consider our familiar augmented LL(1) grammar for an expression language (see Syntax lecture notes on the LL(1) expression grammar): ->

More information

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

Examples of attributes: values of evaluated subtrees, type information, source file coordinates, 1 2 3 Attributes can be added to the grammar symbols, and program fragments can be added as semantic actions to the grammar, to form a syntax-directed translation scheme. Some attributes may be set by

More information

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

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis? Anatomy of a Compiler Program (character stream) Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analysis Parse Tree Intermediate Code Generator Intermediate Code Optimizer Code Generator

More information

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

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 STUDENT ID: INSTRUCTIONS Please write your student ID on this page. Do not write it or your name

More information

ASTs, Objective CAML, and Ocamlyacc

ASTs, Objective CAML, and Ocamlyacc ASTs, Objective CAML, and Ocamlyacc Stephen A. Edwards Columbia University Fall 2012 Parsing and Syntax Trees Parsing decides if the program is part of the language. Not that useful: we want more than

More information

CSE302: Compiler Design

CSE302: Compiler Design CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University April 3, 2007 Outline Recap Syntax-directed

More information

CSCI312 Principles of Programming Languages

CSCI312 Principles of Programming Languages Copyright 2006 The McGraw-Hill Companies, Inc. CSCI312 Principles of Programming Languages! LL Parsing!! Xu Liu Derived from Keith Cooper s COMP 412 at Rice University Recap Copyright 2006 The McGraw-Hill

More information

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

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis? Semantic analysis and intermediate representations Which methods / formalisms are used in the various phases during the analysis? The task of this phase is to check the "static semantics" and generate

More information

Abstract Syntax. Mooly Sagiv. html://www.cs.tau.ac.il/~msagiv/courses/wcc06.html

Abstract Syntax. Mooly Sagiv. html://www.cs.tau.ac.il/~msagiv/courses/wcc06.html Abstract Syntax Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc06.html Outline The general idea Cup Motivating example Interpreter for arithmetic expressions The need for abstract syntax Abstract

More information

Semantic Analysis. Lecture 9. February 7, 2018

Semantic Analysis. Lecture 9. February 7, 2018 Semantic Analysis Lecture 9 February 7, 2018 Midterm 1 Compiler Stages 12 / 14 COOL Programming 10 / 12 Regular Languages 26 / 30 Context-free Languages 17 / 21 Parsing 20 / 23 Extra Credit 4 / 6 Average

More information

Chapter 4. Abstract Syntax

Chapter 4. Abstract Syntax Chapter 4 Abstract Syntax Outline compiler must do more than recognize whether a sentence belongs to the language of a grammar it must do something useful with that sentence. The semantic actions of a

More information

3.5 Practical Issues PRACTICAL ISSUES Error Recovery

3.5 Practical Issues PRACTICAL ISSUES Error Recovery 3.5 Practical Issues 141 3.5 PRACTICAL ISSUES Even with automatic parser generators, the compiler writer must manage several issues to produce a robust, efficient parser for a real programming language.

More information

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1 CSE 401 Compilers LR Parsing Hal Perkins Autumn 2011 10/10/2011 2002-11 Hal Perkins & UW CSE D-1 Agenda LR Parsing Table-driven Parsers Parser States Shift-Reduce and Reduce-Reduce conflicts 10/10/2011

More information

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

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer. The Compiler So Far CSC 4181 Compiler Construction Scanner - Lexical analysis Detects inputs with illegal tokens e.g.: main 5 (); Parser - Syntactic analysis Detects inputs with ill-formed parse trees

More information

CSE 401 Midterm Exam Sample Solution 11/4/11

CSE 401 Midterm Exam Sample Solution 11/4/11 Question 1. (12 points, 2 each) The front end of a compiler consists of three parts: scanner, parser, and (static) semantics. Collectively these need to analyze the input program and decide if it is correctly

More information

COP5621 Exam 3 - Spring 2005

COP5621 Exam 3 - Spring 2005 COP5621 Exam 3 - Spring 2005 Name: (Please print) Put the answers on these sheets. Use additional sheets when necessary. Show how you derived your answer when applicable (this is required for full cred

More information

Compilers. Compiler Construction Tutorial The Front-end

Compilers. Compiler Construction Tutorial The Front-end Compilers Compiler Construction Tutorial The Front-end Salahaddin University College of Engineering Software Engineering Department 2011-2012 Amanj Sherwany http://www.amanj.me/wiki/doku.php?id=teaching:su:compilers

More information

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

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking CSE450 Translation of Programming Languages Lecture 11: Semantic Analysis: Types & Type Checking Structure Project 1 - of a Project 2 - Compiler Today! Project 3 - Source Language Lexical Analyzer Syntax

More information

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers Plan for Today Review main idea syntax-directed evaluation and translation Recall syntax-directed interpretation in recursive descent parsers Syntax-directed evaluation and translation in shift-reduce

More information

CMSC 330: Organization of Programming Languages. Operational Semantics

CMSC 330: Organization of Programming Languages. Operational Semantics CMSC 330: Organization of Programming Languages Operational Semantics Notes about Project 4, Parts 1 & 2 Still due today (7/2) Will not be graded until 7/11 (along with Part 3) You are strongly encouraged

More information

SML-SYNTAX-LANGUAGE INTERPRETER IN JAVA. Jiahao Yuan Supervisor: Dr. Vijay Gehlot

SML-SYNTAX-LANGUAGE INTERPRETER IN JAVA. Jiahao Yuan Supervisor: Dr. Vijay Gehlot SML-SYNTAX-LANGUAGE INTERPRETER IN JAVA Jiahao Yuan Supervisor: Dr. Vijay Gehlot Target Design SML-Like-Syntax Build Parser in ANTLR Abstract Syntax Tree Representation ANLTER Integration In Interpreter

More information

Topic 3: Syntax Analysis I

Topic 3: Syntax Analysis I Topic 3: Syntax Analysis I Compiler Design Prof. Hanjun Kim CoreLab (Compiler Research Lab) POSTECH 1 Back-End Front-End The Front End Source Program Lexical Analysis Syntax Analysis Semantic Analysis

More information

Programming Languages Third Edition. Chapter 7 Basic Semantics

Programming Languages Third Edition. Chapter 7 Basic Semantics Programming Languages Third Edition Chapter 7 Basic Semantics Objectives Understand attributes, binding, and semantic functions Understand declarations, blocks, and scope Learn how to construct a symbol

More information

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) CSE 413 Languages & Implementation Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1 Goals Representing programs as data Racket structs as a better way to represent

More information

Project Compiler. CS031 TA Help Session November 28, 2011

Project Compiler. CS031 TA Help Session November 28, 2011 Project Compiler CS031 TA Help Session November 28, 2011 Motivation Generally, it s easier to program in higher-level languages than in assembly. Our goal is to automate the conversion from a higher-level

More information

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher COP4020 ming Languages Compilers and Interpreters Robert van Engelen & Chris Lacher Overview Common compiler and interpreter configurations Virtual machines Integrated development environments Compiler

More information

The SPL Programming Language Reference Manual

The SPL Programming Language Reference Manual The SPL Programming Language Reference Manual Leonidas Fegaras University of Texas at Arlington Arlington, TX 76019 fegaras@cse.uta.edu February 27, 2018 1 Introduction The SPL language is a Small Programming

More information

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5 What is Stack? Stack 1. Stack is LIFO Structure [ Last in First Out ] 2. Stack is Ordered List of Elements of Same Type. 3. Stack is Linear List 4. In Stack all Operations such as Insertion and Deletion

More information

G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32

G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32 G53CMP: Lecture 4 Syntactic Analysis: Parser Generators Henrik Nilsson University of Nottingham, UK G53CMP: Lecture 4 p.1/32 This Lecture Parser generators ( compiler compilers ) The parser generator Happy

More information

Compiler Construction 1. Introduction. Oscar Nierstrasz

Compiler Construction 1. Introduction. Oscar Nierstrasz Compiler Construction 1. Introduction Oscar Nierstrasz Compiler Construction Lecturers Assistants Lectures Exercises WWW Prof. Oscar Nierstrasz, Dr. Mircea Lungu Jan Kurš, Boris Spasojević E8 001, Fridays

More information

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1 CSE P 501 Compilers Intermediate Representations Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 G-1 Administrivia Semantics/types/symbol table project due ~2 weeks how goes it? Should be caught up on

More information

Sprite an animation manipulation language Language Reference Manual

Sprite an animation manipulation language Language Reference Manual Sprite an animation manipulation language Language Reference Manual Team Leader Dave Smith Team Members Dan Benamy John Morales Monica Ranadive Table of Contents A. Introduction...3 B. Lexical Conventions...3

More information

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

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Language Processing Systems Prof. Mohamed Hamada Software ngineering Lab. The University of Aizu Japan Intermediate/Code Generation Source language Scanner (lexical analysis) tokens Parser (syntax analysis)

More information

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser JavaCC Parser The Compilation Task Input character stream Lexer stream Parser Abstract Syntax Tree Analyser Annotated AST Code Generator Code CC&P 2003 1 CC&P 2003 2 Automated? JavaCC Parser The initial

More information

CS202 Compiler Construction. Christian Skalka. Course prerequisites. Solid programming skills a must.

CS202 Compiler Construction. Christian Skalka.  Course prerequisites. Solid programming skills a must. CS202 Compiler Construction Christian Skalka www.cs.uvm.edu/~skalka/202 CS 202-1 Introduction 1 Course prerequisites CS101 Must be able to read assembly CS103 Understand tree operations basic grammars

More information

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End

More information

Stating the obvious, people and computers do not speak the same language.

Stating the obvious, people and computers do not speak the same language. 3.4 SYSTEM SOFTWARE 3.4.3 TRANSLATION SOFTWARE INTRODUCTION Stating the obvious, people and computers do not speak the same language. People have to write programs in order to instruct a computer what

More information

Compiling Techniques

Compiling Techniques Lecture 2: The view from 35000 feet 19 September 2017 Table of contents 1 2 Passes Representations 3 Instruction Selection Register Allocation Instruction Scheduling 4 of a compiler Source Compiler Machine

More information

UNIT IV INTERMEDIATE CODE GENERATION

UNIT IV INTERMEDIATE CODE GENERATION UNIT IV INTERMEDIATE CODE GENERATION 2 Marks 1. Draw syntax tree for the expression a=b*-c+b*-c 2. Explain postfix notation. It is the linearized representation of syntax tree.it is a list of nodes of

More information

CSE 582 Autumn 2002 Exam 11/26/02

CSE 582 Autumn 2002 Exam 11/26/02 Name There are 8 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. You may refer to the following reference materials:

More information

Parsing III. (Top-down parsing: recursive descent & LL(1) )

Parsing III. (Top-down parsing: recursive descent & LL(1) ) Parsing III (Top-down parsing: recursive descent & LL(1) ) Roadmap (Where are we?) Previously We set out to study parsing Specifying syntax Context-free grammars Ambiguity Top-down parsers Algorithm &

More information

Lexical and Syntax Analysis

Lexical and Syntax Analysis Lexical and Syntax Analysis (of Programming Languages) Top-Down Parsing Lexical and Syntax Analysis (of Programming Languages) Top-Down Parsing Easy for humans to write and understand String of characters

More information

CS606- compiler instruction Solved MCQS From Midterm Papers

CS606- compiler instruction Solved MCQS From Midterm Papers CS606- compiler instruction Solved MCQS From Midterm Papers March 06,2014 MC100401285 Moaaz.pk@gmail.com Mc100401285@gmail.com PSMD01 Final Term MCQ s and Quizzes CS606- compiler instruction If X is a

More information