CSE 431S Final Review. Washington University Spring 2013

Size: px
Start display at page:

Download "CSE 431S Final Review. Washington University Spring 2013"

Transcription

1 CSE 431S Final Review Washington University Spring 2013

2 What You Should Know The six stages of a compiler and what each stage does. The input to and output of each compilation stage (especially the back-end). Context-free languages. Definition of a context-free grammar (including the formal definition). Leftmost and rightmost derivations and parse trees. Ambiguity.

3 What You Should Know Bottom-up (shift-reduce) parsing. LR(0) parser construction. SLR conflict resolution. LR(1) parser construction. Abstract syntax trees. L-value vs R-value Static type checking. Symbol tables.

4 What You Should Know CUP actions. Jasmin basics. Code generation. Call stack (function activation). Stack-based vs heap-based memory allocation. Parameter passing mechanisms. Register allocation (graph coloring).

5 Context-Free Languages Recall right-linear grammars: X a Y b Restricted right-hand side Context-free grammars: Allow anything on the right-hand side. A ( A ) x

6 Context-Free Grammars A grammar is a 4-tuple: : set of terminals V: set of nonterminals S: start nonterminal P: set of productions (rewrite rules) For a grammar to be context-free, all productions must be of the form: A α, where α is any sequence of symbols (terminals and nonterminals)

7 Ambiguity What about: E E + E a Two syntax trees for the string a + a + a E E E + E E + E E + E a a E + E a a a a

8 Ambiguity If there are multiple parse trees--or, equivalently, multiple leftmost derivations--for some string then the grammar is ambiguous. Note that it is the grammar that is ambiguous, not the language. There may exist a non-ambiguous grammar for the same language.

9 Bottom-Up Parsing Instead of starting from a start nonterminal and producing the parse tree, start from the leaves and build tree bottom up Start nonterminal is now the goal nonterminal

10 Sample Grammar 1. S E $ 2. E E + T 3. T 4. T a 5. ( E )

11 LR(0) Item The dot represents the current parse state (e.g. what has been seen ) The initial set of rules are the called the kernel The non-kernel items are generated from the closure operation and represents any nonterminals after the dot

12 LR(0) Parse States I 0 = START S E $

13 LR(0) Parse States I 0 = START S E $ E E + T E T T a T ( E ) The closure operation adds all of the rules for a nonterminal to the immediate right of the dot Close on the operation The number in the square indicates which state to go to on the symbol to the right of the dot Must go to a single state for each symbol (deterministic)

14 LR(0) Parse States I 0 = START S E $ E E + T E T T a T ( E ) I 1 = GOTO(I 0, E ) S E $ E E + T There must be only one state with a given kernel i.e., no identical states

15 1. S A C $ 2. A a B C d 3. B Q 4. λ 5. B b B 6. d 7. C c 8. λ 9. Q q Example

16 LR(0) Parse States I 0 S A C $ 1 I 3 S A C $ I 7 A a B C d 8 A a B C d 5 A B Q 12 I 4 I 8 A C c A a B C d B b B 9 B d I 1 S A C $ C c C I 5 I 6 A a B C d B b B B d A a B C d I 9 B b B B b B B d I 10 B b B I 2 S A C $ 3 C c C 4

17 I 13 A B Q I 14 Q q LR(0) Parse States I 11 B d I 12 A B Q Q q Grammar is not LR(0) parsable: shift/reduce conflicts in states 0, 1, and 6

18 SLR(1) Create the LR(0) states. If there are no conflicts then we are done. For states with conflicts Try to use follow sets to resolve the conflicts. If all conflicts can be resolved using the follow sets then the grammar is SLR(1).

19 SLR(1) Shift/Reduce conflict Need to make sure that every terminal to the immediate right of a in not in the Follow set of the nonterminal of the reduction rule I 1 S A C $ C c C I 6 A a B C d C c C States 1 and 6: Follow(C) = { d, $ } so c is not an element of Follow(C)

20 SLR(1) I 0 S A C $ A a B C d A B Q A B b B B d All conflicts can be resolved using the Follow sets, so the grammar is SLR parsable State 0: Follow(A) = { c, $ } so a, b, and d are not elements of Follow(A)

21 SLR(1) State Table State a b c d q $ A B C Q S 0 S5 S9 R4 S12 R4 S1 S13 Done 1 S4 R8 R8 S2 2 S3 3 R1 4 R7 5 S9 S12 S6 6 S4 R8 R8 S7 7 S8 8 R2 9 S9 S11 S10 10 R5 11 R6 12 S14 S13 13 R3 14 R9

