Towards efficient, typed LR parsers
|
|
- Posy Dean
- 5 years ago
- Views:
Transcription
1 roduction An automaton An ML implementation Beyond ML Conclusion 1 rançois Pottier and Yann Régis-Gianas June 2005 rançois Pottier and Yann Régis-Gianas
2 roduction An automaton An ML implementation Beyond ML Conclusion 2 roduction An automaton An ML implementation Beyond ML Conclusion rançois Pottier and Yann Régis-Gianas
3 roduction An automaton An ML implementation Beyond ML Conclusion 3 In short his talk is meant to illustrate how an expressive type system allows guaranteeing the safety of complex programs. he programs considered here are LR parsers and the type system is an extension of ML with generalized algebraic data types GADs). rançois Pottier and Yann Régis-Gianas
4 roduction An automaton An ML implementation Beyond ML Conclusion 4 LR parsers People like to specify a parser as a context-free grammar, typically in BN format, decorated with semantic actions. People like to implement a parser as a deterministic pushdown automaton DPDA). A grammar is LR if such an implementation is possible. rançois Pottier and Yann Régis-Gianas
5 roduction An automaton An ML implementation Beyond ML Conclusion 5 LR parser generators here are tools that generate, out of an LR grammar, a program that simulates execution of the corresponding automaton. Can one guarantee the safety of the generated program without requiring trust in the tool s correctness? rançois Pottier and Yann Régis-Gianas
6 roduction An automaton An ML implementation Beyond ML Conclusion 6 What do existing tools produce? Yacc, Bison, etc. produce C programs, with no safety guarantee. hey use a union to represent semantic values, and do not protect against stack underflow. ML-Yacc or Happy produce ML or Haskell programs, which are typed. Yet, runtime exceptions still arise when pattern matching fails, so safety isn t quite guaranteed. urthermore, redundant dynamic tests incur a runtime penalty. Before showing any code, let s have a look at a sample grammar and automaton. rançois Pottier and Yann Régis-Gianas
7 roduction An automaton An ML implementation Beyond ML Conclusion 7 roduction An automaton An ML implementation Beyond ML Conclusion rançois Pottier and Yann Régis-Gianas
8 roduction An automaton An ML implementation Beyond ML Conclusion 8 A simple grammar Here is a very simple LR grammar, drawn from the Dragon Book: 1) {x} {y} {x y} 2) {x} {x} 3) {x} {y} {x y} 4) {x} {x} 5) {x} ) {x} 6) int{x} {x} he terminals or tokens are,,, ), and int. he non-terminals are,, and. he first four have no semantic value ; the last four have an integer semantic value. rançois Pottier and Yann Régis-Gianas
9 roduction An automaton An ML implementation Beyond ML Conclusion 9 S1 S11 S2 ) S12 S3 S7 S8 S4 S9 S6 S5 S10 <)> :. Here is a pushdown automaton that accepts this grammar. rançois Pottier and Yann Régis-Gianas
10 roduction An automaton An ML implementation Beyond ML Conclusion 10 S1 S11 S2 ) S12 S3 S7 S8 S4 S9 S6 S5 S10 <)> :. Input int ) $ Stack State Next action ɛ S 1 shift S 4 rançois Pottier and Yann Régis-Gianas
11 roduction An automaton An ML implementation Beyond ML Conclusion 11 S1 S11 S2 ) S12 S3 S7 S8 S4 S9 S6 S5 S10 <)> :. Input int ) $ Stack State Next action S 1 S 4 shift S 10 rançois Pottier and Yann Régis-Gianas
12 roduction An automaton An ML implementation Beyond ML Conclusion 12 S1 S11 S2 ) S12 S3 S7 S8 S4 S9 S6 S5 S10 <)> :. Input ) $ Stack S 1 S 4 int State S 10 Next action reduce int, goto rançois Pottier and Yann Régis-Gianas
13 roduction An automaton An ML implementation Beyond ML Conclusion 13 S1 S11 S2 ) S12 S3 S7 S8 S4 S9 S6 S5 S10 <)> :. Input ) $ Stack S 1 S 4 State Next action goto rançois Pottier and Yann Régis-Gianas
14 roduction An automaton An ML implementation Beyond ML Conclusion 14 S1 S11 S2 ) S12 S3 S7 S8 S4 S9 S6 S5 S10 <)> :. Input ) $ Stack S 1 S 4 State S 5 Next action reduce, goto rançois Pottier and Yann Régis-Gianas
15 roduction An automaton An ML implementation Beyond ML Conclusion 15 S1 S11 S2 ) S12 S3 S7 S8 S4 S9 S6 S5 S10 <)> :. Input ) $ Stack S 1 S 4 State S 6 Next action reduce, goto rançois Pottier and Yann Régis-Gianas
16 roduction An automaton An ML implementation Beyond ML Conclusion 16 S1 S11 S2 ) S12 S3 S7 S8 S4 S9 S6 S5 S10 <)> :. Input ) $ Stack State Next action S 1 S 4 S 11 shift S 12 rançois Pottier and Yann Régis-Gianas
17 roduction An automaton An ML implementation Beyond ML Conclusion 17 S1 S11 S2 ) S12 S3 S7 S8 S4 S9 S6 S5 S10 <)> :. Input $ Stack S 1 S 4 S 11 ) State S 12 Next action reduce ), goto rançois Pottier and Yann Régis-Gianas
18 roduction An automaton An ML implementation Beyond ML Conclusion 18 S1 S11 S2 ) S12 S3 S7 S8 S4 S9 S6 S5 S10 <)> :. Input $ Stack S 1 State S 5 Next action reduce, goto rançois Pottier and Yann Régis-Gianas
19 roduction An automaton An ML implementation Beyond ML Conclusion 19 S1 S11 S2 ) S12 S3 S7 S8 S4 S9 S6 S5 S10 <)> :. Input $ Stack S 1 State S 6 Next action reduce, goto rançois Pottier and Yann Régis-Gianas
20 roduction An automaton An ML implementation Beyond ML Conclusion 20 S1 S11 S2 ) S12 S3 S7 S8 S4 S9 S6 S5 S10 <)> :. Input $ Stack S 1 State S 2 Next action accept rançois Pottier and Yann Régis-Gianas
21 roduction An automaton An ML implementation Beyond ML Conclusion 21 roduction An automaton An ML implementation Beyond ML Conclusion rançois Pottier and Yann Régis-Gianas
22 roduction An automaton An ML implementation Beyond ML Conclusion 22 Lexer interface okens are made up of a tag and possibly of a semantic value: type token = KPlus KStar KLeft KRight Knd K of int he lexer provides two functions for looking up and for discarding the current token: val peek : unit token val discard : unit unit rançois Pottier and Yann Régis-Gianas
23 roduction An automaton An ML implementation Beyond ML Conclusion 23 Data structures he type of states is easily defined: type state = S0 S1... S11 rançois Pottier and Yann Régis-Gianas
24 roduction An automaton An ML implementation Beyond ML Conclusion 24 Data structures cont d) he stack is made up of pairs of a state and a semantic value whose type depends on the non-terminal with which it is associated. his is a linked list of tagged cells. type stack = Smpty SPlus of stack state SStar of stack state SLeft of stack state SRight of stack state S of stack state int S of stack state int S of stack state int S of stack state int rançois Pottier and Yann Régis-Gianas
25 roduction An automaton An ML implementation Beyond ML Conclusion 25 Implementation general structure) he automaton is simulated by run. Out of the current state, stack, and implicitly) token stream, this function either produces a semantic value for the entire parse or fails. let rec run s : state) stack : stack) : int = match s, peek) with... shift or reduce transitions ), raise Syntaxrror rançois Pottier and Yann Régis-Gianas
26 roduction An automaton An ML implementation Beyond ML Conclusion 26 Implementation shift) A shift transition pushes the current state and the semantic value for the current token onto the stack, discards the current token, and changes the current state: let rec run s : state) stack : stack) : int = match s, peek) with... S9, KStar shift S 7 ) discard ); run S7 SStar stack, S9))... rançois Pottier and Yann Régis-Gianas
27 roduction An automaton An ML implementation Beyond ML Conclusion 27 Implementation reduce) A reduce transition pops a number of semantic values off the stack and exploits them to compute a new one, which is pushed back onto the stack. let rec run s : state) stack : stack) : int = match s, peek) with... S9, KPlus reduce {x} {y} {x y} ) let S SPlus S stack, s, x ), ),, y) = stack in let stack = S stack, s, x y) in goto s stack goto )... Observe that pattern matching is nonexhaustive. rançois Pottier and Yann Régis-Gianas
28 roduction An automaton An ML implementation Beyond ML Conclusion 28 Implementation end) A goto transition examines the state that was popped off the stack during reduction and changes the current state. and goto s : state) : stack int = match s with S0 run S1 S4 run S8 Again, pattern matching is nonexhaustive. rançois Pottier and Yann Régis-Gianas
29 roduction An automaton An ML implementation Beyond ML Conclusion 29 In short his program is considered well-typed by an ML compiler. Yet, the compiler warns about nonexhaustive pattern matching, which means that the absence of runtime failures is not guaranteed. he problem is to modify the program so that every pattern matching becomes exhaustive. Suppressing redundant dynamic tests will lead to a safety guarantee as well as better efficiency. rançois Pottier and Yann Régis-Gianas
30 roduction An automaton An ML implementation Beyond ML Conclusion 30 roduction An automaton An ML implementation Beyond ML Conclusion rançois Pottier and Yann Régis-Gianas
31 roduction An automaton An ML implementation Beyond ML Conclusion 31 Why are these tests redundant? he dynamic tests performed during the previous reduce transition are redundant because, when the automaton is in state S 9, the stack must be of the form...??? he dynamic tests performed during the previous goto transition are redundant because, when the automaton is in state S 9, the stack must be of the form... S 0 S 4 )????? rançois Pottier and Yann Régis-Gianas
32 roduction An automaton An ML implementation Beyond ML Conclusion 32 he invariant fragment) In fact, one can prove that, when the automaton is in state S 9, the stack must be of the form... S 0 S 4 ) S 1 S 8 ) S 6 More generally, knowledge of the current state determines a suffix of the stack rançois Pottier and Yann Régis-Gianas
33 roduction An automaton An ML implementation Beyond ML Conclusion 33 he full invariant Stack State ɛ S 0 ɛ S 0 S 1... S 0 S 4 ) S 2... S 0 S 4 S 6 ) S 3... S 0 S 4 S 6 S 7 ) S 4... S 0 S 4 S 6 S 7 ) int S 5... S 0 S 4 ) S 1 S 8 ) S 6... S 0 S 4 S 6 ) S 2 S 9 ) S 7... S 0 S 4 S 6 S 7 ) S 4 S 8... S 0 S 4 ) S 1 S 8 ) S 6 S 9... S 0 S 4 S 6 ) S 2 S 9 ) S 7 S S 0 S 4 S 6 S 7 ) S 4 S 8 ) S 11 rançois Pottier and Yann Régis-Gianas
34 roduction An automaton An ML implementation Beyond ML Conclusion 34 owards more precise types It is easy to manually prove, by structural induction over a run of the automaton, that the invariant is sound. or this invariant to be exploited by the compiler, it has to be explicitly provided and mechanically verified. he programming language must come with a type system that is sufficiently expressive to allow encoding the invariant. rançois Pottier and Yann Régis-Gianas
35 roduction An automaton An ML implementation Beyond ML Conclusion 35 he idea On must tell the compiler about the correlation between the current state and the structure of the stack. o this end, one parameterizes the type state with a type variable α. he idea is, if the current state has type α state, then the current stack has type α. rançois Pottier and Yann Régis-Gianas
36 roduction An automaton An ML implementation Beyond ML Conclusion 36 he structure of stacks he type stack disappears. he structure of stacks is defined by a family of parameterized types, which are independent of one another: type empty = Smpty type α cellplus = SPlus of α α state type α cellstar = SStar of α α state type α cellleft = SLeft of α α state type α cellright = SRight of α α state type α cell = S of α α state int type α cell = S of α α state int type α cell = S of α α state int type α cell = S of α α state int Compare to the original definition.) rançois Pottier and Yann Régis-Gianas
37 roduction An automaton An ML implementation Beyond ML Conclusion 37 ncoding the invariant fragment) he fact that, when the automaton is in state S 9, the stack must be of the form...???, is encoded by assigning the data constructor S9 the type and similarly for other states. α.α c cp c state Such a declaration is impossible in ML! he type state is a generalized algebraic data type GAD). rançois Pottier and Yann Régis-Gianas
38 roduction An automaton An ML implementation Beyond ML Conclusion 38 he structure of states type state : where S0 : empty state S1 : empty c state S2 : α.α c state S3 : α.α c state S4 : α.α cl state S5 : α.α ci state S6 : α.α c cp state S7 : α.α c cs state S8 : α.α cl c state S9 : α.α c cp c state S10 : α.α c cs c state S11 : α.α cl c cr state rançois Pottier and Yann Régis-Gianas
39 roduction An automaton An ML implementation Beyond ML Conclusion 39 Implementation general structure) he type of run changes: it now accepts an arbitrary state and a stack whose structure is consistent with respect to that state. let rec run : α.α state α int = fun s stack match s, peek) with..., raise Syntaxrror Compare to the original type.) rançois Pottier and Yann Régis-Gianas
40 roduction An automaton An ML implementation Beyond ML Conclusion 40 Implementation shift) he code for shift transitions is unchanged, but typechecking becomes more subtle. let rec run : α.α state α int = fun s stack match s, peek) with S9, KStar SStar stack, S9) has type α cs ) run S7 has type γ.γ c cs int ) urthermore, α = β c cp c, for an unknown β ) hus α cs = γ c cs, where γ = β c cp ) discard ); run S7 SStar stack, S9)) Consult the definition of the type of states.) rançois Pottier and Yann Régis-Gianas
41 roduction An automaton An ML implementation Beyond ML Conclusion 41 Implementation reduce) he code for reduce transitions is also unchanged, but pattern matching is now exhaustive. let rec run : α.α state α int = fun s stack match s, peek) with S9, KPlus α = β c cp c, for an unknown β ) hus stack : β c cp c ) let S SPlus S stack, s, x ), ),, y) = stack in stack : β, s : β state, x : int, y : int ) let stack = S stack, s, x y) in stack : β c ) goto s stack rançois Pottier and Yann Régis-Gianas
42 roduction An automaton An ML implementation Beyond ML Conclusion 42 Implementation end) he type ascribed to goto states that at the top of the stack is a cell associated with the non-terminal and that the remainder of the stack must be consistent with state s. and goto : α.α state α c int = fun s match s with S0 run S1 S4 run S8 has type β cl c int, for every β ) urthermore, α = β cl, for an unknown β ) run S8 Here, pattern matching remains nonexhaustive.) rançois Pottier and Yann Régis-Gianas
43 roduction An automaton An ML implementation Beyond ML Conclusion 43 In short We have encoded part of the invariant into data type declarations and into the types ascribed to run and goto. In fact, the whole invariant can be encoded. hen, typechecking involves proving the invariant. Pattern matching provides type equations with local scope. Shared type variables allow coordinating data structures. All this is typical of GADs. rançois Pottier and Yann Régis-Gianas
44 roduction An automaton An ML implementation Beyond ML Conclusion 44 roduction An automaton An ML implementation Beyond ML Conclusion rançois Pottier and Yann Régis-Gianas
45 roduction An automaton An ML implementation Beyond ML Conclusion 45 Results We have obtained a safety guarantee about the generated parser, without requiring trust in the generator. he tool that produces the automaton knows the invariant, or thinks it knows, and produces appropriate data type declarations without difficulty. If the tool produces an incorrect program, the latter is rejected by the compiler. rusting the compiler remains necessary, unless of course a certifying compiler is used. rançois Pottier and Yann Régis-Gianas
46 roduction An automaton An ML implementation Beyond ML Conclusion 46 owards more proofs in programs We have exploited a very expressive type system to prove the safety of a program. Proof assistants have allowed this, and more, for a long time. Here, however, we have remained within the framework of a programming language equipped, in particular, with a powerful type inference mechanism and with an extremely efficient compilation scheme. Narrowing the gap between programming and proving is probably a worthy long-term?) research goal. rançois Pottier and Yann Régis-Gianas
47 roduction An automaton An ML implementation Beyond ML Conclusion 47 References Slides, draft paper, and prototype implementations of the typechecker and parser generator are available online: http: // cristal. inria. fr/ ~fpottier/ http: // cristal. inria. fr/ ~regisgia/ rançois Pottier and Yann Régis-Gianas
LR 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 informationAdding GADTs to OCaml the direct approach
Adding GADTs to OCaml the direct approach Jacques Garrigue & Jacques Le Normand Nagoya University / LexiFi (Paris) https://sites.google.com/site/ocamlgadt/ Garrigue & Le Normand Adding GADTs to OCaml 1
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 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 informationLecture Notes on Bottom-Up LR Parsing
Lecture Notes on Bottom-Up LR Parsing 15-411: Compiler Design Frank Pfenning Lecture 9 1 Introduction In this lecture we discuss a second parsing algorithm that traverses the input string from left to
More informationCompiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6
Compiler Design 1 Bottom-UP Parsing Compiler Design 2 The Process The parse tree is built starting from the leaf nodes labeled by the terminals (tokens). The parser tries to discover appropriate reductions,
More informationBottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh.
Bottom-Up Parsing II Different types of Shift-Reduce Conflicts) Lecture 10 Ganesh. Lecture 10) 1 Review: Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient Doesn
More informationCS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1
CS453 : JavaCUP and error recovery CS453 Shift-reduce Parsing 1 Shift-reduce parsing in an LR parser LR(k) parser Left-to-right parse Right-most derivation K-token look ahead LR parsing algorithm using
More informationReview: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk
Review: Shift-Reduce Parsing Bottom-up parsing uses two actions: Bottom-Up Parsing II Lecture 8 Shift ABC xyz ABCx yz Reduce Cbxy ijk CbA ijk Prof. Aiken CS 13 Lecture 8 1 Prof. Aiken CS 13 Lecture 8 2
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 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 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 informationLecture Notes on Bottom-Up LR Parsing
Lecture Notes on Bottom-Up LR Parsing 15-411: Compiler Design Frank Pfenning Lecture 9 September 23, 2009 1 Introduction In this lecture we discuss a second parsing algorithm that traverses the input string
More informationLecture Bottom-Up Parsing
Lecture 14+15 Bottom-Up Parsing CS 241: Foundations of Sequential Programs Winter 2018 Troy Vasiga et al University of Waterloo 1 Example CFG 1. S S 2. S AyB 3. A ab 4. A cd 5. B z 6. B wz 2 Stacks in
More informationCS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG) Objectives Introduce Pushdown Automaton (PDA) Show that PDA = CFG In terms of descriptive power Pushdown Automaton (PDA) Roughly
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 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 informationBottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing
Also known as Shift-Reduce parsing More powerful than top down Don t need left factored grammars Can handle left recursion Attempt to construct parse tree from an input string eginning at leaves and working
More informationLL(1) predictive parsing
LL(1) predictive parsing Informatics 2A: Lecture 11 Mary Cryan School of Informatics University of Edinburgh mcryan@staffmail.ed.ac.uk 10 October 2018 1 / 15 Recap of Lecture 10 A pushdown automaton (PDA)
More informationHiding local state in direct style: a higher-order anti-frame rule
1 / 65 Hiding local state in direct style: a higher-order anti-frame rule François Pottier January 28th, 2008 2 / 65 Contents Introduction Basics of the type system A higher-order anti-frame rule Applications
More informationFormal Languages and Compilers Lecture VII Part 3: Syntactic A
Formal Languages and Compilers Lecture VII Part 3: Syntactic Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
More informationChapter 3: Lexing and Parsing
Chapter 3: Lexing and Parsing Aarne Ranta Slides for the book Implementing Programming Languages. An Introduction to Compilers and Interpreters, College Publications, 2012. Lexing and Parsing* Deeper understanding
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 informationBottom-Up Parsing II. Lecture 8
Bottom-Up Parsing II Lecture 8 1 Review: Shift-Reduce Parsing Bottom-up parsing uses two actions: Shift ABC xyz ABCx yz Reduce Cbxy ijk CbA ijk 2 Recall: he Stack Left string can be implemented by a stack
More informationValidating LR(1) parsers
Validating LR(1) parsers Jacques-Henri Jourdan François Pottier Xavier Leroy INRIA Paris-Rocquencourt, projet Gallium Journées LTP/MTVV, 2011-10-28 Parsing: recap text or token stream abstract syntax tree
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 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 informationLL(1) predictive parsing
LL(1) predictive parsing Informatics 2A: Lecture 11 John Longley School of Informatics University of Edinburgh jrl@staffmail.ed.ac.uk 13 October, 2011 1 / 12 1 LL(1) grammars and parse tables 2 3 2 / 12
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 informationProgramming Languages Third Edition
Programming Languages Third Edition Chapter 12 Formal Semantics Objectives Become familiar with a sample small language for the purpose of semantic specification Understand operational semantics Understand
More informationCSE3322 Programming Languages and Implementation
Monash University School of Computer Science & Software Engineering Exam 2005 CSE3322 Programming Languages and Implementation Total Time Allowed: 3 Hours 1. Reading time is of 10 minutes duration. 2.
More informationExample CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.
Example CFG Lectures 16 & 17 Bottom-Up Parsing CS 241: Foundations of Sequential Programs Fall 2016 1. Sʹ " S 2. S " AyB 3. A " ab 4. A " cd Matt Crane University of Waterloo 5. B " z 6. B " wz 2 LL(1)
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 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 informationLR Parsing. Table Construction
#1 LR Parsing Table Construction #2 Outline Review of bottom-up parsing Computing the parsing DFA Closures, LR(1) Items, States Transitions Using parser generators Handling Conflicts #3 In One Slide An
More informationSemantic actions for declarations and expressions. Monday, September 28, 15
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 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 informationWhere is ML type inference headed?
1 Constraint solving meets local shape inference September 2005 2 Types are good A type is a concise description of the behavior of a program fragment. Typechecking provides safety or security guarantees.
More informationCSE 5317 Midterm Examination 4 March Solutions
CSE 5317 Midterm Examination 4 March 2010 1. / [20 pts] Solutions (parts a j; -1 point for each wrong answer, 0 points for each blank answer, 2 point for each correct answer. Therefore, the score for this
More informationLALR stands for look ahead left right. It is a technique for deciding when reductions have to be made in shift/reduce parsing. Often, it can make the
LALR parsing 1 LALR stands for look ahead left right. It is a technique for deciding when reductions have to be made in shift/reduce parsing. Often, it can make the decisions without using a look ahead.
More informationSemantics of programming languages
Semantics of programming languages Informatics 2A: Lecture 27 John Longley School of Informatics University of Edinburgh jrl@inf.ed.ac.uk 21 November, 2011 1 / 19 1 2 3 4 2 / 19 Semantics for programming
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 informationError Recovery. Computer Science 320 Prof. David Walker - 1 -
Error Recovery Syntax Errors: A Syntax Error occurs when stream of tokens is an invalid string. In LL(k) or LR(k) parsing tables, blank entries refer to syntax erro How should syntax errors be handled?
More informationLR Parsing - The Items
LR Parsing - The Items Lecture 10 Sections 4.5, 4.7 Robb T. Koether Hampden-Sydney College Fri, Feb 13, 2015 Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 1 / 31 1 LR
More informationLR Parsing E T + E T 1 T
LR Parsing 1 Introduction Before reading this quick JFLAP tutorial on parsing please make sure to look at a reference on LL parsing to get an understanding of how the First and Follow sets are defined.
More informationFaculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology
Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4303 April 9, 2010 14.00-15.30 This exam (6 pages) consists of 52 True/False
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 informationMore Bottom-Up Parsing
More Bottom-Up Parsing Lecture 7 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 1 Back By Wednesday (ish) savior lexer in ~cs142/s09/bin Project 2 Due Friday, Apr. 24, 11:55pm My office hours 3pm
More informationLecture Notes on Shift-Reduce Parsing
Lecture Notes on Shift-Reduce Parsing 15-411: Compiler Design Frank Pfenning, Rob Simmons, André Platzer Lecture 8 September 24, 2015 1 Introduction In this lecture we discuss shift-reduce parsing, which
More informationMenhir Reference Manual (version ) INRIA {Francois.Pottier,
Menhir Reference Manual (version 20141215) François Pottier Yann Régis-Gianas INRIA {Francois.Pottier, Yann.Regis-Gianas}@inria.fr Contents 1 Foreword 4 2 Usage 4 3 Lexical conventions 7 4 Syntax of grammar
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 informationFROWN An LALR(k) Parser Generator
FROWN An LALR(k) Parser Generator RALF HINZE Institute of Information and Computing Sciences Utrecht University Email: ralf@cs.uu.nl Homepage: http://www.cs.uu.nl/~ralf/ September, 2001 (Pick the slides
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 informationBottom-Up Parsing LR Parsing
Bottom-Up Parsing LR Parsing Maryam Siahbani 2/19/2016 1 What we need for LR parsing LR0) states: Describe all possible states in which parser can be Parsing table ransition between LR0) states Actions
More informationLecture 8: Deterministic Bottom-Up Parsing
Lecture 8: Deterministic Bottom-Up Parsing (From slides by G. Necula & R. Bodik) Last modified: Fri Feb 12 13:02:57 2010 CS164: Lecture #8 1 Avoiding nondeterministic choice: LR We ve been looking at general
More informationComputing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.
COMP 412 FALL 2017 Computing Inside The Parser Syntax-Directed Translation Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights
More informationCSCE 531 Spring 2009 Final Exam
CSCE 531 Spring 2009 Final Exam Do all problems. Write your solutions on the paper provided. This test is open book, open notes, but no electronic devices. For your own sake, please read all problems before
More informationSEMANTIC ANALYSIS TYPES AND DECLARATIONS
SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether
More informationLecture 7: Deterministic Bottom-Up Parsing
Lecture 7: Deterministic Bottom-Up Parsing (From slides by G. Necula & R. Bodik) Last modified: Tue Sep 20 12:50:42 2011 CS164: Lecture #7 1 Avoiding nondeterministic choice: LR We ve been looking at general
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 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 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 informationCS 415 Midterm Exam Spring 2002
CS 415 Midterm Exam Spring 2002 Name KEY Email Address Student ID # Pledge: This exam is closed note, closed book. Good Luck! Score Fortran Algol 60 Compilation Names, Bindings, Scope Functional Programming
More informationVerifying Program Invariants with Refinement Types
Verifying Program Invariants with Refinement Types Rowan Davies and Frank Pfenning Carnegie Mellon University Princeton and Yale Colloquium Talks February, 2001 Acknowledgments: Robert Harper 1 Overview
More informationCSCI-GA Scripting Languages
CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation
More informationOutline. The strategy: shift-reduce parsing. Introduction to Bottom-Up Parsing. A key concept: handles
Outline Introduction to Bottom-Up Parsing Lecture Notes by Profs. Alex Aiken and George Necula (UCB) he strategy: -reduce parsing A key concept: handles Ambiguity and precedence declarations CS780(Prasad)
More informationOutline. Lecture 17: Putting it all together. Example (input program) How to make the computer understand? Example (Output assembly code) Fall 2002
Outline 5 Fall 2002 Lecture 17: Putting it all together From parsing to code generation Saman Amarasinghe 2 6.035 MIT Fall 1998 How to make the computer understand? Write a program using a programming
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 informationn Parsing function takes a lexing function n Returns semantic attribute of 10/27/16 2 n Contains arbitrary Ocaml code n May be omitted 10/27/16 4
Parser Code Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http://courses.engr.illinois.edu/cs421 Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul
More informationSyntax. 2.1 Terminology
Syntax 2 Once you ve learned to program in one language, learning a similar programming language isn t all that hard. But, understanding just how to write in the new language takes looking at examples
More informationSyntax Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay
Syntax Analysis (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay September 2007 College of Engineering, Pune Syntax Analysis: 2/124 Syntax
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 informationCompilers and computer architecture From strings to ASTs (2): context free grammars
1 / 1 Compilers and computer architecture From strings to ASTs (2): context free grammars Martin Berger October 2018 Recall the function of compilers 2 / 1 3 / 1 Recall we are discussing parsing Source
More informationCOS 320. Compiling Techniques
Topic 5: Types COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Types: potential benefits (I) 2 For programmers: help to eliminate common programming mistakes, particularly
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 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 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 informationLL Parsing, LR Parsing, Complexity, and Automata
LL Parsing, LR Parsing, Complexity, and Automata R. Gregory Taylor Department of Mathematics and Computer Science Manhattan College Riverdale, New York 10471-4098 USA Abstract It
More informationIntroduction to Bottom-Up Parsing
Introduction to Bottom-Up Parsing Lecture 11 CS 536 Spring 2001 1 Outline he strategy: shift-reduce parsing Ambiguity and precedence declarations Next lecture: bottom-up parsing algorithms CS 536 Spring
More informationLet us construct the LR(1) items for the grammar given below to construct the LALR parsing table.
MODULE 18 LALR parsing After understanding the most powerful CALR parser, in this module we will learn to construct the LALR parser. The CALR parser has a large set of items and hence the LALR parser is
More informationVisitors Unchained. Using visitors to traverse abstract syntax with binding. François Pottier. WG 2.8, Edinburgh June 15, 2017
Visitors Unchained Using visitors to traverse abstract syntax with binding François Pottier WG 2.8, Edinburgh June 15, 2017 The problem Manipulating abstract syntax with binding requires many standard
More informationFaculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology
Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4020 July 5, 2007 14.00-15.30 This exam (8 pages) consists of 60 True/False
More informationCompiler Theory. (Semantic Analysis and Run-Time Environments)
Compiler Theory (Semantic Analysis and Run-Time Environments) 005 Semantic Actions A compiler must do more than recognise whether a sentence belongs to the language of a grammar it must do something useful
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 informationIntroduction to Parsing Ambiguity and Syntax Errors
Introduction to Parsing Ambiguity and Syntax rrors Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity Syntax errors Compiler Design 1 (2011) 2 Languages
More informationParser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root
Parser Generation Main Problem: given a grammar G, how to build a top-down parser or a bottom-up parser for it? parser : a program that, given a sentence, reconstructs a derivation for that sentence ----
More informationError Handling Syntax-Directed Translation Recursive Descent Parsing
Error Handling Syntax-Directed Translation Recursive Descent Parsing Lecture 6 by Professor Vijay Ganesh) 1 Outline Recursive descent Extensions of CFG for parsing Precedence declarations Error handling
More informationCS 4110 Programming Languages & Logics. Lecture 35 Typed Assembly Language
CS 4110 Programming Languages & Logics Lecture 35 Typed Assembly Language 1 December 2014 Announcements 2 Foster Office Hours today 4-5pm Optional Homework 10 out Final practice problems out this week
More informationParsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468
Parsers Xiaokang Qiu Purdue University ECE 468 August 31, 2018 What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure
More informationLR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States
TDDD16 Compilers and Interpreters TDDB44 Compiler Construction LR Parsing, Part 2 Constructing Parse Tables Parse table construction Grammar conflict handling Categories of LR Grammars and Parsers An NFA
More informationSoftware II: Principles of Programming Languages
Software II: Principles of Programming Languages Lecture 4 Language Translation: Lexical and Syntactic Analysis Translation A translator transforms source code (a program written in one language) into
More informationIntroduction to Parsing Ambiguity and Syntax Errors
Introduction to Parsing Ambiguity and Syntax rrors Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity Syntax errors 2 Languages and Automata Formal
More informationVIVA QUESTIONS WITH ANSWERS
VIVA QUESTIONS WITH ANSWERS 1. What is a compiler? A compiler is a program that reads a program written in one language the source language and translates it into an equivalent program in another language-the
More informationThe role of semantic analysis in a compiler
Semantic Analysis Outline The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors
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 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 informationError Handling Syntax-Directed Translation Recursive Descent Parsing
Announcements rror Handling Syntax-Directed ranslation Lecture 6 PA1 & WA1 Due today at midnight PA2 & WA2 Assigned today Prof. Aiken CS 143 Lecture 6 1 Prof. Aiken CS 143 Lecture 6 2 Outline xtensions
More informationReview main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers
Plan for Today Review main idea syntax-directed evaluation and translation Recall syntax-directed interpretation in recursive descent parsers Syntax-directed evaluation and translation in shift-reduce
More informationSemantics of programming languages
Semantics of programming languages Informatics 2A: Lecture 28 Mary Cryan School of Informatics University of Edinburgh mcryan@inf.ed.ac.uk 21 November 2018 1 / 18 Two parallel pipelines A large proportion
More informationRYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 97 INSTRUCTIONS
RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 97 STUDENT ID: INSTRUCTIONS Please write your student ID on this page. Do not write it or your name
More information