Constructing Precedence Table

Slides:



Advertisements
Similar presentations
CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Advertisements

Chapter 2-2 A Simple One-Pass Compiler
CPSC 388 – Compiler Design and Construction
Semantic Analysis and Symbol Tables
Intermediate Code Generation
1 Compiler Construction Intermediate Code Generation.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Compiler Summary Mooly Sagiv html://
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
CPS 506 Comparative Programming Languages Syntax Specification.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Overview of Previous Lesson(s) Over View  In syntax-directed translation 1 st we construct a parse tree or a syntax tree then compute the values of.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
7. Symbol Table Chih-Hung Wang Compilers References 1. C. N. Fischer and R. J. LeBlanc. Crafting a Compiler with C. Pearson Education Inc., D.
Lecture 12 Intermediate Code Generation Translating Expressions
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 9 Symbol Table and Attributed Grammars
Semantic analysis Jakub Yaghob
Compiler Design (40-414) Main Text Book:
Parsing #1 Leonidas Fegaras.
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
8. Symbol Table Chih-Hung Wang
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Lecture 1 CS510.
CS416 Compiler Design lec00-outline September 19, 2018
CS 3304 Comparative Languages
Course supervisor: Lubna Siddiqui
Compiler Design 4. Language Grammars
Introduction CI612 Compiler Design CI612 Compiler Design.
CSE 3302 Programming Languages
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CMPE 152: Compiler Design October 4 Class Meeting
Compilers B V Sai Aravind (11CS10008).
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
CS416 Compiler Design lec00-outline February 23, 2019
SYNTAX DIRECTED DEFINITION
Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Compiler Construction
Presentation transcript:

Constructing Precedence Table Compiler Design Lecture 8 Syntax Analysis (6) Constructing Precedence Table and Syntax Tree

Operator-Precedence Parsing: Example

Construction of Precedence Table (1/5) 1) Using precedence of operators: For binary operators:

Construction of Precedence Table (1’/5) 1) Using precedence of operators: For unary operators: If opi is unary prefix operator then For all other operators opj If opi has higher precedence than opj then else Example: If ! is a unary operator and & is binary operator, and ! Has higher precedence than & then a & ! b => a & (! b) ! &

Construction of Precedence Table (2/5) 2) Using Associativity of operators:

Construction of Precedence Table (3/5) 3) Identifiers:

Construction of Precedence Table (4/5) (x * (y + z) (X * W)) 4) Parentheses:

Construction of Precedence Table (5/5) 5) End Marker $:

Error Sources in Operator-Precedence Parsing When a blank cell is found for the topmost stack terminal and current input terminal If the detected handle does not match any RHS Example: the input $()+id$ Error since the handle () is not found as RHS of any production Stack Operator Input $ < ()+id$ $<( = )+id$ $<() > +id$

Combining Parsing Methods Top-down and bottom-up parsers can be used in one compiler for the same language. Use top-down for the major constructs of the language Use bottom-up for expressions Example: S ::= if E then S else S | while E do S | for (E;E;E) loop S | id = E E ::= E + E | …. Use top-down for the whole grammar until E is found in the stack, then Call bottom-up parser for E until input tokens for E is finished How to know end of E in the input tokens? loop <w ) loop …. stack input

Combining Parsing Methods (cont) First idea: until t  FOLLOW (E) is found Such as {then, do, ;} But: )  FOLLOW(E) and one E terminals also Any ideas?

Abstract Syntax Tree Parse tree keeps a lot of parentheses and keywords for grouping purpose only Many grammar nonterminals are only used to: eliminate ambiguity Rewrite the grammar to be suitable for some parser A parser typically generates an Abstract Syntax Tree (AST) or Syntax Tree for short scanner parser get token token source file get next character AST

Abstract Syntax Tree (cont) In a syntax tree, operators and keywords don’t appear in leaves as in parse tree, but appear as interior nodes E T + E F T * E id(x) F T id(y) F id(z) + * Id(x) Id(y) Id(z)

AST Example S ::= if E then S else S If-then-else S E S S

Symbol Table and Attributed Grammars

Type Checking scanner parser get token token source file get next character AST type checking symbol table type errors Checking whether the use of names is consistent with their declaration in the program int x; x := x+1; correct use of x x.A := 1; x[0] := 0; type errors Statically typed languages: done at compile time, not at run time Need to remember declarations Symbol Table

Symbol Table A compile-time data structure used to map names into declarations It stores: for each type name, its type definition eg. for the C type declaration typedef int* mytype, it maps the name mytype to a data structure that represents the type int* for each variable name, its type if the variable is an array, it also stores dimension information for each constant name, its type and value for each function and procedure, its formal parameter list and its output type each formal parameter must have name type type of passing (by-reference, by-value, etc)