22 Sample Parse Stack Remaining Input - 0 S5 a b b d d c $ - 0 a 5 S9 b b d d c $ - 0 a 5 b 9 S9 b d d c $ - 0 a 5 b 9 b 9 S11 d d c $ - 0 a 5 b 9 b 9 d 11 R6 d c $ - 0 a 5 b 9 b 9 S10 B d c $ - 0 a 5 b 9 b 9 B 10 R5 d c $

23 Sample Parse Stack Remaining Input - 0 a 5 b 9 S10 B d c $ - 0 a 5 b 9 B 10 S9 d c $ - 0 a 5 R5 B d c $ - 0 a 5 B 6 S6 d c $ - 0 a 5 B 6 R8 C d c $ - 0 a 5 B 6 C 7 S7 d c $ - 0 a 5 B 6 C 7 d 8 S8 c $

24 Sample Parse Stack Remaining Input - 0 R2 A c $ - 0 A 1 S1 c $ - 0 A 1 c 4 S4 $ - 0 A 1 R7 C $ - 0 A 1 C 2 S2 $ - 0 A 1 C 2 $ 3 S3-0 R1 Done S

25 Syntax Trees Concrete Actual parse tree Abstract Eliminates unnecessary nodes Structures the tree appropriately for evaluation Serves as basis for code generation

26 Concrete vs. Abstract

27 Construction Java code added to productions Most common action is to build a new tree node and assign to RESULT, which attaches it to the left-hand nonterminal Values for the nonterminals on the right-hand side are usually child tree nodes Stmt ::= id:id assign E:e {: RESULT = new AssignmentNode(id, e); :} if lparen E:pr rparent Stmt:s fi {: RESULT = new IfNode(pr, s); :} if lparen E:pr rparent Stmt:s1 else Stmt:s2 fi {: RESULT = new IfNode(pr, s1, s2); :} ;

28 Construction Stmt ::= begin Stmts:block end {: RESULT = block; :} ; Stmts ::= Stmts:block semi Stmt:stmt {: block.add(stmt); RESULT = block; :} Stmt:s {: RESULT = new BlockNode(s); :} ;

29 Construction Alternate construction of BlockNode Stmt ::= begin Stmts:list end {: RESULT = new BlockNode(list); :} ; Stmts ::= Stmts:list semi Stmt:stmt {: list.add(stmt); RESULT = list; :} Stmt:s {: RESULT = new ArrayList(); RESULT.add(s); :} ;

30 Left and Right Values x = y x is the L-value Refers to the location of x, not its value y is the R-value Refers to the value of y, not its location

31 Example Note that there is an error in this figure. The deref in the tree for example b should not be there.

32 Type Checking When are types checked? Statically at compile time Compiler does type checking during compilation Ideally eliminate runtime checks Dynamically at runtime Compiler generates code to do type checking at runtime JavaScript vs. Java Java still does a large amount of runtime type checking We ll focus on static typing for basic types

33 Expression Types For every operator we need to know allowed types of operands resulting type implicit coercion changes the representation, not the data short to long implicit conversion may change the data int to float explicit cast may lose information float to int, int to short

34 What are the types? =? x int +? y int 3.14 float

35 Determining Types make sure type is allowed (int + float) assign the resultant type to the operator (float) generate any necessary coercion(s) or conversion(s) most hardware has (int + int) and (float + float) but not (int + float)

36 Adding Coercion =? x int + float int 2 float float 3.14 float y int

37 Explicit Casting = int x int float 2 int int + float int 2 float float 3.14 float y int

38 Symbol Table Proc Dcls Body Synthesize symbol info Proc Inherit symbol info Dcls int I; float j; Body i=3; j = i * 3.14;

39 Symbol Table Persists the synthesized information as a side effect of the translation Maps a name and environment to information Environment is the scope Scope is static Basic actions Establish a mapping Retrieve a mapping

40 public class Car { int id; int color; int GetType() { String id; } public class Wheel { Object id; int GetType() { float id; } } } Name Scope Info id Car int color Car int id Car:GetType String id Car:Wheel Object id Car:Wheel:GetType float

41 Scopes Scopes are static Scopes are nested LIFO (last in, first out) Car scope GetType scope Wheel scope GetType scope

42 Possible Implementations Option 1: Keep all information available at all times Option 2: Use LIFO and process a scope at a time Name Scope Info id Car int color Car int id Car:GetType String id Car:Wheel Object id Car:Wheel:GetType float

43 LIFO Scopes Symbol table will be a stack of maps of name to information One map per scope (environment) Four basic operations Enter Scope Leave Scope Add Symbol Lookup Symbol

44 Implementation Scopes are LIFO so using a stack makes sense For each scope, use a map since we lookup names to retrieve info about them Typically use a hash map

