Compiler Design 22. ANTLR AST Traversal (AST as Input, AST Grammars)

Slides:



Advertisements
Similar presentations
COS 320 Compilers David Walker. Outline Last Week –Introduction to ML Today: –Lexical Analysis –Reading: Chapter 2 of Appel.
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
1 JavaCUP JavaCUP (Construct Useful Parser) is a parser generator Produce a parser written in java, itself is also written in Java; There are many parser.
Environments and Evaluation
Chapter 2 A Simple Compiler
StringTemplate Terence Parr University of San Francisco
Syntax Analysis – Part II Quick Look at Using Bison Top-Down Parsers EECS 483 – Lecture 5 University of Michigan Wednesday, September 20, 2006.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Getting Started with ANTLR Chapter 1. Domain Specific Languages DSLs are high-level languages designed for specific tasks DSLs include data formats, configuration.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
7-1 7 Contextual analysis  Aspects of contextual analysis  Scope checking  Type checking  Case study: Fun contextual analyser  Representing types.
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.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Comp 311 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright August 28, 2009.
1 Parsers and Grammar. 2 Categories of Grammar Rules  Declarations or definitions. AttributeDeclaration ::= [ final ] [ static ] [ access ] datatype.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
CS 153: Concepts of Compiler Design September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Comp 411 Principles of Programming Languages Lecture 3 Parsing
CSE 3302 Programming Languages
CS 3304 Comparative Languages
A Simple Syntax-Directed Translator
Constructing Precedence Table
CS 3304 Comparative Languages
Tutorial On Lex & Yacc.
Programming Languages Translator
Chapter 2 :: Programming Language Syntax
Lecture #12 Parsing Types.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Principles of programming languages 4: Parameter passing, Scope rules
PROGRAMMING LANGUAGES
Context-free Languages
Semantic Analysis Chapter 6.
CMPE 152: Compiler Design April 5 Class Meeting
4 (c) parsing.
Parsing Techniques.
Syntax-Directed Definition
Chapter 2: A Simple One Pass Compiler
CSE 3302 Programming Languages
Bottom-up derivation tree, original grammar
Compiler Design 7. Top-Down Table-Driven Parsing
CSE401 Introduction to Compiler Construction
CS 3304 Comparative Languages
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
Syntax-Directed Translation
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
ANTLR v3 Overview (for ANTLR v2 users)
CS 153: Concepts of Compiler Design October 30 Class Meeting
LL and Recursive-Descent Parsing
Chapter 4 Action Routines.
Syntax Analysis - Parsing
Chapter 2 :: Programming Language Syntax
CMPE 152: Compiler Design April 9 Class Meeting
The Recursive Descent Algorithm
Chapter 2 :: Programming Language Syntax
CMPE 152: Compiler Design March 21 Class Meeting
Compiler Construction
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Introduction to ANTLR Jin Tianxing
CMPE 152: Compiler Design March 28 Class Meeting
Course Overview PART I: overview material PART II: inside a compiler
Faculty of Computer Science and Information System
Presentation transcript:

Compiler Design 22. ANTLR AST Traversal (AST as Input, AST Grammars) Kanat Bolazar April 13, 2010

Chars → Tokens → AST → .... Lexer Parser Tree Parser

ANTLR Syntax grammar file, name.g one rule Trees /** doc comment */ kind grammar name; options {…} tokens {…} scopes… @header {…} @members {…} rules… /** doc comment */ rule[String s, int z] returns [int x, int y] throws E options {…} scopes @init {…} @after {…} :  |  ; catch [Exception e] {…} finally {…} Trees ^(root child1 … childN)

What is LL(*)? Natural extension to LL(k) lookahead DFA: Allow cyclic DFA that can skip ahead past common prefixes to see what follows Analogy: like trying to decide which line to get in at the movies: long line, can’t see sign ahead from the back; run ahead to see sign Predict and proceed normally with LL parse No need to specify k a priori Weakness: can’t deal with recursive left-prefixes ticket_line : PEOPLE+ STAR WARS 9 | PEOPLE+ AVATAR 2 ;

