Compiler Construction

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Yacc Examples Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Syntax Directed Definitions Synthesized Attributes
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
LEX and YACC work as a team
Syntax-Directed Translation
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 YACC Parser Generator. 2 YACC YACC (Yet Another Compiler Compiler) Produce a parser for a given grammar.  Compile a LALR(1) grammar Original written.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
國立台灣大學 資訊工程學系 薛智文 98 Spring Syntax-Directed Translation (textbook ch#5.1–5.6, 4.8, 4.9 )
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
LECTURE 11 Semantic Analysis and Yacc. REVIEW OF LAST LECTURE In the last lecture, we introduced the basic idea behind semantic analysis. Instead of merely.
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
YACC (Yet Another Compiler-Compiler) Chung-Ju Wu
1 Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
9-December-2002cse Tools © 2002 University of Washington1 Lexical and Parser Tools CSE 413, Autumn 2002 Programming Languages
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
Lecture 9 Symbol Table and Attributed Grammars
Semantic analysis Jakub Yaghob
Semantics Analysis.
Syntax Analysis Part III
Syntax-Directed Translation
A Simple Syntax-Directed Translator
Chapter 5 Syntax Directed Translation
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Context-free Languages
Compiler Lecture 1 CS510.
Syntax Analysis Part III
Bison: Parser Generator
Syntax-Directed Translation Part I
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
SYNTAX DIRECTED TRANSLATION
פרק 5 תרגום מונחה תחביר תורת הקומפילציה איתן אביאור.
Syntax Analysis Part III
Syntax Analysis Part III
Syntax Analysis Part III
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Subject: Language Processor
Chapter 4 Action Routines.
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax-Directed Translation
Syntax Analysis - 3 Chapter 4.
Compiler Lecture Note, Miscellaneous
Compiler Structures 7. Yacc Objectives , Semester 2,
Topic 2: Compiler Front-End
Compiler Design Yacc Example "Yet Another Compiler Compiler"
Syntax-Directed Translation Part I
Chapter 5 Syntax Directed Translation
CMPE 152: Compiler Design December 4 Class Meeting
Presentation transcript:

Compiler Construction Chapter 6 Compiler Construction Dr K. V. N. Sunitha

Syntax Directed Translation Syntax directed definition (SDD) is a high-level language specification for translation. Syntax directed translation (SDT) describes translation of language constructs guided by context free grammars. The grammar together with semantic action is called syntax-directed translation They are used in automatic tools like YACC. SDT helps the compiler designer to translate the language constructs directly by attaching semantic actions or subroutines. Aα + {action} = SDT Compiler Construction Dr K. V. N. Sunitha

Attributes for Grammar Symbols Synthesized attribute – The value of a synthesized attribute at a node is computed from the values of attributes at the children of that node in the parse tree. Inherited attribute – The value of an inherited attribute is computed from the values of attributes at the siblings and parent of that node. Compiler Construction Dr K. V. N. Sunitha

Writing Syntax Directed Translation Involves three steps as follows: Define grammar for input string. Take a simple string and draw parse tree. Attach semantic actions by looking at expected output. Compiler Construction Dr K. V. N. Sunitha

SDT for Evaluation of Expressions / Desk Calculator E → E + T { E•val = E•val + T•val } E → T { E•val = T•val } T → T * F { T•val = T•val * F•val } T → F { T•val = F•val } F → id { F•val = num•lval } Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Annotated Parse Tree Compiler Construction Dr K. V. N. Sunitha

SDT for Converting Infix to Postfix Expression E → E + T { print(‘+’); } E → T { } T → T * F { print(‘*’); } T → F { } F → id { print(“id•name”); } Compiler Construction Dr K. V. N. Sunitha

Creation of Syntax Tree E → E + T { E•nptr = mknode ( E•nptr , ‘+’ , T•nptr ) ; } E → T { E•nptr = T•nptr; } T → T * F { T•nptr = mknode ( T•nptr , ‘*’ , F•nptr ) ; } T → F { T•nptr = F•nptr; } F → id { F•nptr = mknode ( NULL, id•lvalue , NULL ) ; } Compiler Construction Dr K. V. N. Sunitha

S-Attributed Definition It uses only synthesized attributes only. Semantic actions can be placed only at the end of right hand side of a production. Attributes are generally evaluated during bottom up parsing. Compiler Construction Dr K. V. N. Sunitha

L-Attributed Definition It allows both types. But if an inherited attribute is present there is a restriction. The restriction is – each inherited attribute is restricted to inherit either from parent or left sibling only. Translation rules can be placed anywhere on right hand side of the production. Attributes are generally evaluated by traversing the parse tree depth first and left to right. Compiler Construction Dr K. V. N. Sunitha

SDT for Storing Type Information in Symbol Table D → TL {L•type = T•type ; } T → int { T•type = int } T → char { T•type = char } L → L1 , id { L1•in = L•in ; add_type(L1•in , id) ; } L → id { add_type(L•in , id•name) ; } Compiler Construction Dr K. V. N. Sunitha

Converting L-Attributed to S-Attributed Definition D → D1 , id {D•type = D1•type ; add_type(D1•type , id•name) ; } D → T , id {L•type = T•type ; add_type(T•type , id•name) } T → int { T•type = int } T → char { T•type = char } Compiler Construction Dr K. V. N. Sunitha

Compiler Construction YACC Compiler Construction Dr K. V. N. Sunitha

Compiler Construction What Is YACC ? Automatic tool to build a parser for a given grammar. YACC (Yet Another Compiler Compiler) implements LALR(1) technique. Input is a CFG and actions to take upon recognizing a rule, i.e., SDT. Output is a parser in C. Compiler Construction Dr K. V. N. Sunitha

Compiler Construction LEX and YACC: A Team call yylex() [0-9]+ next token is NUM NUM ‘+’ NUM Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Availability lex, yacc on most UNIX systems bison: a yacc replacement from GNU flex: fast lexical analyzer BSD yacc Windows/MS-DOS versions exist Compiler Construction Dr K. V. N. Sunitha

YACC Basic Operational Sequence gram.y File containing desired grammar in YACC format YACC program yacc y.tab.c C source program created by YACC cc or gcc C compiler a.out Executable program that will parse grammar given in gram.y Compiler Construction Dr K. V. N. Sunitha

YACC – A Parser Generator A language for specifying parsers and semantic analyzers lang.y Yacc compiler y.tab.c y.tab.c C compiler a.out tokens a.out syntax tree Compiler Construction Dr K. V. N. Sunitha

Compiler Construction YACC Programs %{ C declarations %} Yacc declarations %% Grammar rules Additional C code Compiler Construction Dr K. V. N. Sunitha

YACC Program for Evaluation of Expressions Input: 2+3*4 Output: 14 Compiler Construction Dr K. V. N. Sunitha

YACC Program for Evaluation of Expressions %{ #include<stdio.h> %} %token NUM %% L:L E '\n' {printf("value of expr=%d\n",$2);} |; E:E'+'T {$$=$1+$3;} | T ; T:T'*' F {$$=$1*$3;} |F F:NUM {$$=$1;} Compiler Construction Dr K. V. N. Sunitha

Compiler Construction main() { yyparse(); } void yyerror(char *s) {printf("error\n"); yylex() char c; while((c=getchar())!='\n') if(isdigit(c)) yylval=c-'0'; return NUM; else return c; } return c; Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Input: 2+3*4 Output: 14 $yacc –v x.y gives y.output FSM Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Lex Program %% [ \t] { } [0-9]+ {sscanf(yytext,"%d",&yylval); return num;} [\n] {return(yytext[0]);} [+*-/] {return(yytext[0]);} yywrap() { return 1; } Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Parsing Parsers are already being used extensively for compiler construction, in database interfaces, in self-describing databases, in linguistics for text analysis, corpora analysis, machine translation, text analysis of b, in document preparation and conversion Compiler Construction Dr K. V. N. Sunitha