Using Symbol Tables Collect information about names (identifiers) incrementally during analysis phases Lexical analyzer Create an entry as soon as it sees the characters that make up a lexeme. Parser Decides whether to use a previously created entry or create a new one for the identifier Semantic analyzer Later …. Used by the synthesis phases to generate the target code

Translation of an assignment statement

Identifier Scope Scope Most languages allow nested blocks portion of a program that is the scope of one or more declarations Most languages allow nested blocks The “most-closely nested” rule for blocks an identifier x is in the scope of the most-closely nested declaration of x examine blocks inside-out, starting with the block in which x appears

Symbol Table per Scope Chained Symbol Table int w; { int x; int y; { int w; bool y; int z; w ; x ; y ; z ; } w ; x ; y ; w ; Chained Symbol Table

Semantic Analysis Flow of control checks Uniqueness check: e.g. break must be within while, for, or switch Uniqueness check: Identifier names must be unique within the same scope Switch cases must be distinct Enumerated names must be distinct Name-related checks Some names must appears at begin and end of blocks: function xyz begin …….. End xyz Type Checking A very important part in semantic analysis …. More later on

Attributes Some compiler phases collect information about identifiers and other phases use these information Where to store such information? As attributes in parse (or syntax) tree nodes Two types of attributes: Synthesized attributes passed upwards from the leaves up to the root Inherited attributes passed downwards from root to leaves

Attributes An attribute is said to be synthesized if its value at a parse-tree node N is determined from attribute values at the children of N and at N itself. The "inherited" attributes have their value at a parse-tree node determined from attribute values at the node itself, its parent, and its siblings in the parse tree.

Syntax-directed translation Syntax-directed translation engines that produce collections of routines for walking a parse tree and generating intermediate code.

Syntax-Directed Definition A syntax-directed definition (SDD) is a context-free grammar together with, attributes and set of semantic rules (for computing the values of the attributes). Attributes are associated with grammar symbols and rules are associated with productions. If X is a symbol and a is one of its attributes, then we write X.a to denote the value of a at a particular parse-tree node labeled X. If we implement the nodes of the parse tree by records or objects, then the attributes of X can be implemented by data fields in the records that represent the nodes for X.

Syntax-Directed Definition A syntax-directed definition associates: With each grammar symbol, a set of attributes, and With each production, a set of semantic rules for computing the values of the attributes associated with the symbols appearing in the production

SDD Example Syntax-directed definition for infix to postfix translation The symbol || in the semantic rule is the operator for string concatenation

Synthesized Attribute Example Parse tree for 9-5+2 with attribute t annotated parse tree for 9-5+2 A parse tree, showing the value(s) of its attribute(s) is called an annotated parse tree.

Tree Traversal A traversal of a tree starts at the root and visits each node of the tree in some order Depth-first Breadth-first

Type Checking Correct use of operators x + y check that both x and y are of equivalent types accepted by addition Integers Floats Integer and float Array subscript it used only with arrays Subscript index is integer A[2.5]  subscript error Int A; A[2];  Error: A is not an array Pointer operator is used only with pointer variables etc Correct function parameter passing The same number of arguments are passed to the function with compatible types

Type Expressions Represents the structure of types Basic types, or Formed by applying type constructor to a type expression Example: int a[2][3] array(2, array(3, integer))

Type Expressions (cont) A basic type is a type expression boolean, char, integer, float, and void A type name is a type expression If t is type expression then array(size, t) is a type expression If s and t are type expressions then s  t is a type expression for functions s x t (their Cartesian product) is type expression e.g. for function parameters int f(int, float, char) int x float x char  int Int f() void  int void f(int) int  void record is a type constructor applied to record (struct) field names and their types  the type constructor for function types. Array record

Declarations Simple grammar for declarations

References Compilers: Principles, Techniques and Tools, Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman. 2nd Edition, Addison-Wesley, 2007 (sections: 2.3, 2.7, 6.3) Basics of Compiler Design, Torben Ægidius Mogensen. Published through lulu.com, 2008 Course notes of Leonidas Fegaras, University of Texas at Arlington, CSE, 2005

References Compilers: Principles, Techniques and Tools, Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman. 1st Edition, Addison-Wesley, 1986 (pages 203-208) Teaching Materials of: Elements of Compiler Design, Alexander Meduna ,Taylor & Francis, New York, 2007 Course notes of Leonidas Fegaras, University of Texas at Arlington, CSE, 2005