Grammar Variation in Compiler Design Carl Wu. Three topics Syntax Grammar vs. AST Component(?)-based grammar Aspect-oriented grammar.

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Control Flow Analysis (Chapter 7) Mooly Sagiv (with Contributions by Hanne Riis Nielson)
Abstract Syntax Mooly Sagiv html:// 1.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Exercise 1: Balanced Parentheses Show that the following balanced parentheses grammar is ambiguous (by finding two parse trees for some input sequence)
Aspect Oriented Programming. AOP Contents 1 Overview 2 Terminology 3 The Problem 4 The Solution 4 Join point models 5 Implementation 6 Terminology Review.
CS 31003: Compilers  Difference between SLR and LR(1)  Construction of LR(1) parsing table  LALR parser Bandi Sumanth 11CS30006 Date : 9/10/2013.
SableCC SableCC is developed by professors and graduate students at McGill University and is open source (licensed under the Apache License, Version 2.0)‏
Syntax Trees MathWorks Compiler Course – Day 5. Syntax Trees MathWorks Compiler Course – Day 5 Parser lexemes shift/reduce seq. Cfg tables Tree Symbols.
10/22/2002© 2002 Hal Perkins & UW CSEG-1 CSE 582 – Compilers Intermediate Representations Hal Perkins Autumn 2002.
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
CS Summer 2005 Top-down and Bottom-up Parsing - a whirlwind tour June 20, 2005 Slide acknowledgment: Radu Rugina, CS 412.
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
Chapter 2 A Simple Compiler
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
Generative Programming. Generic vs Generative Generic Programming focuses on representing families of domain concepts Generic Programming focuses on representing.
8/19/2015© Hal Perkins & UW CSEC-1 CSE P 501 – Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008.
Parser construction tools: YACC
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
1 Top Down Parsing. CS 412/413 Spring 2008Introduction to Compilers2 Outline Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
McLab Tutorial Part 3 – McLab Frontend Frontend organization Introduction to Beaver Introduction to JastAdd 6/4/2011 Frontend-1McLab.
Abstract Syntax Trees Compiler Baojian Hua
A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University.
Weaving a Debugging Aspect into Domain-Specific Language Grammars SAC ’05 PSC Track Santa Fe, New Mexico USA March 17, 2005 Hui Wu, Jeff Gray, Marjan Mernik,
1 Parsers and Grammar. 2 Categories of Grammar Rules  Declarations or definitions. AttributeDeclaration ::= [ final ] [ static ] [ access ] datatype.
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
12/18/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Winter 2008.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Costas Busch - LSU1 Parsing. Costas Busch - LSU2 Compiler Program File v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,5.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
Epsilon Compiler Advanced Compilers Progress Report James Simister 11/3/99.
1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
1 Compiler Construction Vana Doufexi office CS dept.
Announcements/Reading
Compiler Design (40-414) Main Text Book:
Chapter 1 Introduction.
Introduction to Compiler Construction
CS510 Compiler Lecture 4.
50/50 rule You need to get 50% from tests, AND
CS 614: Theory and Construction of Compilers
Chapter 1 Introduction.
Compiler Lecture 1 CS510.
4 (c) parsing.
Basic Program Analysis: AST
Syntax-Directed Translation
CPSC 388 – Compiler Design and Construction
Bottom-up derivation tree, original grammar
Implementing Language Extensions with Model Transformations
CSE401 Introduction to Compiler Construction
R.Rajkumar Asst.Professor CSE
Parsing Costas Busch - LSU.
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Intermediate Representations Hal Perkins Autumn 2005
LL and Recursive-Descent Parsing
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
Implementing Language Extensions with Model Transformations
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Review for the Midterm. Overview (Chapter 1):
Presentation transcript:

Grammar Variation in Compiler Design Carl Wu

Three topics Syntax Grammar vs. AST Component(?)-based grammar Aspect-oriented grammar

Grammar vs. AST (I) How to automatically generate a tree from a grammar?

Grammar vs. AST (I) Stmt ::= Block | “if” Expr “then” Stmt | IdUse “:=” Exp

Grammar vs. AST (I) Stmt ::= Block | “if” Exp “then” Stmt | IdUse “:=” Exp JastAdd Specification (Tree) abstract Stmt; BlockStmt : Stmt ::= Block; IfStmt : Stmt ::= Exp Stmt; AssignStmt : Stmt ::= IdUse Exp;

Grammar vs. AST (I) Restricted CFG Definition A ::= B C D √ => aggregation A ::= B | C | D √ => inheritance A ::= B C | D ×

Grammar vs. AST (I) RCFG Specification Stmt :: Block | IfStmt | AssignStmt IfStmt :: “if” Exp “then” Stmt AssignStmt :: IdUse “:=” Exp

Grammar vs. AST (II) Parse tree vs. IR tree

Grammar vs. AST (II) In an IDE, there are multiple visitors for the same source code (>12 !). Different requirement for the tree structure: –Syntax vs. semantics –Immutable vs. transformable (optimization) –Parse tree vs. IR tree

Grammar vs. AST (II) Generate two tree structures from the same grammar! One immutable, strong-typed, concrete parse tree – Read only! One transferable, untyped, abstract IR tree – Read and write!

Grammar vs. AST (II) IfStmt :: “if” Exp “then” Stmt Class ASTNode{ protected ASTNode[] children; } class IfStmt extends ASTNode{ final protected Token token_if, Exp exp, Token token_then, Stmt stmt; IfStmt(Token token_if, Exp exp, Token token_then, Stmt stmt){ // parse tree construction this.token_if = token_if; this.exp = exp; this.token_then = token_then; this.stmt = stmt; // IR tree construction children[0] = exp; children[1] = stmt; }

Component(?)-based grammar

Component vs. module What is the different between a component and a module? What is a modularized grammar? What is an ideal component-based grammar?

Component vs. module Grammar Component Parser Grammar Module Grammar Parser Modularized grammar Component-based grammar

Benefits Benefits from modularized grammar –Easy to read, write, change –Eliminate naming conflicts Additional benefits brought from component- based grammar –Each component can be designed, developed and tested individually. –Any change to certain component does not require compiling all the other components. –Different type of grammars/parsing algorithms can be used for different component, e.g., one component can be LL, one can be LALR.

Difficulty in designing component- based grammar No clear guards between two components. –Switch the control to a new parser or stay in the same? –Suitable for embed languages, e.g., Jscript in Html –Not suitable for an integral language, e.g., Java Two much coupling between two components. –Not just reuse the component as a whole, may also reuse the internal productions and symbols. –Not applicable for LR parsers, once the table is built, you can’t reuse the internal productions (no way to jump into a table).

Ideal vs. reality

Suggestions?

Aspect-oriented grammar

Join-point: grammar patterns that crosscut multiple productions Punctuations, identifiers, modifiers…

Example ";“ appears 25 times in one of the Java grammars “.” appears 74 times in one of the Cobol grammars Every one of them should be carefully placed!

::= '.' | '.' | '.' pointcut PreDot(): ; after PreDot(): ‘.'

Another example pointcut Content(): … … before Content(): “(”; after Content(): “)”; Guarantee they match!

Grammar weaving Base Grammar Grammar Aspect Result grammar Parser

What do you think?