managed compiler

Download Managed Compiler

Post on 02-Jul-2015

1.177 views

Category:

Technology

2 download

Embed Size (px)

TRANSCRIPT

  • 1. Language framework in a managed environment Microsoft Development Center CopenhagenAuthor:Joo Filipe Gama de Magalhes E-mail: t-joaode@microsoft.com February 2008

2. Overview

  • Objectives
  • Compiler Structure
  • Compiler Implementation
  • Developed Solution
  • Demo
  • Conclusions
  • Questions

Language framework in a managed environment February 2008 3. Objectives

  • Introduce compiler construction
  • Differences between managed (.NET) compiler and native compiler
  • Introduce some of the used tools
  • Give directions on some of the most important design decisions

Language framework in a managed environment February 2008 4.

  • Constitutes the way to convert an input file (source) into a output (target) file in a process called translation
  • Composed by 4 fundamental parts (lexer, parser, semantic analysis and generation)

Compiler Structure Language framework in a managed environment February 2008 Lexical Analyzer Semantic Analyzer Syntactical Analyzer Code Generator AST sourcefile target file tokens 5. Compiler Implementation

  • Construction limited to managed (.NET) tools
  • Lexer and parser defined in LEX/YACC syntax (reuse old parsers and lexers)
  • C# backed parser, semantic analyzer and code generator
  • Utilization of patterns to simplify work in semantic analysis and code generation

Language framework in a managed environment February 2008 Overview 6. Compiler Implementation

  • Created by Queensland University
  • LEX / FLEX (like) syntax
  • Generates C# code
  • Compatible with GPPG generated parsers
  • Easy to use