LL(*) Example Note: ‘x’, ‘y’ not in prediction DFA s : ID+ ':' ‘x’ | ID+ '.' ‘y’ ; void s() { int alt=0; while (LA(1)==ID) consume(); if ( LA(1)==‘:’ ) alt=1; if ( LA(1)==‘.’ ) alt=2; switch (alt) { case 1 : … case 2 : … default : error; } Note: ‘x’, ‘y’ not in prediction DFA

Tree Rewrite Rules Maps an input grammar fragment to an output tree grammar fragment grammar T; options {output=AST;} stat : 'return' expr ';' -> ^('return' expr) ; decl : 'int' ID (',' ID)* -> ^('int' ID+) ; decl : 'int' ID (',' ID)* -> ^('int' ID)+ ;

Template Rewrite Rules Reference template name with attribute assigments as args: Template assign is defined like this: grammar T; options {output=template;} s : ID '=' INT ';' -> assign(x={$ID.text},y={$INT.text}) ; group T; assign(x,y) ::= "<x> := <y>;"

ANTLR AST (Abstract Syntax Tree) Processing ANTLR allows creation and manipulation of ASTs 1. Generate an AST (file.mj → AST in memory) grammar MyLanguage; options { output = AST; ASTLabelType = CommonTree; } 2. Traverse, process AST → AST: tree grammar TypeChecker; tokenVocab = MyLanguage; 3. AST → action (Java): grammar Interpreter; options { tokenVocab = MyLanguage; }

AST Processing: Calculator 2, 3 ANTLR expression evaluator (calculator) examples: http://www.antlr.org/wiki/display/ANTLR3/Expression+evaluator We are interested in the examples that build an AST, and evaluate (interpret) the language AST. These are in the calculator.zip, as examples 2 and 3. grammar Expr; options { output=AST; ASTLabelType=CommonTree; } Expr AST tree grammar Eval; options { tokenVocab=Expr; ASTLabelType=CommonTree; } Eval

grammar Expr; options { output=AST; ASTLabelType=CommonTree; } prog: ( stat {System.out.println( $stat.tree.toStringTree());} )+ ; stat: expr NEWLINE -> expr | ID '=' expr NEWLINE -> ^('=' ID expr) | NEWLINE -> ; expr: multExpr (('+'^|'-'^) multExpr)* multExpr : atom ('*'^ atom)* atom: INT | ID | '('! expr ')'! tree grammar Eval; options { tokenVocab=Expr; ASTLabelType=CommonTree; } @header { import java.util.HashMap; } @members { HashMap memory = new HashMap(); } prog: stat+ ; stat: expr {System.out.println($expr.value);} | ^('=' ID expr) {memory.put($ID.text, new Integer($expr.value));} ; expr returns [int value] : ^('+' a=expr b=expr) {$value = a+b;} | ^('-' a=expr b=expr) {$value = a-b;} | ^('*' a=expr b=expr) {$value = a*b;} | ID { Integer v = (Integer)memory.get($ID.text); if ( v!=null ) $value = v.intValue(); else System.err.println("undefined var "+$ID.text); | INT {$value = Integer.parseInt($INT.text);}

AST → AST, AST → Template The ANTLR Tree construction page has examples of processing ASTs: AST → AST: Can be used for typechecking, processing (taking derivative of polynomials/formula) AST → Java (action): Often the final step where AST is needed no more. AST → Template: Can simplify Java/action when output is templatized Please see Calculator examples as well. They show which files have to be shared so tree grammars can be used.

Our Tree Grammar Look at sample output from our AST generator (syntax_test_ast.txt): 9. program X27 (program X27 10. 11. // constants 12. final int CONST = 25; (final (TYP int) CONST 25) 13. final char CH = '\n'; (final (TYP char) CH '\n') 14. final notype[] B3 = 35; (final (ARRAY notype) B3 35) 15. 16. // classes (types) 17. class Helper { (class Helper 18. // only variable declarations... 19. int x; (VARLIST (VAR (TYP int) x) 20. char y; (VAR (TYP char) y) 21. foo[] bar; (VAR (ARRAY foo) bar))) 22. } We can create our tree grammar from this Also look at imaginary tokens in your AST generation