45 Hello World :: Source public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); } }

46 Hello World :: Jasmin.class public HelloWorld.super java/lang/object ; ; standard initializer (calls java.lang.object's initializer) ;.method public <init>()v aload_0 invokenonvirtual java/lang/object/<init>()v return.end method ; ; main() - prints out Hello World ;.method public static main([ljava/lang/string;)v.limit stack 2 ; up to two items can be pushed ; push System.out onto the stack getstatic java/lang/system/out Ljava/io/PrintStream ; ; push a string onto the stack ldc "Hello World! ; call the PrintStream.println() method. invokevirtual java/io/printstream/println(ljava/lang/string;)v ; done return.end method

47 Source to AST Source if (i > 431) { a = b + c; } AST IF_STATEMENT GREATER_THAN VAR_USE IDENTIFIER (i) (SymbolInfo: INT, lv = 0) INTEGER_LITERAL (431) BLOCK EXPRESSION_STATEMENT ASSIGN IDENTIFIER (a) (SymbolInfo: INT, lv = 1) ADDITION VAR_USE IDENTIFIER (b) (SymbolInfo: INT, lv = 2) VAR_USE IDENTIFIER (c) (SymbolInfo: INT, lv = 3)

48 AST to Code AST IF_STATEMENT GREATER_THAN VAR_USE IDENTIFIER (i) (SymbolInfo: INT, lv = 0) INTEGER_LITERAL (431) BLOCK EXPRESSION_STATEMENT ASSIGN IDENTIFIER (a) (SymbolInfo: INT, lv = 1) ADDITION VAR_USE IDENTIFIER (b) (SymbolInfo: INT, lv = 2) VAR_USE IDENTIFIER (c) (SymbolInfo: INT, lv = 3) Code iload 0 ldc 431 if_icmpgt label3 iconst_0 goto label4 label3: iconst_1 label4: ifeq label1 iload 2 iload 3 iadd istore 1 goto label2 label1: label2:

49 Break It Down IF_STATEMENT node Create two labels (will be needed later) Visit first child Code for boolean test expression should be generated Code for the boolean expression should leave 0 (for false) or 1 (for true) on top of stack Output code that compares top of stack to 0 and jump to label for else block (to be output later) if 0 Visit second child Code for then block should be generated Output code that jumps over else block and output label to start else block Visit third child (if it exists) Code for else block should be generated Output label at end of else block

50 IF_STATEMENT private void visitifstatementnode(astnode node) throws Exception { String elselabel = generatelabel(); String endlabel = generatelabel(); } node.getchild(0).accept(this); // visit first child stream.println(" ifeq " + elselabel); node.getchild(1).accept(this); // visit second child stream.println(" goto " + endlabel); stream.println(elselabel + ":"); ASTNode elseblock = node.getchild(2); if (elseblock!= null) { elseblock.accept(this); // visit third child } stream.println(endlabel + ":");

51 Run-time System The run-time system consists of everything needed at run-time to support the execution of a process. This includes memory management, call-stack management, system call API, etc.

52 Function Calls Invoke f during runtime What happens? 1. Parameters are transmitted 2. Local storage is allocated 3. Local storage is initialized 4. Body of f executes 5. Return values prepared 6. Free storage 7. Return context to call

53 Function Calls Each invocation of f is a new activation What is the lifetime of f?

54 Lifetime a b a b overlapping a b b disjoint a

55 Activation Use a stack to represent activations No activation specific info survives death No activation specific info required for birth Each activation pushes a new activation record onto the run-time stack What will we record in it?

56 Activation Record Return address Storage information Local storage Parameters Access to non-locals

57 Parameter Passing Call by value Argument is R-value Value of arguments are copied into the function swap(x, y) won t change the value of x or y Call by reference Argument is L-value Variable in function points to the same location as the argument swap(x, y) would change the value of x and y Most modern languages use call-by-value semantics

58 Parameter Passing Java uses call-by-value semantics It is sometimes said that Java uses call-by-value for primitives and call-by-reference for object types, but that is not quite true. Java is call-by-value for everything, except that it does not copy objects but rather copies references to the objects. That is, the caller and callee both have references to the same object.

59 Parameter Passing Does not work in Java Primitive parameters are copied void swap(int x, int y) { int t = x; x = y; y = t; }

60 Parameter Passing Still does not work in Java References to objects are copied void swap(integer x, Integer y) { Integer t = x; x = y; y = t; }

61 Parameter Passing Cannot swap the objects, but could change the internal state of the objects void swap(modinteger x, ModInteger y) { int t = x.getvalue(); x.setvalue(y.getvalue()); y.setvalue(t); }

62 Register Allocation Most architectures have only a handful of registers to use for calculations Values need to be copied from memory into registers when needed, and then copied back to memory when a register is needed for something else For performance, we want to minimize the number of copies to/from memory

63 Register Allocation Can build an interference graph to determine what variables are live at the same time First, determine the live ranges of variables based on their "use" and "def" A def is an assignment to a variable (L-value) A use is the use of the value of a variable (R-value)

64 Live Ranges x y z x = y = z = = x Variables with ranges that overlap are live at the same time and therefore must use different registers to avoid extra copying in and out of memory = z = y

65 Interference Graph Each variable is a vertex in the graph An edge in the graph indicates that those two variables are live at the same time So the edges indicate which variables cannot share a register x y z

66 Graph Coloring The problem of allocating registers now becomes one of coloring the interference graph We want to color the vertices of the graph so that no two adjacent vertices have the same color The maximum number of colors we can use is the equal to the number of available registers A coloring with a maximum number of colors k is called a k- coloring But k-coloring a graph is NP-complete and we need it to be fast Use a heuristic algorithm

67 Graph Coloring Find a vertex whose edge count is < k Push the vertex on a stack and remove from the graph Repeat until there are no vertices left in the graph or there are no vertices with an edge count < k in the graph If all vertices have been removed from the graph then the graph can be k-colored Pop a vertex from the stack and add back to the graph Color the vertex a different color from any of its neighbors currently in the graph How can we know that there is an available color? Repeat until stack is empty

68 Graph Coloring Try k = 3 A B C G D E F

69 Graph Coloring A B C G D E F

70 Graph Coloring Note that if we get to a point when removing vertices from the graph where all of the remaining vertices have an edge count >= k then it does not necessarily mean the graph cannot be k-colored It just means the heuristic algorithm failed Could try a different algorithm But it could be that the graph is not k-colorable Will need to spill the registers At some point, copy the registers out to memory so we can use them to hold other variables

71 Parsers LR(0) 0 symbols of look ahead when creating the parse table SLR Simple LR resolves conflicts using global grammar follow sets LALR Look Ahead LR combines some states based on follow set information LR(k) Most powerful of those where parse states are created ahead of time

72 1. P S $ 2. S A B A C 3. a a c 4. A a a 5. B b 6. λ 7. C c 8. λ Yet Another Example

73 Kernel Rules I 0 P S $, {} Grammar Parse States I 2 S A B A C, {$} S a a c, {$} A a a, {b,a} S a a c, {$} A a a, {b, a} I 3 S a a c, {$} 3 I 1 S a a c, {$} A a a, {b, a} 2 2 I 4 S A B A C, {$} B b, {a} B, {a} I 5 B b, {a} 6 5 I 6 S A B A C, {$} A a a, {c,$} 7 9

74 Kernel Rules Grammar Parse States (cont.) I 7 A a a, {c,$} 8 I 12 P S $, {} 13 I 8 A a a, {c,$} I 13 P S $, {} I 9 S A B A C, {$} C c, {$} C, {$} I 10 C c, {$} I 11 S A B A C, {$}

CSE 431S Code Generation. Washington University Spring 2013

CSE 431S Code Generation. Washington University Spring 2013 CSE 431S Code Generation Washington University Spring 2013 Code Generation Visitor The code generator does not execute the program represented by the AST It outputs code to execute the program Source to

More information

02 B The Java Virtual Machine

02 B The Java Virtual Machine 02 B The Java Virtual Machine CS1102S: Data Structures and Algorithms Martin Henz January 22, 2010 Generated on Friday 22 nd January, 2010, 09:46 CS1102S: Data Structures and Algorithms 02 B The Java Virtual

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

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology MIT 6.035 Parse Table Construction Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Parse Tables (Review) ACTION Goto State ( ) $ X s0 shift to s2 error error goto s1

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

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table. MODULE 18 LALR parsing After understanding the most powerful CALR parser, in this module we will learn to construct the LALR parser. The CALR parser has a large set of items and hence the LALR parser is

More information

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing Parsing Wrapup Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing LR(1) items Computing closure Computing goto LR(1) canonical collection This lecture LR(1) parsing Building ACTION

More information

Wednesday, September 9, 15. Parsers

Wednesday, September 9, 15. Parsers Parsers What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

More information

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs: What is a parser Parsers A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

More information

Context-free grammars

Context-free grammars Context-free grammars Section 4.2 Formal way of specifying rules about the structure/syntax of a program terminals - tokens non-terminals - represent higher-level structures of a program start symbol,

More information

Wednesday, August 31, Parsers

Wednesday, August 31, Parsers Parsers How do we combine tokens? Combine tokens ( words in a language) to form programs ( sentences in a language) Not all combinations of tokens are correct programs (not all sentences are grammatically

More information

Compiler Construction: Parsing

Compiler Construction: Parsing Compiler Construction: Parsing Mandar Mitra Indian Statistical Institute M. Mitra (ISI) Parsing 1 / 33 Context-free grammars. Reference: Section 4.2 Formal way of specifying rules about the structure/syntax

More information

CSE 431S Type Checking. Washington University Spring 2013

CSE 431S Type Checking. Washington University Spring 2013 CSE 431S Type Checking Washington University Spring 2013 Type Checking When are types checked? Statically at compile time Compiler does type checking during compilation Ideally eliminate runtime checks

More information

CSC 4181 Handout : JVM

CSC 4181 Handout : JVM CSC 4181 Handout : JVM Note: This handout provides you with the basic information about JVM. Although we tried to be accurate about the description, there may be errors. Feel free to check your compiler

More information

Over-view. CSc Java programs. Java programs. Logging on, and logging o. Slides by Michael Weeks Copyright Unix basics. javac.

Over-view. CSc Java programs. Java programs. Logging on, and logging o. Slides by Michael Weeks Copyright Unix basics. javac. Over-view CSc 3210 Slides by Michael Weeks Copyright 2015 Unix basics javac java.j files javap 1 2 jasmin converting from javap to jasmin classfile structure calling methods adding line numbers Java programs

More information

Compiler construction 2009

Compiler construction 2009 Compiler construction 2009 Lecture 2 Code generation 1: Generating Jasmin code JVM and Java bytecode Jasmin Naive code generation The Java Virtual Machine Data types Primitive types, including integer

More information

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468 Parsers Xiaokang Qiu Purdue University ECE 468 August 31, 2018 What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure

More information

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1) TD parsing - LL(1) Parsing First and Follow sets Parse table construction BU Parsing Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1) Problems with SLR Aho, Sethi, Ullman, Compilers

