compiler for flat tiny c

Click here to load reader

Upload: reddydv

Post on 22-Oct-2015

22 views

Category:

Documents


1 download

TRANSCRIPT

Studies in Bounded Query Hierarchies

Topic: Compiler for Flat Tiny C (FLTC)Compilers ProjectThank you. The title of my talk is Studies in Bounded Query Hierarchies. The results I present in this talk are a joint work with my advisor Richard Chang from University of Maryland, Baltimore County.1CompilersA Compiler translates a program in a source language to an equivalent program in a target language

TypicallySource Languages - C, C++, ADA, FORTRAN etc.Target languages - Instruction set of a microprocessorAn assembler translates assemble language programs in to object code.CompilerSource ProgramTarget ProgramGCC

Structure of a Three Phase CompilerAn Optimizer Analyzes IR and rewrites (or transforms) IRPrimary goal is to reduce running time of the compiled codeMay also improve space, power consumption, Must preserve meaning of the codeMeasured by values of named variablesFront EndBack EndSourceProgramTargetALPIRCompilerOptimizerIRprogramLexical Analyzertoken streamSyntax Analyzersyntax treeSemantic Analyzersyntax treeIntermediate Code Generatorintermediate representationMachine-Independent Code Optimizerintermediate representationCode Generatortarget-machine codeMachine-Dependent Code Optimizertarget-machine codeStructure of a Typical CompilerFront EndSymbol TableBack EndInstruction SelectionRegister AllocationInstruction Scheduling

programLexical Analyzer/Scannertoken streamSyntax Analyzer/Parsersyntax treeSemantic Analyzersyntax treeIntermediate Code Generatorintermediate representationSymbol TableThe Front End The Front End: Scanner and ParserTokensScannerParserSourceCodeIRErrorsParserTakes as input a stream of tokensChecks if the stream of tokens constitutes a syntactically valid program of the languageIf the input program is syntactically correctOutput an intermediate representation of the code (like AST)If the input program has syntactic errorsOutputs relevant diagnostic informationContext Free Grammars and Programming LanguagesExpr Expr Binop Expr | Expr | ! Expr | ( Expr )Binop Arithop | Relop | Eqop | CondopArithop + | | * | / | % | > Relop < | > | =Eqop == | !=Condop && | || CFGs and Programming LanguagesStatement Location = Expr ; | MethodCall ; | if ( Expr ) Block | if ( Expr ) Block else Block | while ( Expr ) Block | continue ; | Block Block { VarDeclList StatementList }StatementList Statement | Statement StatementList Context Free Grammars and Programming LanguagesKey Idea: All modern programming languages can be expressed using context free grammars (by design!)Programs have recursive structuresA program is a collection of functionsA function is a sequence of statementsA statement can be any of if, while, for, assignment statements etc.The body of a while loop is a sequence of statementsAn arithmetic expression is a sum/product of two AEs. CFGs are a nice way of expressing programs with recursive structureCFGs and Programming LanguagesAdvantages of using CFGs to specify syntactic structure of languagesClear and concise syntactic specification for languagesLanguage can be developed or evolved iterativelyNew constructs in the language can be added with relative ease. Programming languages can be specified using a special sub-class of CFGs for which efficient parsing techniques and automatic parser generators exists.These special class of CFGs also allow for automatically capturing ambiguities in the languageCFGs impose a structure on the program which facilitates easy translation to intermediate or target object code.Grammar for FTCProgram class main { Field_Decl* Statement* } Field_Decl Type { id | id [ int_literal ] }+, ;Type int | boolean | char Statement Labelled_Statement | Location = Expr ; | if Expr then goto label ; // label is a token like id | goto label ; | Method_Call;Labelled_Statement label Statement //Think of label as id: Here is a brief outline of my talk. First I shall introduce you to some basic complexity classes and explain what Bounded Query Hierarchies are. Followed by that, I shall explain our results in some detail. In general I shall explain what the results are and their significance without proving any theorems. Finally, I shall show you how we can amplify the success probability of ZPP^SAT[1] machines, followed by conclusions. 12Grammar for FTCLocation id | id [ Expr ] Expr Literal | Location | Expr Binop Expr | - Exp | ! Expr | ( Expr ) Binop Arithop | Relop | CondopArithop + | - | * | /Relop < | > | = | == | !=Condop && | ||Method_Call print( Expr +,); | read(Location); Literal int_literal | string_literal | char_literal | bool_literalHere is a brief outline of my talk. First I shall introduce you to some basic complexity classes and explain what Bounded Query Hierarchies are. Followed by that, I shall explain our results in some detail. In general I shall explain what the results are and their significance without proving any theorems. Finally, I shall show you how we can amplify the success probability of ZPP^SAT[1] machines, followed by conclusions. 13Parsing ApproachesCocke-Younger-Kasami (CYK) algorithm can construct a parse tree for a given string and CFG in (n3) worst-case time.Earleys algorithmO(n3) for general CFGsO(n2) for unambiguous grammarsWe would like to have linear-time algorithms for parsing programs.Yacc (Bison)

Structure of a Yacc Specification fileYacc

flex can generate the yylex() function using the lexical specification.Yacc

Abstract Syntax Trees (ASTs)Compilers often use an abstract syntax tree instead of a parse treeThe AST summarizes grammatical structure, without including detail about the derivation

This is much more conciseASTs are one kind of intermediate representation (IR)

x + 2 - yAbstract Syntax TreesWhileexpr subtreestatement subtreeifexpr subtreestatement subtreeif-elseexpr subtreeif-statement subtreeelse-statement subtreeAST Construction for Expression Grammar%{struct { enum Op op; struct astnode *left; struct astnode *right} astnode;#define YYSTYPE struct astnode *;%}%token NUMBER%left - +%left * /%%expr: expr + expr { $$ = getNewAstnode(); $$->op = plus; $$->left = $1; $$->right = $3; } | expr - expr { $$ = getNewAstnode(); $$->op = minus; $$->left = $1; $$->right = $3; } | expr * expr { $$ = getNewAstnode(); $$->op = mult; $$->left = $1; $$->right = $3; } | expr / expr { $$ = getNewAstnode(); $$->op = div; $$->left = $1; $$->right = $3; }Note: This yacc specification is not complete. I highlighted only the important parts.Abstract Syntax TreesWhileexpr subtreestatement subtreeifexpr subtreestatement subtreeif-elseexpr subtreeif-statement subtreeelse-statement subtreeAST Construction

Note: The way statement lists are handled here is different from the code I have shown in the class.

Symbol Tables