Language framework in a managed environment February 2008 GPLEX Scanner Generator I 7. Compiler Implementation Language framework in a managed environment February 2008 GPLEX Scanner Generator II %using gppg; %option minimize %namespace Microsoft.Dynamics.PBvNext.Modelling.ExpressionCompiler /* independent scanner section start */ %x COMMENT %x STRING %x METADATA White0[ fv] White{White0}|CmntStart/* CmntEnd*/ ABStar[^* ]* ABStar2[^"* ]* NONl[^ ]* StrStart" StrEnd" 8. Compiler Implementation Language framework in a managed environment February 2008 GPLEX Scanner Generator III %{ %} if{ return (int)Tokens.KWIF; } switch{ return (int)Tokens.KWSWITCH; } case{ return (int)Tokens.KWCASE; } [_][a-zA-Z0-9_.]+return (int)Tokens.IDENT; } [0-9]+{ return (int)Tokens.NUMBER; } {CmntStart}{ABStar}**{CmntEnd} { return (int)Tokens.LEX_COMMENT; }{CmntStart}{ABStar}**{ BEGIN(COMMENT); return (int)Tokens.LEX_COMMENT; } |{ABStar}**{ return (int)Tokens.LEX_COMMENT; } /* the end of the comment */ {ABStar}**{CmntEnd}{ BEGIN(INITIAL); return (int)Tokens.LEX_COMMENT; } /* all the other cases */ .{ yyerror("illegal char"); return (int)Tokens.LEX_ERROR; } %{ %} 9. Compiler Implementation

  • []alsocreated by Queensland University
  • YACC / Bison (like) syntax
  • []alsogenerates C# code
  • Compatible with GPLEX generated parsers
  • []alsoeasy to use

Language framework in a managed environment February 2008 GPPG Parser Generator I 10. Compiler Implementation Language framework in a managed environment February 2008 GPPG Parser Generator II %using Microsoft.Dynamics.PBvNext.DataStructure.ExpressionAST %namespace Microsoft.Dynamics.PBvNext.Modelling.ExpressionCompiler %valuetype LexValue %partial %union { public string str; public AstNode node; } %{ public RootNode rootNode; %} %token KWIF KWSWITCH KWCASE %token STR CMT META IDENT NUMBER %left BARBAR AMPAMP %left '!' %left NEQ EQ %left '-' '+' 11. Compiler Implementation Language framework in a managed environment February 2008 GPPG Parser Generator III %% E : B { rootNode = new RootNode((ExpressionNode) $1.node);} | { rootNode = new RootNode(); } ; B : B AMPAMP B { $$.node = new BinaryBooleanExpressionNode(BooleanOperationType.AND, (ExpressionNode) $1.node, (ExpressionNode) $3.node); } ; A : A '+' A { $$.node = new BinaryArithmeticExpressionNode(ArithmeticOperationType.PLUS, (ArithmeticExpressionNode) $1.node, (ArithmeticExpressionNode) $3.node); } A : A '+' error {throw new ParsingError(Error reducing literal expression); } ; L : ATTR{ $$.node = new AttributeNode($1.str); } | CONST { $$.node = new ConstantNode(Int32.Parse($1.str)); } | error { throw new ParsingError(Error reducing literal expression); } ; CONST : NUMBER { $$.str = $1.str; } ; ATTR : IDENT{ $$.str = $1.str; } ; 12. Compiler Implementation Language framework in a managed environment February 2008 GPPG and GPLEX // creates a new scanner Scanner scanner =newScanner(); // creates a new parser Parser parser =newParser(); // sets the scanner for the parser parser.scanner = scanner; // sets the stream to parse scanner.buffer =newScanner.StringBuff( this .Value); // parses the file parser.Parse(); // retrieves the output root node this .RootNode = parser.rootNode; 13. Compiler Implementation

  • One of the 23 GOF design patterns
  • Is a technique used to provide a separation between the algorithm and an object structure
  • Uses the concept ofvisitor(algorithm) andvisitable(structure) to implement that separation

Language framework in a managed environment February 2008 Visitor Pattern I 14. Compiler Implementation Language framework in a managed environment February 2008 Visitor Pattern II 15. Compiler Implementation

  • // implementation of the IVisitable (Element) in the class ProductNode
  • public override voidAccept( IVisitorvisitor)
  • {
  • // first visit
  • visitor.Visit( this );
  • if(!visitor.State)
  • return ;
  • foreach( ProductElementNodenodeinproductElementList)
  • {
  • node.Accept(visitor);
  • }
  • // second visit (optional)
  • visitor.Visit( this );
  • }

Language framework in a managed environment February 2008 Visitor Pattern III 16. Compiler Implementation

  • Using visitor to provide the semantic analysis creates a simple yet powerful way of doing it
  • In case the language is complex it might be necessary to use a stack to give some memory [] a symbols table is also useful (hashing)
  • Maybe multiple visits are required (multiple pass)

Language framework in a managed environment February 2008 Semantic Analysis I 17. Compiler Implementation

  • public voidVisit( ProductNodeproductNode)
  • {
  • // tests if the symbol is already defined
  • if(symbols.contains(productNode.ProductValue))
  • throw new DuplicateValueException (productNode);
  • }
  • public voidVisit( VariableNodevariableNode)
  • {
  • // tests if the symbol is already defined in the local table
  • if(localSymbols.contains(variableNode.VariableValue))
  • throw new DuplicateValueException (variableNode);
  • if(variableNode.Array)
  • {
  • // tests if the array size is valid
  • if(variableNode.ArrayIndex < 1)
  • throw new InvalidArrayxception (variableNode);
  • }
  • }

Language framework in a managed environment February 2008 Semantic Analysis II 18. Compiler Implementation

  • Visitor is the perfect choice in case XML is the target generated code
  • A stack is required for layout management (in case the AST is not well organized)
  • Dual visitis required, in order to close the xml nodes

Language framework in a managed environment February 2008 Code Generation I 19. Compiler Implementation

  • public voidVisit( ProductNodeproductNode)
  • {
  • // tests if it is the first visit
  • if(testVisit(productNode))
  • {
  • xmlTextWriter.WriteStartElement( product" );
  • xmlTextWriter.WriteAttributeString( "value" , productNode.ProductValue);
  • ...
  • }
  • // in case it is the second visit
  • else
  • xmlTextWriter.WriteEndElement();
  • }

Language framework in a managed environment February 2008 Code Generation II 20. Compiler Implementation

  • Recursive descent of the XML document structure
  • This method is more hard coded and less flexible than the usage of visitors
  • Memory maybe necessary depending on the XML structure
  • Dual visit is not necessary

Language framework in a managed environment February 2008 XML Interpretation I 21. Compiler Implementation

  • private ProductNodeParseProduct( XmlNodeproductNode)
  • {
  • // creates the product node
  • ProductNodenode =new ProductNode ();
  • // retrieves all the attributes from the node
  • XmlAttributeCollectionxmlAttributeCollection = productNode.Attributes;
  • foreach( XmlAttributeattributeinxmlAttributeCollection)
  • {
  • // gets the name of the attribute