More information

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

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012 LR(0) Drawbacks Consider the unambiguous augmented grammar: 0.) S E $ 1.) E T + E 2.) E T 3.) T x If we build the LR(0) DFA table, we find that there is a shift-reduce conflict. This arises because the

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 8! Bo;om- Up Parsing Shi?- Reduce LR(0) automata and

More information

Monday, September 13, Parsers

Monday, September 13, Parsers Parsers Agenda Terminology LL(1) Parsers Overview of LR Parsing Terminology Grammar G = (Vt, Vn, S, P) Vt is the set of terminals Vn is the set of non-terminals S is the start symbol P is the set of productions

More information

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F). CS 2210 Sample Midterm 1. Determine if each of the following claims is true (T) or false (F). F A language consists of a set of strings, its grammar structure, and a set of operations. (Note: a language

More information

Compiling Techniques

Compiling Techniques Lecture 10: Introduction to 10 November 2015 Coursework: Block and Procedure Table of contents Introduction 1 Introduction Overview Java Virtual Machine Frames and Function Call 2 JVM Types and Mnemonics

More information

Under the Hood: The Java Virtual Machine. Lecture 23 CS2110 Fall 2008

Under the Hood: The Java Virtual Machine. Lecture 23 CS2110 Fall 2008 Under the Hood: The Java Virtual Machine Lecture 23 CS2110 Fall 2008 Compiling for Different Platforms Program written in some high-level language (C, Fortran, ML,...) Compiled to intermediate form Optimized

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

Time : 1 Hour Max Marks : 30

Time : 1 Hour Max Marks : 30 Total No. of Questions : 6 P4890 B.E/ Insem.- 74 B.E ( Computer Engg) PRINCIPLES OF MODERN COMPILER DESIGN (2012 Pattern) (Semester I) Time : 1 Hour Max Marks : 30 Q.1 a) Explain need of symbol table with

