Parsing Techniques. AST Review. AST Data Structures. LL AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum
|
|
- Chrystal Owen
- 6 years ago
- Views:
Transcription
1 Parsing Techniques C41/C413 Introduction to Compilers Tim Teitelbaum Lecture 11: yntax-directed Definitions February 14, 005 LL parsing Computes a Leftmost derivation Determines the derivation top-down LL parsing table indicates which production to use for expanding the leftmost non-terminal LR parsing Computes a Rightmost derivation Determines the derivation bottom-up Uses a set of LR states and a stack of symbols LR parsing table indicates, for each state, what action to perform (shift/reduce) and what state to go to next Use these techniques to construct an AT C 41/413 pring 007 Introduction to Compilers 1 C 41/413 pring 007 Introduction to Compilers AT Review Derivation = sequence of applied productions E E 1 + Parse tree = graph representation of a derivation Doesn t capture the order of applying the productions Abstract yntax Tree (AT) discards unnecessary information from the parse tree Parse Tree E + 1 E + E 3 AT AT Data tructures abstract class Expr { class extends Expr { Expr left, right; (Expr L, Expr R) { left = L; right = R; class Num extends Expr { int value; Num (int v) { value = v; Num 1 Num Num 3 C 41/413 pring 007 Introduction to Compilers 3 C 41/413 pring 007 Introduction to Compilers 4 AT Construction LL/LR parsing techniques implicitly walk the parse tree during parsing LL parsing: Parse tree is implicitly represented by the sequence of applied derivation steps (preorder) LR parsing: Parse tree is implicitly represented by the sequence of applied reductions (endorder) The AT is implicitly defined by the parse tree We want to explicitly construct the AT during parsing: add code in the parser to explicitly build the AT LL AT Construction LL parsing: extend procedures for nonterminals Example: E' ' ε + E num ( ) vo parse_() { switch (token) { case num: case ( : parse_e(); parse_ (); return; default: throw new ParseError(); Expr parse_() { switch (token) { case num: case ( : Expr left = parse_e(); Expr right = parse_ (); if (right == null) return left; else return new (left, right); default: throw new ParseError(); C 41/413 pring 007 Introduction to Compilers 5 C 41/413 pring 007 Introduction to Compilers 6 1
2 LR AT Construction LR parsing We also need to add code for explicit AT construction AT construction mechanism for LR Parsing tore parts of the tree on the stack For each symbol X on stack, also store the AT subtree rooted at X on stack Whenever the parser performs a reduce operation for a production A β, create an AT node for A from AT fragments on stack for constituents of β Example stack LR AT Construction, ctd. + E Before reduction E+ Num() Num(3) Num(1) E+ E num ( ) Num(1) After reduction E+ Num() Num(3) C 41/413 pring 007 Introduction to Compilers 7 C 41/413 pring 007 Introduction to Compilers 8 Issues Unstructured code: mixed parsing code with AT construction code Automatic parser generators The generated parser needs to contain AT construction code How to construct a customized AT data structure using an automatic parser generator? May want to perform other actions concurrently with the parsing phase E.g., semantic checks This can reduce the number of compiler passes yntax-directed Definition olution: syntax-directed definition Extends each grammar production with an associated semantic action (code): E+ { action The parser generator adds these actions into the generated parser Each action is executed when the corresponding production is reduced C 41/413 pring 007 Introduction to Compilers 9 C 41/413 pring 007 Introduction to Compilers 10 emantic Actions Actions = code in a programming language ame language as the automatically generated parser Examples: Yacc = actions written in C CUP = actions written in Java The actions can access the parser stack! Parser generators extend the stack of states (corresponding to RH symbols) symbols with entries for user-defined structures (e.g., parse trees) The action code should be able to refer to the states (corresponding to the RH grammar symbols in the production Need a naming scheme C 41/413 pring 007 Introduction to Compilers 11 Naming cheme Need names for grammar symbols to use in the semantic action code Need to refer to multiple occurrences of the same nonterminal symbol E E 1 + E Distinguish the nonterminal on the LH E 0 E + E C 41/413 pring 007 Introduction to Compilers 1
3 Naming cheme: CUP CUP: Name RH nonterminal occurrences using distinct, user-defined labels: expr ::= expr:e1 PLU expr:e Use keyword REULT for LH nonterminal CUP Example: expr ::= expr:e1 PLU expr:e {: REULT = e1 + e; : Naming cheme: yacc Yacc: Uses keywords: $1 refers to the first RH symbol, $ refers to the second RH symbol, etc. Keyword $$ refers to the LH nonterminal YaccExample: expr ::= expr PLU expr { $$ = $1 + $3; C 41/413 pring 007 Introduction to Compilers 13 C 41/413 pring 007 Introduction to Compilers 14 Building the AT Use semantic actions to build the AT AT is built bottom-up during parsing non terminal Expr expr; User-defined type for semantic objects on the stack Nonterminal name expr ::= NUM:i {: REULT = new Num(i.val); : expr ::= expr:e1 PLU expr:e {: REULT = new (e1,e); : expr ::= expr:e1 MULT expr:e {: REULT = new Mul(e1,e); : expr ::= LPAR expr:e RPAR {: REULT = e; : Example E num (E) E+E E*E Parser stack stores value of each symbol (1+)*3 (1 +)*3 (E Num(1) +)*3 REULT=new Num(1) (E+ )*3 (E+E Num() )*3 REULT=new Num() (E (, ) )*3 REULT=new (e1,e) (E) *3 E *3 REULT=e C 41/413 pring 007 Introduction to Compilers 15 C 41/413 pring 007 Introduction to Compilers 16 AT Design Keep the AT abstract Do not introduce a tree node for every node in parse tree (not very abstract) E + ( ) E E E? 1 3 um Expr um LPar um RPar Expr Expr um 3 1 Expr C 41/413 pring 007 Introduction to Compilers 17 AT Design Do not use one single class AT_node E.g., need information for if, while, +, *, ID, NUM class AT_node { int node_type; AT_node[ ] children; tring name; int value; etc Problem: must have fields for every different kind of node with attributes Not extensible, Java type checking no help C 41/413 pring 007 Introduction to Compilers 18 3
4 Use Class Hierarchy Can use subclassing to solve problem Use an abstract class for each interesting set of non-terminals in grammar (e.g. expressions) E E+E E*E -E (E) abstract class Expr { class extends Expr { Expr left, right; class Mult extends Expr { Expr left, right; // or: class BinExpr extends Expr { Oper o; Expr l, r; class Minus extends Expr { Expr e; Another Example E ::= num (E) E+E ::= E ; if (E) if (E) else = E ; ; abstract class Expr { class Num extends Expr { Num(int value) class extends Expr { (Expr e1, Expr e) class Id extends Expr { Id(tring name) abstract class tmt { class If extends tmt { If(Expr c, tmt s1, tmt s) class Empty extends tmt { Empty() class Assign extends tmt { Assign(tring, Expr e) C 41/413 pring 007 Introduction to Compilers 19 C 41/413 pring 007 Introduction to Compilers 0 Other yntax-directed Definitions Can use syntax-directed definitions to perform semantic checks during parsing E.g., type-checking Benefit = efficiency One single compiler pass for multiple tasks Disadvantage = unstructured code Mixes parsing and semantic checking phases Perform checks while AT is changing Limited to one pass in bottom-up order C 41/413 pring 007 Introduction to Compilers 1 Type Declaration Example D T D D 1, { Type(, T.type); D.type = T.type; { Type(, D 1.type); D.type = D 1.type; T int { T.type = inttype; T float { T.type = floattype; C 41/413 pring 007 Introduction to Compilers Propagation of Values Propagate type attributes while building the AT int a, b T.type D.type D T inttype int D.type D, Type(,D.type) Type(,T.type) D T L Another Example { D.type = T.type; L.type = T.type; T int { T.type = inttype; T float { T.type = floattype; L { Type(,???); L L 1, { Type(, L 1.type);??? C 41/413 pring 007 Introduction to Compilers 3 C 41/413 pring 007 Introduction to Compilers 4 4
5 Propagation of Values Propagate values both bottom-up and top-down int a, b T.type T inttype int LR parsing: AT is built bottom-up! D L.type L.type L L Type(,L.Type), Type(,L.type) tructured Approach eparate AT construction from semantic checking phase Traverse the AT and perform semantic checks (or other actions) only after the tree has been built and its structure is stable This approach is more flexible and less error-prone It is better when efficiency is not a critical issue C 41/413 pring 007 Introduction to Compilers 5 C 41/413 pring 007 Introduction to Compilers 6 Where We Are Visitor Methodology for AT Traversal ource code (character stream) Token stream Abstract syntax tree (AT) if (b == 0) a = b; if ( b == 0 ) a = b ; if == b 0 = a b Lexical Analysis yntax Analysis (Parsing) emantic Analysis Visitor pattern: useful OO programming pattern that separates data structure definition (e.g., the AT) from code that traverses the structure (e.g., the name resolution code and the type checking code). Define a Visitor interface for all traversals of the AT Extend each AT class with a method that accepts any Visitor Code each traversal as a separate class that implements the Visitor interface C 41/413 pring 007 Introduction to Compilers 7 C 41/413 pring 007 Introduction to Compilers 8 AT Data tructure abstract class Expr Expr { class extends Expr Expr { Expr Expre1, e e class Num Numextends Expr Expr { int intvalue class Id Idextends Expr Expr { tring name interface Visitor { vo visit( e); e); vo visit(num e); e); vo visit(id e); e); Visitor Interface C 41/413 pring 007 Introduction to Compilers 9 C 41/413 pring 007 Introduction to Compilers 30 5
6 Accept methods Visitor Methods abstract class classexpr {{ abstract public publicvo voaccept(visitor v); v); class class extends Expr Expr {{ public publicvo voaccept(visitor v) v) {{ v.visit(this); class classnum Numextends Expr Expr {{ public publicvo voaccept(visitor v) v) {{ v.visit(this); class classid Idextends Expr Expr {{ public publicvo voaccept(visitor v) v) { v.visit(this); The declared type of this is the subclass it which it occurs. Overload resolution of v.visit(this); invokes appropriate visit function in the Visitor. For each kind of traversal, implement the Visitor interface, e.g., class PostfixOutputVisitor implements Visitor { vo visit( e) { e.e1.accept(this); e.e.accept(this); ystem.out.print( + ); vo visit(num e) { ystem.out.print(value); vo visit(id e) { ystem.out.print(); Dispatch to e.accept in the visit methods eliminates case analysis on AT subclasses To traverse expression e: PostfixOutputVisitor v = new PostfixOutputVisitor(); e.accept(v); C 41/413 pring 007 Introduction to Compilers 31 C 41/413 pring 007 Introduction to Compilers 3 Inherited and ynthesized Information o far, OK for traversal and action w/o communication of values But we need a way to pass information Down the AT (inherited) Up the AT (synthesized) To pass information down the AT add parameter to visit functions To pass information up the AT add return value to visit functions Visitor Interface () interface Visitor { Object visit( e, e, Object inh); Object visit(num e, e, Object inh); inh); Object visit(id e, e, Object inh); inh); C 41/413 pring 007 Introduction to Compilers 33 C 41/413 pring 007 Introduction to Compilers 34 Accept methods () abstract class classexpr {{ abstract public publicobject Objectaccept(Visitor v, v, Object Object inh); inh); class class extends Expr Expr {{ public publicobject Objectaccept(Visitor v, v, Object Object inh) inh) {{ return returnv.visit(this, inh); inh); class classnum Numextends Expr Expr {{ public publicobject Objectaccept(Visitor v, v, Object Object inh) inh) {{ return returnv.visit(this, inh); inh); class classid Idextends Expr Expr {{ public publicobject Objectaccept(Visitor v, v, Object Object inh) inh) {{ return returnv.visit(this, inh); inh); C 41/413 pring 007 Introduction to Compilers 35 Visitor Methods () For kind of traversal, implement the Visitor interface, e.g., class EvaluationVisitor implements Visitor { Object visit( e, Object inh) { int left = (int) e.e1.accept(this, inh); int right = (int) e.e.accept(this, inh); return left+right; Object visit(num e, Object inh) { return value; Object visit(id e, Object inh) { return Lookup(, (ymboltable)inh); To traverse expression e: EvaluationVisitor v = new EvaluationVisitor (); e.accept(v, EmptyTable()); C 41/413 pring 007 Introduction to Compilers 36 6
7 ummary yntax-directed definitions attach semantic actions to grammar productions Easy to construct the AT using syntax-directed definitions Can use syntax-directed definitions to perform semantic checks eparate AT construction from semantic checks or other actions that traverse the AT C 41/413 pring 007 Introduction to Compilers 37 7
Parsing Techniques. AST Review. AST Data Structures. Implicit AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum
Parsing Techniques C41/C413 Introduction to Compilers Tim Teitelbaum Lecture 11: yntax-irected efinitions February 16, 005 LL parsing Computes a Leftmost derivation Builds the derivation top-down LL parsing
More informationSummary: Semantic Analysis
Summary: Semantic Analysis 1 Basic Concepts When SA is performed: Semantic Analysis may be performed: In a two-pass compiler: after syntactic analysis is finished, the semantic analyser if called with
More informationOutline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction
C12/1 Introduction to Compilers and Translators pring 00 Outline More tips forll1) grammars Bottom-up parsing LR0) parser construction Lecture 5: Bottom-up parsing Lecture 5 C 12/1 pring '00 Andrew Myers
More informationRecursive Descent Parsers
Recursive Descent Parsers Lecture 7 Robb T. Koether Hampden-Sydney College Wed, Jan 28, 2015 Robb T. Koether (Hampden-Sydney College) Recursive Descent Parsers Wed, Jan 28, 2015 1 / 18 1 Parsing 2 LL Parsers
More informationPlan for Today. Regular Expressions: repetition and choice. Syntax and Semantics. Context Free Grammars
Plan for Today Context Free s models for specifying programming languages syntax semantics example grammars derivations Parse trees yntax-directed translation Used syntax-directed translation to interpret
More informationA programming language requires two major definitions A simple one pass compiler
A programming language requires two major definitions A simple one pass compiler [Syntax: what the language looks like A context-free grammar written in BNF (Backus-Naur Form) usually suffices. [Semantics:
More informationSyntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions
Concepts Introduced in Chapter 5 Syntax-Directed Definitions Translation Schemes Synthesized Attributes Inherited Attributes Dependency Graphs Syntax-Directed Translation Uses a grammar to direct the translation.
More informationLR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A Non-LR(0) Grammar. LR(0) Parsing Table CS412/CS413
LR(0) Parsing ummary C412/C41 Introduction to Compilers Tim Teitelbaum Lecture 10: LR Parsing February 12, 2007 LR(0) item = a production with a dot in RH LR(0) state = set of LR(0) items valid for viable
More informationA Simple Syntax-Directed Translator
Chapter 2 A Simple Syntax-Directed Translator 1-1 Introduction The analysis phase of a compiler breaks up a source program into constituent pieces and produces an internal representation for it, called
More informationSyntax-Directed Translation. Lecture 14
Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik) 9/27/2006 Prof. Hilfinger, Lecture 14 1 Motivation: parser as a translator syntax-directed translation stream of tokens parser ASTs,
More informationLecture 14 Sections Mon, Mar 2, 2009
Lecture 14 Sections 5.1-5.4 Hampden-Sydney College Mon, Mar 2, 2009 Outline 1 2 3 4 5 Parse A parse tree shows the grammatical structure of a statement. It includes all of the grammar symbols (terminals
More informationSyntax-Directed Translation. Introduction
Syntax-Directed Translation Introduction Translation of languages guided by context-free grammars Attach attributes to the grammar symbols Values of the attributes are computed by semantic rules associated
More informationCOMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table
COMPILER CONSTRUCTION Lab 2 Symbol table LABS Lab 3 LR parsing and abstract syntax tree construction using ''bison' Lab 4 Semantic analysis (type checking) PHASES OF A COMPILER Source Program Lab 2 Symtab
More informationCS 406: Syntax Directed Translation
CS 406: Syntax Directed Translation Stefan D. Bruda Winter 2015 SYNTAX DIRECTED TRANSLATION Syntax-directed translation the source language translation is completely driven by the parser The parsing process
More informationSemantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413
CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 13: Types and Type-Checking 19 Feb 07 Semantic Analysis Last time: Semantic errors related to scopes Symbol tables Name resolution This lecture:
More informationCS 4120 Introduction to Compilers
CS 4120 Introduction to Compilers Andrew Myers Cornell University Lecture 6: Bottom-Up Parsing 9/9/09 Bottom-up parsing A more powerful parsing technology LR grammars -- more expressive than LL can handle
More informationArchitecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End
Architecture of Compilers, Interpreters : Organization of Programming Languages ource Analyzer Optimizer Code Generator Context Free Grammars Intermediate Representation Front End Back End Compiler / Interpreter
More informationLR Parsing LALR Parser Generators
LR Parsing LALR Parser Generators Outline Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the
More informationCOMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou
COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! Any questions about the syllabus?! Course Material available at www.cs.unic.ac.cy/ioanna! Next time reading assignment [ALSU07]
More informationCOP5621 Exam 3 - Spring 2005
COP5621 Exam 3 - Spring 2005 Name: (Please print) Put the answers on these sheets. Use additional sheets when necessary. Show how you derived your answer when applicable (this is required for full cred
More informationSyntax Directed Translation
Syntax Directed Translation Rupesh Nasre. CS3300 Compiler Design IIT Madras Aug 2015 Character stream Lexical Analyzer Machine-Independent Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax
More informationParsing II Top-down parsing. Comp 412
COMP 412 FALL 2018 Parsing II Top-down parsing Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled
More informationProject Compiler. CS031 TA Help Session November 28, 2011
Project Compiler CS031 TA Help Session November 28, 2011 Motivation Generally, it s easier to program in higher-level languages than in assembly. Our goal is to automate the conversion from a higher-level
More informationSyntax-Directed Translation
Syntax-Directed Translation What is syntax-directed translation? The compilation process is driven by the syntax. The semantic routines perform interpretation based on the syntax structure. Attaching attributes
More informationBottom-Up Parsing. Lecture 11-12
Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 2/20/08 Prof. Hilfinger CS164 Lecture 11 1 Administrivia Test I during class on 10 March. 2/20/08 Prof. Hilfinger CS164 Lecture 11
More informationContext-free grammars
Context-free grammars Section 4.2 Formal way of specifying rules about the structure/syntax of a program terminals - tokens non-terminals - represent higher-level structures of a program start symbol,
More informationSyntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans
Syntax-Directed Translation CS3300 - Compiler Design Syntax Directed Translation V. Krishna Nandivada IIT Madras Attach rules or program fragments to productions in a grammar. Syntax directed definition
More informationEDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:
EDAN65: Compilers, Lecture 06 A LR parsing Görel Hedin Revised: 2017-09-11 This lecture Regular expressions Context-free grammar Attribute grammar Lexical analyzer (scanner) Syntactic analyzer (parser)
More informationMIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology
MIT 6.035 Parse Table Construction Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Parse Tables (Review) ACTION Goto State ( ) $ X s0 shift to s2 error error goto s1
More informationCompiler Construction: Parsing
Compiler Construction: Parsing Mandar Mitra Indian Statistical Institute M. Mitra (ISI) Parsing 1 / 33 Context-free grammars. Reference: Section 4.2 Formal way of specifying rules about the structure/syntax
More informationAs we have seen, token attribute values are supplied via yylval, as in. More on Yacc s value stack
More on Yacc s value stack As we noted last time, Yacc uses a second stack to store the attribute values of the tokens and terminals in the parse stack. For a token, the attributes are computed by the
More informationSyntax-Directed Translation
Syntax-Directed Translation ALSU Textbook Chapter 5.1 5.4, 4.8, 4.9 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 What is syntax-directed translation? Definition: The compilation
More informationMIT Top-Down Parsing. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology
MIT 6.035 Top-Down Parsing Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Orientation Language specification Lexical structure regular expressions Syntactic structure
More informationCS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08
CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 17: Types and Type-Checking 25 Feb 08 CS 412/413 Spring 2008 Introduction to Compilers 1 What Are Types? Types describe the values possibly
More informationCompiler Principle and Technology. Prof. Dongming LU April 15th, 2019
Compiler Principle and Technology Prof. Dongming LU April 15th, 2019 PART TWO 6. Semantic Analysis Contents Part One 6.1 Attributes and Attribute Grammars Part Two 6.2 Algorithms for Attribute Computation
More informationIntroduction to Parsing. Lecture 8
Introduction to Parsing Lecture 8 Adapted from slides by G. Necula Outline Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Languages and Automata Formal languages
More informationParsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)
Parsing Part II (Ambiguity, Top-down parsing, Left-recursion Removal) Ambiguous Grammars Definitions If a grammar has more than one leftmost derivation for a single sentential form, the grammar is ambiguous
More information3. Parsing. Oscar Nierstrasz
3. Parsing Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/ http://www.cs.purdue.edu/homes/hosking/
More informationSection A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.
Section A 1. What do you meant by parser and its types? A parser for grammar G is a program that takes as input a string w and produces as output either a parse tree for w, if w is a sentence of G, or
More informationAbstract Syntax Trees Synthetic and Inherited Attributes
Abstract Syntax Trees Synthetic and Inherited Attributes Lecture 22 Sections 5.1-5.2 Robb T. Koether Hampden-Sydney College Mon, Mar 16, 2015 Robb T. Koether (Hampden-Sydney College)Abstract Syntax TreesSynthetic
More informationIntroduction to Compiler Design
Introduction to Compiler Design Lecture 1 Chapters 1 and 2 Robb T. Koether Hampden-Sydney College Wed, Jan 14, 2015 Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14,
More informationParsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones
Parsing III (Top-down parsing: recursive descent & LL(1) ) (Bottom-up parsing) CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper,
More informationCPS 506 Comparative Programming Languages. Syntax Specification
CPS 506 Comparative Programming Languages Syntax Specification Compiling Process Steps Program Lexical Analysis Convert characters into a stream of tokens Lexical Analysis Syntactic Analysis Send tokens
More informationObject-oriented Compiler Construction
1 Object-oriented Compiler Construction Extended Abstract Axel-Tobias Schreiner, Bernd Kühl University of Osnabrück, Germany {axel,bekuehl}@uos.de, http://www.inf.uos.de/talks/hc2 A compiler takes a program
More informationParsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing
Roadmap > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing The role of the parser > performs context-free syntax analysis > guides
More informationG53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32
G53CMP: Lecture 4 Syntactic Analysis: Parser Generators Henrik Nilsson University of Nottingham, UK G53CMP: Lecture 4 p.1/32 This Lecture Parser generators ( compiler compilers ) The parser generator Happy
More informationLR Parsing LALR Parser Generators
Outline LR Parsing LALR Parser Generators Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the
More informationStructure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.
More detailed overview of compiler front end Structure of a compiler Today we ll take a quick look at typical parts of a compiler. This is to give a feeling for the overall structure. source program lexical
More informationBottom-Up Parsing. Lecture 11-12
Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 9/22/06 Prof. Hilfinger CS164 Lecture 11 1 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient
More informationSyntax-Directed Translation Part I
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011 2 The Structure of our Compiler Revisited Character stream
More informationCSE302: Compiler Design
CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University April 3, 2007 Outline Recap Syntax-directed
More informationLL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012
Predictive Parsers LL(k) Parsing Can we avoid backtracking? es, if for a given input symbol and given nonterminal, we can choose the alternative appropriately. his is possible if the first terminal of
More informationOutline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)
Outline Limitations of regular languages Introduction to Parsing Parser overview Lecture 8 Adapted from slides by G. Necula Context-free grammars (CFG s) Derivations Languages and Automata Formal languages
More informationCS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).
CS 2210 Sample Midterm 1. Determine if each of the following claims is true (T) or false (F). F A language consists of a set of strings, its grammar structure, and a set of operations. (Note: a language
More informationExamples of attributes: values of evaluated subtrees, type information, source file coordinates,
1 2 3 Attributes can be added to the grammar symbols, and program fragments can be added as semantic actions to the grammar, to form a syntax-directed translation scheme. Some attributes may be set by
More informationCompilers. Compiler Construction Tutorial The Front-end
Compilers Compiler Construction Tutorial The Front-end Salahaddin University College of Engineering Software Engineering Department 2011-2012 Amanj Sherwany http://www.amanj.me/wiki/doku.php?id=teaching:su:compilers
More informationChapter 4. Lexical and Syntax Analysis
Chapter 4 Lexical and Syntax Analysis Chapter 4 Topics Introduction Lexical Analysis The Parsing Problem Recursive-Descent Parsing Bottom-Up Parsing Copyright 2012 Addison-Wesley. All rights reserved.
More informationSemantic Analysis Attribute Grammars
Semantic Analysis Attribute Grammars Martin Sulzmann Martin Sulzmann Semantic Analysis Attribute Grammars 1 / 18 Syntax versus Semantics Syntax Analysis When is a program syntactically valid? Formalism:
More informationLR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.
LR Parsing Compiler Design CSE 504 1 Shift-Reduce Parsing 2 LR Parsers 3 SLR and LR(1) Parsers Last modifled: Fri Mar 06 2015 at 13:50:06 EST Version: 1.7 16:58:46 2016/01/29 Compiled at 12:57 on 2016/02/26
More informationPrograms as data Parsing cont d; first-order functional language, type checking
Programs as data Parsing cont d; first-order functional language, type checking Peter Sestoft Monday 2009-09-14 Plan for today Exercises week 2 Parsing: LR versus LL How does an LR parser work Hand-writing
More informationProf. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan
Language Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Semantic Analysis Compiler Architecture Front End Back End Source language Scanner (lexical analysis)
More informationThe Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing
ICOM 4036 Programming Languages Lexical and Syntax Analysis Lexical Analysis The Parsing Problem Recursive-Descent Parsing Bottom-Up Parsing This lecture covers review questions 14-27 This lecture covers
More informationProgramming Languages
Programming Languages Tevfik Koşar Lecture - IX February 14 th, 2006 1 Roadmap Semantic Analysis Role of Semantic Analysis Static vs Dynamic Analysis Attribute Grammars Evaluating Attributes Decoration
More informationBuilding Compilers with Phoenix
Building Compilers with Phoenix Syntax-Directed Translation Structure of a Compiler Character Stream Intermediate Representation Lexical Analyzer Machine-Independent Optimizer token stream Intermediate
More informationLexical and Syntax Analysis. Top-Down Parsing
Lexical and Syntax Analysis Top-Down Parsing Easy for humans to write and understand String of characters Lexemes identified String of tokens Easy for programs to transform Data structure Syntax A syntax
More informationSyntactic Directed Translation
Syntactic Directed Translation Translation Schemes Copyright 2016, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission
More informationSyntax Analysis Part I
Syntax Analysis Part I Chapter 4: Context-Free Grammars Slides adapted from : Robert van Engelen, Florida State University Position of a Parser in the Compiler Model Source Program Lexical Analyzer Token,
More informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 5: Syntax Analysis (Parsing) Zheng (Eddy) Zhang Rutgers University January 31, 2018 Class Information Homework 1 is being graded now. The sample solution
More informationCSE 431S Type Checking. Washington University Spring 2013
CSE 431S Type Checking Washington University Spring 2013 Type Checking When are types checked? Statically at compile time Compiler does type checking during compilation Ideally eliminate runtime checks
More informationCompiler Construction
Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-17/cc/ Recap: LR(1) Parsing LR(1) Items and Sets Observation:
More informationCompilers. Bottom-up Parsing. (original slides by Sam
Compilers Bottom-up Parsing Yannis Smaragdakis U Athens Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Bottom-Up Parsing More general than top-down parsing And just as efficient Builds
More informationRYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS
RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 STUDENT ID: INSTRUCTIONS Please write your student ID on this page. Do not write it or your name
More informationSemantic actions for declarations and expressions
Semantic actions for declarations and expressions Semantic actions Semantic actions are routines called as productions (or parts of productions) are recognized Actions work together to build up intermediate
More informationCompiler Design Concepts. Syntax Analysis
Compiler Design Concepts Syntax Analysis Introduction First task is to break up the text into meaningful words called tokens. newval=oldval+12 id = id + num Token Stream Lexical Analysis Source Code (High
More informationTree visitors. Context classes. CS664 Compiler Theory and Design LIU 1 of 11. Christopher League* 24 February 2016
CS664 Compiler Theory and Design LIU 1 of 11 Tree visitors Christopher League* 24 February 2016 Context classes ANTLR automatically generates heterogeneous representations of the parse trees for your grammar.
More informationCompiler Construction
Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-17/cc/ Recap: LR(1) Parsing Outline of Lecture 11 Recap: LR(1)
More informationTypes of parsing. CMSC 430 Lecture 4, Page 1
Types of parsing Top-down parsers start at the root of derivation tree and fill in picks a production and tries to match the input may require backtracking some grammars are backtrack-free (predictive)
More informationContext-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation
Concepts Introduced in Chapter 2 A more detailed overview of the compilation process. Parsing Scanning Semantic Analysis Syntax-Directed Translation Intermediate Code Generation Context-Free Grammar A
More informationCSCI312 Principles of Programming Languages!
CSCI312 Principles of Programming Languages!! Chapter 3 Regular Expression and Lexer Xu Liu Recap! Copyright 2006 The McGraw-Hill Companies, Inc. Clite: Lexical Syntax! Input: a stream of characters from
More informationIn One Slide. Outline. LR Parsing. Table Construction
LR Parsing Table Construction #1 In One Slide An LR(1) parsing table can be constructed automatically from a CFG. An LR(1) item is a pair made up of a production and a lookahead token; it represents a
More informationCOP4020 Spring 2011 Midterm Exam
COP4020 Spring 2011 Midterm Exam Name: (Please print Put the answers on these sheets. Use additional sheets when necessary or write on the back. Show how you derived your answer (this is required for full
More informationReview of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1
Review of CFGs and Parsing II Bottom-up Parsers Lecture 5 1 Outline Parser Overview op-down Parsers (Covered largely through labs) Bottom-up Parsers 2 he Functionality of the Parser Input: sequence of
More informationA clarification on terminology: Recognizer: accepts or rejects strings in a language. Parser: recognizes and generates parse trees (imminent topic)
A clarification on terminology: Recognizer: accepts or rejects strings in a language Parser: recognizes and generates parse trees (imminent topic) Assignment 3: building a recognizer for the Lake expression
More informationCompilation 2013 Parser Generators, Conflict Management, and ML-Yacc
Compilation 2013 Parser Generators, Conflict Management, and ML-Yacc Erik Ernst Aarhus University Parser generators, ML-Yacc LR parsers are tedious to write, but can be generated, e.g., by ML-Yacc Input:
More information4. Lexical and Syntax Analysis
4. Lexical and Syntax Analysis 4.1 Introduction Language implementation systems must analyze source code, regardless of the specific implementation approach Nearly all syntax analysis is based on a formal
More informationCOMP 181. Prelude. Prelude. Summary of parsing. A Hierarchy of Grammar Classes. More power? Syntax-directed translation. Analysis
Prelude COMP 8 October, 9 What is triskaidekaphobia? Fear of the number s? No aisle in airplanes, no th floor in buildings Fear of Friday the th? Paraskevidedekatriaphobia or friggatriskaidekaphobia Why
More informationProf. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan
Language Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Semantic Analysis Compiler Architecture Front End Back End Source language Scanner (lexical analysis)
More informationMore On Syntax Directed Translation
More On Syntax Directed Translation 1 Types of Attributes We have productions of the form: A X 1 X 2 X 3... X n with semantic rules of the form: b:= f(c 1, c 2, c 3,..., c n ) where b and the c s are attributes
More informationHow do LL(1) Parsers Build Syntax Trees?
How do LL(1) Parsers Build Syntax Trees? So far our LL(1) parser has acted like a recognizer. It verifies that input token are syntactically correct, but it produces no output. Building complete (concrete)
More informationCSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1
CSE P 501 Compilers LR Parsing Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 D-1 Agenda LR Parsing Table-driven Parsers Parser States Shift-Reduce and Reduce-Reduce conflicts UW CSE P 501 Spring 2018
More informationAnatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?
Anatomy of a Compiler Program (character stream) Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analysis Parse Tree Intermediate Code Generator Intermediate Code Optimizer Code Generator
More informationLexical and Syntax Analysis. Bottom-Up Parsing
Lexical and Syntax Analysis Bottom-Up Parsing Parsing There are two ways to construct derivation of a grammar. Top-Down: begin with start symbol; repeatedly replace an instance of a production s LHS with
More informationIntroduction to Parsing. Lecture 5
Introduction to Parsing Lecture 5 1 Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity 2 Languages and Automata Formal languages are very important
More informationOutline. Top Down Parsing. SLL(1) Parsing. Where We Are 1/24/2013
Outline Top Down Parsing Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form Recursive-descent parsing 1 CS 412/413 Spring 2008 Introduction to Compilers 2 Where We Are SLL(1) Parsing
More informationProgramming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson
Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators Jeremy R. Johnson 1 Theme We have now seen how to describe syntax using regular expressions and grammars and how to create
More informationUsing an LALR(1) Parser Generator
Using an LALR(1) Parser Generator Yacc is an LALR(1) parser generator Developed by S.C. Johnson and others at AT&T Bell Labs Yacc is an acronym for Yet another compiler compiler Yacc generates an integrated
More information4. Lexical and Syntax Analysis
4. Lexical and Syntax Analysis 4.1 Introduction Language implementation systems must analyze source code, regardless of the specific implementation approach Nearly all syntax analysis is based on a formal
More informationSyntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412
Midterm Exam: Thursday October 18, 7PM Herzstein Amphitheater Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412 COMP 412 FALL 2018 source code IR Front End Optimizer Back End IR target
More informationMore Assigned Reading and Exercises on Syntax (for Exam 2)
More Assigned Reading and Exercises on Syntax (for Exam 2) 1. Read sections 2.3 (Lexical Syntax) and 2.4 (Context-Free Grammars) on pp. 33 41 of Sethi. 2. Read section 2.6 (Variants of Grammars) on pp.
More informationPOLITECNICO DI TORINO. (01JEUHT) Formal Languages and Compilers. Laboratory N 3. Lab 3. Cup Advanced Use
POLITCNICO DI TORINO (01JUHT) Laboratory N 3 tefano canzio Mail: Web: http://www.skenz.it/compilers 1 Cup Advanced Use Grammars with ambiguities s Operator precedence Handling syntax errors 2 Ambiguous
More information