More information

JVM. What This Topic is About. Course Overview. Recap: Interpretive Compilers. Abstract Machines. Abstract Machines. Class Files and Class File Format

JVM. What This Topic is About. Course Overview. Recap: Interpretive Compilers. Abstract Machines. Abstract Machines. Class Files and Class File Format Course Overview What This Topic is About PART I: overview material 1 Introduction 2 Language processors (tombstone diagrams, bootstrapping) 3 Architecture of a compiler PART II: inside a compiler 4 Syntax

More information

CS 406/534 Compiler Construction Putting It All Together

CS 406/534 Compiler Construction Putting It All Together CS 406/534 Compiler Construction Putting It All Together Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy

More information

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

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4020 July 5, 2007 14.00-15.30 This exam (8 pages) consists of 60 True/False

More information

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

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4303 April 9, 2010 14.00-15.30 This exam (6 pages) consists of 52 True/False

More information

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino 3. Syntax Analysis Andrea Polini Formal Languages and Compilers Master in Computer Science University of Camerino (Formal Languages and Compilers) 3. Syntax Analysis CS@UNICAM 1 / 54 Syntax Analysis: the

More information

Tutorial 3: Code Generation

Tutorial 3: Code Generation S C I E N C E P A S S I O N T E C H N O L O G Y Tutorial 3: Code Generation Univ.-Prof. Dr. Franz Wotawa, DI Roxane Koitz, Stephan Frühwirt, Christopher Liebmann, Martin Zimmermann Institute for Software

More information

LR Parsing Techniques

LR Parsing Techniques LR Parsing Techniques Introduction Bottom-Up Parsing LR Parsing as Handle Pruning Shift-Reduce Parser LR(k) Parsing Model Parsing Table Construction: SLR, LR, LALR 1 Bottom-UP Parsing A bottom-up parser

More information

LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators LR Parsing LALR Parser Generators Outline Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the

More information

Configuration Sets for CSX- Lite. Parser Action Table

Configuration Sets for CSX- Lite. Parser Action Table Configuration Sets for CSX- Lite State s 6 s 7 Cofiguration Set Prog { Stmts } Eof Stmts Stmt Stmts State s s Cofiguration Set Prog { Stmts } Eof Prog { Stmts } Eof Stmts Stmt Stmts Stmts λ Stmt if ( Expr

More information

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE 1 SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE Tatsuya Hagino hagino@sfc.keio.ac.jp slides URL https://vu5.sfc.keio.ac.jp/sa/ Java Programming Language Java Introduced in 1995 Object-oriented programming

More information

UNIT-III BOTTOM-UP PARSING

UNIT-III BOTTOM-UP PARSING UNIT-III BOTTOM-UP PARSING Constructing a parse tree for an input string beginning at the leaves and going towards the root is called bottom-up parsing. A general type of bottom-up parser is a shift-reduce

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

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University Code Generation Frédéric Haziza Department of Computer Systems Uppsala University Spring 2008 Operating Systems Process Management Memory Management Storage Management Compilers Compiling

More information

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

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax Susan Eggers 1 CSE 401 Syntax Analysis/Parsing Context-free grammars (CFG s) Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax

More information

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing

More information

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Formal Languages and Compilers Lecture VII Part 3: Syntactic A Formal Languages and Compilers Lecture VII Part 3: Syntactic Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

More information

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1 CS453 : JavaCUP and error recovery CS453 Shift-reduce Parsing 1 Shift-reduce parsing in an LR parser LR(k) parser Left-to-right parse Right-most derivation K-token look ahead LR parsing algorithm using

More information

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Formal Languages and Compilers Lecture VII Part 4: Syntactic A Formal Languages and Compilers Lecture VII Part 4: Syntactic Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

More information

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous. Section A 1. What do you meant by parser and its types? A parser for grammar G is a program that takes as input a string w and produces as output either a parse tree for w, if w is a sentence of G, or

More information

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing Also known as Shift-Reduce parsing More powerful than top down Don t need left factored grammars Can handle left recursion Attempt to construct parse tree from an input string eginning at leaves and working

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

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem: Expression evaluation CSE 504 Order of evaluation For the abstract syntax tree + + 5 Expression Evaluation, Runtime Environments + + x 3 2 4 the equivalent expression is (x + 3) + (2 + 4) + 5 1 2 (. Contd

More information

In One Slide. Outline. LR Parsing. Table Construction

In One Slide. Outline. LR Parsing. Table Construction LR Parsing Table Construction #1 In One Slide An LR(1) parsing table can be constructed automatically from a CFG. An LR(1) item is a pair made up of a production and a lookahead token; it represents a

More information

Space Exploration EECS /25

Space Exploration EECS /25 1/25 Space Exploration EECS 4315 www.eecs.yorku.ca/course/4315/ Nondeterminism 2/25 Nondeterministic code is code that, even for the same input, can exhibit different behaviours on different runs, as opposed

More information

LALR Parsing. What Yacc and most compilers employ.

LALR Parsing. What Yacc and most compilers employ. LALR Parsing Canonical sets of LR(1) items Number of states much larger than in the SLR construction LR(1) = Order of thousands for a standard prog. Lang. SLR(1) = order of hundreds for a standard prog.

More information

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer Torben./Egidius Mogensen Introduction to Compiler Design ^ Springer Contents 1 Lexical Analysis 1 1.1 Regular Expressions 2 1.1.1 Shorthands 4 1.1.2 Examples 5 1.2 Nondeterministic Finite Automata 6 1.3

More information

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite LALR r Driver Action Table for CSX-Lite Given the GoTo and parser action tables, a Shift/Reduce (LALR) parser is fairly simple: { S 5 9 5 9 void LALRDriver(){ Push(S ); } R S R R R R5 if S S R S R5 while(true){

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

S Y N T A X A N A L Y S I S LR

S Y N T A X A N A L Y S I S LR LR parsing There are three commonly used algorithms to build tables for an LR parser: 1. SLR(1) = LR(0) plus use of FOLLOW set to select between actions smallest class of grammars smallest tables (number

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

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

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4020 January 19, 2006 14.00-15.30 This exam (8 pages) consists of 60 True/False

More information

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals. Bottom-up Parsing: Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals. Basic operation is to shift terminals from the input to the

More information

Under the Hood: The Java Virtual Machine. Problem: Too Many Platforms! Compiling for Different Platforms. Compiling for Different Platforms

Under the Hood: The Java Virtual Machine. Problem: Too Many Platforms! Compiling for Different Platforms. Compiling for Different Platforms Compiling for Different Platforms Under the Hood: The Java Virtual Machine Program written in some high-level language (C, Fortran, ML, ) Compiled to intermediate form Optimized Code generated for various

More information

Course Overview. PART I: overview material. PART II: inside a compiler. PART III: conclusion

Course Overview. PART I: overview material. PART II: inside a compiler. PART III: conclusion Course Overview PART I: overview material 1 Introduction (today) 2 Language Processors (basic terminology, tombstone diagrams, bootstrapping) 3 The architecture of a Compiler PART II: inside a compiler

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

Syntax Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Syntax Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay Syntax Analysis (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay September 2007 College of Engineering, Pune Syntax Analysis: 2/124 Syntax

More information

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

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1 Semantic Processing The Lexer and Parser Found lexical and syntax errors Built Abstract Syntax Tree Now!Find semantic errors.!build information about the program. Later!Generate IR Code!Optimize IR Code!Generate

More information

SYNTAX ANALYSIS 1. Define parser. Hierarchical analysis is one in which the tokens are grouped hierarchically into nested collections with collective meaning. Also termed as Parsing. 2. Mention the basic

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Building Compilers with Phoenix

Building Compilers with Phoenix Building Compilers with Phoenix Parser Generators: ANTLR History of ANTLR ANother Tool for Language Recognition Terence Parr's dissertation: Obtaining Practical Variants of LL(k) and LR(k) for k > 1 PCCTS:

More information

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1 Table of Contents About the Authors... iii Introduction... xvii Chapter 1: System Software... 1 1.1 Concept of System Software... 2 Types of Software Programs... 2 Software Programs and the Computing Machine...

More information

Lecture Bottom-Up Parsing

Lecture Bottom-Up Parsing Lecture 14+15 Bottom-Up Parsing CS 241: Foundations of Sequential Programs Winter 2018 Troy Vasiga et al University of Waterloo 1 Example CFG 1. S S 2. S AyB 3. A ab 4. A cd 5. B z 6. B wz 2 Stacks in

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

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

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

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id: Error Detection in LALR Parsers In bottom-up, LALR parsers syntax errors are discovered when a blank (error) entry is fetched from the parser action table. Let s again trace how the following illegal CSX-lite

More information

The Structure of a Syntax-Directed Compiler

The Structure of a Syntax-Directed Compiler Source Program (Character Stream) Scanner Tokens Parser Abstract Syntax Tree Type Checker (AST) Decorated AST Translator Intermediate Representation Symbol Tables Optimizer (IR) IR Code Generator Target

More information

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility Quiz Review Q1. What is the advantage of binding things as early as possible? Is there any advantage to delaying binding? Answer: Early binding generally leads to greater efficiency (compilation approach)

More information

CSE-304 Compiler Design

CSE-304 Compiler Design CSE-304 Compiler Design Fall 09 Final Exam December 14, 2009 Duration: 2 hours, 30 minutes. Maximum Score: 30 Name: USB ID Number: INSTRUCTIONS Read the following carefully before answering any question.

More information

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

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1 CSE P 501 Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008 1/15/2008 2002-08 Hal Perkins & UW CSE C-1 Agenda for Today Parsing overview Context free grammars Ambiguous grammars Reading:

More information

Syntax Errors; Static Semantics

Syntax Errors; Static Semantics Dealing with Syntax Errors Syntax Errors; Static Semantics Lecture 14 (from notes by R. Bodik) One purpose of the parser is to filter out errors that show up in parsing Later stages should not have to

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

JVML Instruction Set. How to get more than 256 local variables! Method Calls. Example. Method Calls

JVML Instruction Set. How to get more than 256 local variables! Method Calls. Example. Method Calls CS6: Program and Data Representation University of Virginia Computer Science Spring 006 David Evans Lecture 8: Code Safety and Virtual Machines (Duke suicide picture by Gary McGraw) pushing constants JVML

More information

Lexical and Syntax Analysis. Bottom-Up Parsing

Lexical and Syntax Analysis. Bottom-Up Parsing Lexical and Syntax Analysis Bottom-Up Parsing Parsing There are two ways to construct derivation of a grammar. Top-Down: begin with start symbol; repeatedly replace an instance of a production s LHS with

More information

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008 Parsing: continued David Notkin Autumn 2008 Parsing Algorithms Earley s algorithm (1970) works for all CFGs O(N 3 ) worst case performance O(N 2 ) for unambiguous grammars Based on dynamic programming,

More information

LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators Outline LR Parsing LALR Parser Generators Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the

More information

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309 PART 3 - SYNTAX ANALYSIS F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 64 / 309 Goals Definition of the syntax of a programming language using context free grammars Methods for parsing

More information

Context-free grammars (CFG s)

Context-free grammars (CFG s) Syntax Analysis/Parsing Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax tree (AST) AST: captures hierarchical structure of

More information

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

More information

Conflicts in LR Parsing and More LR Parsing Types

Conflicts in LR Parsing and More LR Parsing Types Conflicts in LR Parsing and More LR Parsing Types Lecture 10 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 2 Due Friday, Apr. 24, 11:55pm The usual lecture time is being replaced by a discussion

More information

Lecture 8: Deterministic Bottom-Up Parsing

Lecture 8: Deterministic Bottom-Up Parsing Lecture 8: Deterministic Bottom-Up Parsing (From slides by G. Necula & R. Bodik) Last modified: Fri Feb 12 13:02:57 2010 CS164: Lecture #8 1 Avoiding nondeterministic choice: LR We ve been looking at general

More information

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing 8 Parsing Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces strings A parser constructs a parse tree for a string

More information

CSE P 501 Exam Sample Solution 12/1/11

CSE P 501 Exam Sample Solution 12/1/11 Question 1. (10 points, 5 each) Regular expressions. Give regular expressions that generate the following sets of strings. You may only use the basic operations of concatenation, choice ( ), and repetition

More information

Bottom-Up Parsing. Lecture 11-12

Bottom-Up Parsing. Lecture 11-12 Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 9/22/06 Prof. Hilfinger CS164 Lecture 11 1 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient

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

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

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1 Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery Last modified: Mon Feb 23 10:05:56 2015 CS164: Lecture #14 1 Shift/Reduce Conflicts If a DFA state contains both [X: α aβ, b] and [Y: γ, a],

More information

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam Compilers Type checking Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Summary of parsing Parsing A solid foundation: context-free grammars A simple parser: LL(1) A more powerful parser:

More information

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6 Compiler Design 1 Bottom-UP Parsing Compiler Design 2 The Process The parse tree is built starting from the leaf nodes labeled by the terminals (tokens). The parser tries to discover appropriate reductions,

More information

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis Chapter 4 Lexical and Syntax Analysis Introduction - Language implementation systems must analyze source code, regardless of the specific implementation approach - Nearly all syntax analysis is based on

More information

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

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1 CSE P 501 Compilers Static Semantics Hal Perkins Winter 2008 1/22/2008 2002-08 Hal Perkins & UW CSE I-1 Agenda Static semantics Types Attribute grammars Representing types Symbol tables Note: this covers

More information

Downloaded from Page 1. LR Parsing

Downloaded from  Page 1. LR Parsing Downloaded from http://himadri.cmsdu.org Page 1 LR Parsing We first understand Context Free Grammars. Consider the input string: x+2*y When scanned by a scanner, it produces the following stream of tokens:

More information

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1 CSE P 501 Compilers Java Implementation JVMs, JITs &c Hal Perkins Winter 2008 3/11/2008 2002-08 Hal Perkins & UW CSE V-1 Agenda Java virtual machine architecture.class files Class loading Execution engines

More information

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1 CSE P 501 Compilers LR Parsing Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 D-1 Agenda LR Parsing Table-driven Parsers Parser States Shift-Reduce and Reduce-Reduce conflicts UW CSE P 501 Spring 2018

More information

Lecture 7: Deterministic Bottom-Up Parsing

Lecture 7: Deterministic Bottom-Up Parsing Lecture 7: Deterministic Bottom-Up Parsing (From slides by G. Necula & R. Bodik) Last modified: Tue Sep 20 12:50:42 2011 CS164: Lecture #7 1 Avoiding nondeterministic choice: LR We ve been looking at general

More information