Grammars and Parsing for SSC1

Total Page:16

File Type:pdf, Size:1020Kb

Grammars and Parsing for SSC1 Grammars and Parsing for SSC1 Hayo Thielecke Introduction Grammars and Parsing for SSC1 Grammars Derivations Parse trees abstractly From Hayo Thielecke grammars to code Translation to Java methods Parse trees in Java November 2008 Parser generators Yacc, ANTLR, JavaCC and SableCC Summary Outline of the parsing part of the module Grammars and Parsing for 1 Introduction SSC1 Hayo Thielecke 2 Grammars Derivations Introduction Grammars Parse trees abstractly Derivations Parse trees abstractly 3 From grammars to code From grammars to Translation to Java methods code Translation to Parse trees in Java Java methods Parse trees in Java 4 Parser generators Parser generators Yacc, ANTLR, JavaCC and SableCC Yacc, ANTLR, JavaCC and SableCC 5 Summary Summary Motivation: a challenge Grammars and Parsing for SSC1 Hayo Write a program that reads strings like this and evaluates Thielecke them: Introduction (2+3*(2-3-4))*2 Grammars Derivations In particular, brackets and precedence must be handled Parse trees abstractly correctly (* binds more tightly than +). From grammars to If you attempt brute-force hacking, you may end up with code Translation to something that is inefficient, incorrect, or both. |Try it! Java methods Parse trees in With parsing, that sort of problem is straighforward. Java Parser Moreover, the techniques scale up to more realistic generators Yacc, ANTLR, JavaCC and problems. SableCC Summary Books Grammars and Parsing for SSC1 Hayo I hope these slides are detailed enough, but if you want to dig Thielecke deeper: Introduction There are lots of book on compilers. Grammars The ones which I know best are: Derivations Parse trees Appel, Modern Compiler Design in Java. abstractly From grammars to Aho, Sethi, and Ullman, nicknamed \The Dragon Book" code Translation to Parsing is covered in both, but the Composite Pattern for trees Java methods Parse trees in and Visitors for tree walking only in Appel. Java Parser See also the websites for ANTLR (http://antlr.org) and generators Yacc, ANTLR, SableCC (http://sablecc.org). JavaCC and SableCC Summary Why do you need to learn about grammars? Grammars and Parsing for Grammars are widespread in programming. SSC1 Hayo XML is based on grammars and needs to be parsed. Thielecke Knowing grammars makes it much easier to learn the Introduction syntax of a new programming language. Grammars Derivations Parse trees Powerful tools exists for parsing (e.g., yacc, bison, abstractly ANTLR, SableCC, . ). But you have to understand From grammars to grammars to use them. code Translation to Java methods Grammars give us examples of some more advanced Parse trees in Java object-oriented programming: Composite Pattern and Parser polymorphic methods. generators Yacc, ANTLR, JavaCC and You may need parsing in your final-year project for reading SableCC complex input. Summary Describing syntax Grammars and Parsing for SSC1 Hayo One often has to describe syntax precisely, particularly Thielecke when dealing with computers. Introduction \A block consists of a sequence of statements enclosed in Grammars Derivations curly brackets " Parse trees abstractly Informal English descriptions are too clumsy and not From grammars to precise enough. Rather, we need precise rules, something code Translation to like Block ! :::. Java methods Parse trees in Such precise grammar rules exist for all programming Java Parser language. See for example http://java.sun.com/docs/ generators Yacc, ANTLR, books/jls/second_edition/html/syntax.doc.html. JavaCC and SableCC Summary Example from programming language syntax Some rules for statements S in Java or C: Grammars and Parsing for SSC1 S ! if ( E ) S else S Hayo S ! while ( E ) S Thielecke S ! V = E; Introduction Grammars S ! f B g Derivations Parse trees B ! SB abstractly From B ! grammars to code Translation to Here V is for variables and E for expressions. Java methods Parse trees in Java E ! E - 1 Parser generators E ! ( E ) Yacc, ANTLR, JavaCC and E ! 1 SableCC Summary E ! E == 0 V ! foo Nesting in syntax Grammars and Specifically, we need to be very careful about bracketing and Parsing for nesting. Compare: SSC1 Hayo Thielecke while(i < n) a[i] = 0; Introduction Grammars i = i + 1; Derivations Parse trees abstractly and From grammars to code while(i < n) Translation to Java methods { Parse trees in Java a[i] = 0; Parser i = i + 1; generators Yacc, ANTLR, JavaCC and } SableCC Summary Theses snippets looks very similar. But their difference is clear in the parse tree. What do we need in a grammar Grammars and Parsing for SSC1 Hayo Thielecke Some symbols can occur in the actual syntax, like while Introduction Grammars or cat. Derivations Parse trees We also need other symbols that act like variables (for abstractly From nouns in English or statements in Java, say). grammars to code Rules then say how to replace these symbols, e.g. a noun Translation to Java methods Parse trees in can be cat or mat. Java Parser generators Yacc, ANTLR, JavaCC and SableCC Summary Grammars: formal definition Grammars and Parsing for SSC1 Hayo A context-free grammar consists of Thielecke some terminal symbols a, b,..., +, ),. Introduction Grammars some non-terminal symbols A, B, S,. Derivations Parse trees a distinguished non-terminal start symbol S abstractly From some rules of the form grammars to code Translation to Java methods A ! X ::: X Parse trees in 1 n Java Parser where n ≥ 0, A is a non-terminal, and the Xi are symbols. generators Yacc, ANTLR, JavaCC and SableCC Summary Notation: Greek letters Grammars and Mathematicians and computer scientists are inordinately fond of Parsing for Greek letters. SSC1 Hayo Thielecke Introduction Grammars Derivations Parse trees α alpha abstractly From grammars to code Translation to β beta Java methods Parse trees in Java Parser γ gamma generators Yacc, ANTLR, JavaCC and SableCC " epsilon Summary Notational conventions for grammars Grammars and Parsing for SSC1 Hayo Thielecke We will use Greek letters α, β, . , to stand for strings of Introduction symbols that may contain both terminals and non-terminals. Grammars Derivations In particular, " is used for the empty string (of length 0). Parse trees We will write A, B, . for non-terminals. abstractly From Terminal symbols are usually written in typewriter font, like grammars to code for, while, [. Translation to Java methods These conventions are handy once you get used to them and Parse trees in Java are found in most books. Parser generators Yacc, ANTLR, JavaCC and SableCC Summary Some abbreviations in grammars (BNF) Grammars and Parsing for SSC1 A rule with an alternative, written as a vertical bar j, Hayo Thielecke A ! α j β Introduction Grammars Derivations is the same as having two rules for the same non-terminal: Parse trees abstractly From A ! α grammars to code Translation to A ! β Java methods Parse trees in Java There is also some shorthand notation for repetitions: Parser ∗ generators α stands for zero or more occurences of α, and Yacc, ANTLR, + JavaCC and α stands for one or more occurrences of α. SableCC Summary Derivations Grammars and Parsing for SSC1 Hayo If A ! α is a rule, we can replace A by α for any strings β Thielecke and γ on the left and right: Introduction Grammars β A γ ) βαγ Derivations Parse trees abstractly This is one derivation step. From grammars to A string w consisting only of terminal symbols is code Translation to Java methods generated by the grammar if there is a sequence of Parse trees in derivation steps leading to it from the start symbol S: Java Parser generators Yacc, ANTLR, S )···) w JavaCC and SableCC Summary An example derivation Recall the rules Grammars and Parsing for SSC1 S ! { B } Hayo B ! SB Thielecke B ! Introduction Grammars Replacing always the leftmost non-terminal symbol, we have: Derivations Parse trees abstractly S ) { B } From grammars to ) { SB } code Translation to Java methods ) {{ B } B } Parse trees in Java ) {{} B } Parser generators ) {{} SB } Yacc, ANTLR, JavaCC and ) {{}{ B } B} SableCC Summary ) {{}{} B} ) {{}{}} The language of a grammar Grammars and Parsing for SSC1 Hayo In this context, a language is a set of strings (of terminal Thielecke symbols). Introduction For a given grammar, the language of that grammar Grammars Derivations consists of all strings of terminals that can be derived from Parse trees abstractly the start symbol. From grammars to For useful grammars, there are usually infinitely many code Translation to strings in its language (e.g., all Java programs). Java methods Parse trees in Two different grammars can define the same language. Java Parser Sometimes we may redesign the grammar as long as the generators Yacc, ANTLR, language remains the same. JavaCC and SableCC Summary Grammars and brackets Grammars and Parsing for Grammars are good at expressing various forms of bracketing SSC1 Hayo structure. Thielecke In XML: an element begins with <tag> and ends with </tag>. Introduction The most typical context-free language is the Dyck language of Grammars balanced brackets: Derivations Parse trees abstractly D ! From grammars to code D ! DD Translation to Java methods Parse trees in D ! ( D ) Java D ! [ D ] Parser generators Yacc, ANTLR, JavaCC and (This makes grammars more powerful than Regular SableCC Expressions.) Summary Matching brackets Grammars and Note that the brackets have to match in the rule Parsing for SSC1 Hayo D ! [ D ] Thielecke Introduction This is different from \any number of [ and then any number Grammars of ]". Derivations Parse trees For comparison, we could have a different grammar in which abstractly From the brackets do not have to match: grammars to code Translation to D ! ODC Java methods Parse trees in Java O ! [ O Parser generators O ! " Yacc, ANTLR, JavaCC and C ! C ] SableCC Summary C ! " Another pitfall in reading rules Grammars and Parsing for SSC1 Recall this rule: Hayo Thielecke D ! DD Introduction Grammars Note that the repetition DD does not mean that the same Derivations Parse trees string has to be repeated.
Recommended publications
  • Ece351 Lab Manual
    DEREK RAYSIDE & ECE351 STAFF ECE351 LAB MANUAL UNIVERSITYOFWATERLOO 2 derek rayside & ece351 staff Copyright © 2014 Derek Rayside & ECE351 Staff Compiled March 6, 2014 acknowledgements: • Prof Paul Ward suggested that we look into something with vhdl to have synergy with ece327. • Prof Mark Aagaard, as the ece327 instructor, consulted throughout the development of this material. • Prof Patrick Lam generously shared his material from the last offering of ece251. • Zhengfang (Alex) Duanmu & Lingyun (Luke) Li [1b Elec] wrote solutions to most labs in txl. • Jiantong (David) Gao & Rui (Ray) Kong [3b Comp] wrote solutions to the vhdl labs in antlr. • Aman Muthrej and Atulan Zaman [3a Comp] wrote solutions to the vhdl labs in Parboiled. • TA’s Jon Eyolfson, Vajih Montaghami, Alireza Mortezaei, Wenzhu Man, and Mohammed Hassan. • TA Wallace Wu developed the vhdl labs. • High school students Brian Engio and Tianyu Guo drew a number of diagrams for this manual, wrote Javadoc comments for the code, and provided helpful comments on the manual. Licensed under Creative Commons Attribution-ShareAlike (CC BY-SA) version 2.5 or greater. http://creativecommons.org/licenses/by-sa/2.5/ca/ http://creativecommons.org/licenses/by-sa/3.0/ Contents 0 Overview 9 Compiler Concepts: call stack, heap 0.1 How the Labs Fit Together . 9 Programming Concepts: version control, push, pull, merge, SSH keys, IDE, 0.2 Learning Progressions . 11 debugger, objects, pointers 0.3 How this project compares to CS241, the text book, etc. 13 0.4 Student work load . 14 0.5 How this course compares to MIT 6.035 .......... 15 0.6 Where do I learn more? .
    [Show full text]
  • Implementation of Processing in Racket 1 Introduction
    P2R Implementation of Processing in Racket Hugo Correia [email protected] Instituto Superior T´ecnico University of Lisbon Abstract. Programming languages are being introduced in several ar- eas of expertise, including design and architecture. Processing is an ex- ample of one of these languages that was created to teach architects and designers how to program. In spite of offering a wide set of features, Pro- cessing does not support the use of traditional computer-aided design applications, which are heavily used in the architecture industry. Rosetta is a generative design tool based on the Racket language that attempts to solve this problem. Rosetta provides a novel approach to de- sign creation, offering a set of programming languages that generate de- signs in different computer-aided design applications. However, Rosetta does not support Processing. Therefore, the goal is to add Processing to Rosetta's language set, offering architects that know Processing, an alternative solution that supports computer-aided design applications. In order to achieve this goal, a source-to-source compiler that translates Processing into Racket will be developed. This will also give the Racket community the ability to use Processing in the Racket environment, and, at the same time, will allow the Processing community to take advantage of Racket's libraries and development environment. In this report, an analysis of different language implementation mecha- nisms will be presented, focusing on the different steps of the compilation phase, as well as higher-level solutions, including Language Workbenches. In order to gain insight of source-to-source compiler creation, relevant existing source-to-source compilers are presented.
    [Show full text]
  • Adaptive LL(*) Parsing: the Power of Dynamic Analysis
    Adaptive LL(*) Parsing: The Power of Dynamic Analysis Terence Parr Sam Harwell Kathleen Fisher University of San Francisco University of Texas at Austin Tufts University [email protected] [email protected] kfi[email protected] Abstract PEGs are unambiguous by definition but have a quirk where Despite the advances made by modern parsing strategies such rule A ! a j ab (meaning “A matches either a or ab”) can never as PEG, LL(*), GLR, and GLL, parsing is not a solved prob- match ab since PEGs choose the first alternative that matches lem. Existing approaches suffer from a number of weaknesses, a prefix of the remaining input. Nested backtracking makes de- including difficulties supporting side-effecting embedded ac- bugging PEGs difficult. tions, slow and/or unpredictable performance, and counter- Second, side-effecting programmer-supplied actions (muta- intuitive matching strategies. This paper introduces the ALL(*) tors) like print statements should be avoided in any strategy that parsing strategy that combines the simplicity, efficiency, and continuously speculates (PEG) or supports multiple interpreta- predictability of conventional top-down LL(k) parsers with the tions of the input (GLL and GLR) because such actions may power of a GLR-like mechanism to make parsing decisions. never really take place [17]. (Though DParser [24] supports The critical innovation is to move grammar analysis to parse- “final” actions when the programmer is certain a reduction is time, which lets ALL(*) handle any non-left-recursive context- part of an unambiguous final parse.) Without side effects, ac- free grammar. ALL(*) is O(n4) in theory but consistently per- tions must buffer data for all interpretations in immutable data forms linearly on grammars used in practice, outperforming structures or provide undo actions.
    [Show full text]
  • Conflict Resolution in a Recursive Descent Compiler Generator
    LL(1) Conflict Resolution in a Recursive Descent Compiler Generator Albrecht Wöß, Markus Löberbauer, Hanspeter Mössenböck Johannes Kepler University Linz, Institute of Practical Computer Science, Altenbergerstr. 69, 4040 Linz, Austria {woess,loeberbauer,moessenboeck}@ssw.uni-linz.ac.at Abstract. Recursive descent parsing is restricted to languages whose grammars are LL(1), i.e., which can be parsed top-down with a single lookahead symbol. Unfortunately, many languages such as Java, C++, or C# are not LL(1). There- fore recursive descent parsing cannot be used or the parser has to make its deci- sions based on semantic information or a multi-symbol lookahead. In this paper we suggest a systematic technique for resolving LL(1) conflicts in recursive descent parsing and show how to integrate it into a compiler gen- erator (Coco/R). The idea is to evaluate user-defined boolean expressions, in order to allow the parser to make its parsing decisions where a one symbol loo- kahead does not suffice. Using our extended compiler generator we implemented a compiler front end for C# that can be used as a framework for implementing a variety of tools. 1 Introduction Recursive descent parsing [16] is a popular top-down parsing technique that is sim- ple, efficient, and convenient for integrating semantic processing. However, it re- quires the grammar of the parsed language to be LL(1), which means that the parser must always be able to select between alternatives with a single symbol lookahead. Unfortunately, many languages such as Java, C++ or C# are not LL(1) so that one either has to resort to bottom-up LALR(1) parsing [5, 1], which is more powerful but less convenient for semantic processing, or the parser has to resolve the LL(1) con- flicts using semantic information or a multi-symbol lookahead.
    [Show full text]
  • Keyword Based Search Engine for Web Applications Using Lucene and Javacc Priyesh Wani, Nikita Shah, Kapil Thombare, Chaitalee Zade
    Priyesh Wani et al IJCSET |April 2012| Vol 2, Issue 4,1143-1146 Keyword based Search Engine for Web Applications Using Lucene and JavaCC Priyesh Wani, Nikita Shah, Kapil Thombare, Chaitalee Zade Information Technology, Computer Science University of Pune [email protected] [email protected] [email protected] [email protected] Abstract—Past Few years IT industry has taken leap towards functionality to an application or website. Lucene is able to developing Web based Applications. The need aroused due to achieve fast search responses because, instead of searching increasing globalization. Web applications has revolutionized the text directly, it searches an index instead. Which is the way business processes. It provides scalability and equivalent to retrieving pages in a book related to a extensibility in managing different processes in respective keyword by searching the index at the back of a book, as domains. With evolving standard of these web applications some functionalities has become part of that standard, Search opposed to searching the words in each page of the book. Engine being one of them. Organization dealing with large JavacCC: Java Compiler Compiler (JavaCC) is the most number of clients has to face an overhead of maintaining huge popular parser generator for use with Java applications. A databases. Retrieval of data in that case becomes difficult from parser generator is a tool that reads a grammar specification developer’s point of view. In this paper we have presented an and converts it to a Java program that can recognize efficient way of implementing keyword based search engine matches to the grammar.
    [Show full text]
  • Literature Review
    Rhodes University Computer Science Department Honours year project Literature Review Author: Etienne Stalmans Supervisor: Dr. Karen Bradshaw June 2010 1 Introduction Software visualisation (SV) is the process of producing a visual metaphor to represent the operation of a program or algorithm [13]. The use of SV in teaching is not a new concept and numerous studies have been performed to evaluate its effectiveness. The aim of SV in teaching is to increase learners understanding of concepts through simplification. In order for a SV system to be successful, the visual metaphor needs to effectively and efficiently relay the mapped data [4, 8]. Dougles et al., [8] found that \active use of visualizations by students improves their learning process". Compiler generators produce human-readable parsers from user specified grammars. A grammar is used to formalise the syntax and semantics of a language. In order to understand how a language is parsed, it is important to understand the grammar first. Grammars are classified according to hierarchical type. The grammar type determines which parsing method will be used to evaluate it.One such parsing method is recursive descent parsing. Recursive descent parsers are used to parse Type 2 grammars [25], also known as context free grammars. Coco/R is a recursive descent parser generator which is used to teach compiler theory [25]. VCOCO provides limited visualisation of Coco/R operation. This is done by highlighting the current execution point [23]; providing a debugging approach to expert users [2]. Thus VCOCO is not suitable to be used as an education tool [2], as it assumes prior knowledge of compilers and does not contain explanation facilities which beginners require.
    [Show full text]
  • Capitolo 1 Grammatiche E Linguaggi Formali
    View metadata, citation and similar papers at core.ac.uk brought to you by CORE provided by Electronic Thesis and Dissertation Archive - Università di Pisa UNIVERSITÀ DEGLI STUDI DI PISA Facoltà di Scienze Matematiche, Fisiche e Naturali Corso di Laurea Specialistica in Informatica Tesi di Laurea Compiler Generator: Considerazioni e Confronti Candidato: Gianfilippo Bortone Relatore: Controrelatore: Prof. Marco Bellia Prof. Andrea Maggiolo Schettini Anno Accademico 2006/2007 Ai miei genitori Francesca e Donato che mi hanno sostenuto ed incoraggiato in tutti questi anni. “Una parola non riuscirebbe a racchiudere tutto ciò che è dentro di lui; difficile, a volte, entrare nel suo particolarissimo mondo, ma chiunque sia toccato dal suo affetto non può rimanere impassibile di fronte a quella miscela di stravaganza, dolcezza e fantasia che è... Gianfy B.! ” Alessandra e Francesca A tutti i miei amici. Indice Introduzione .................................................................................. V Capitolo 1: Grammatiche e linguaggi formali 1.1 Linguaggio: introduzione ............................................................ 1 1.1.1 Operazioni su stringhe e su linguaggi .................................. 2 1.1.2 Linguaggi formali ................................................................. 5 1.1.3 Rappresentazione di linguaggi: generatori e riconoscitori ... 6 1.2 Grammatica: Introduzione ........................................................... 6 1.2.1 Grammatiche regolari ...........................................................
    [Show full text]
  • Der Compilergenerator Coco/R
    Der Compilergenerator Coco/R Hanspeter Mössenböck, Albrecht Wöß, Markus Löberbauer Johannes Kepler Universität Linz, Institut für Praktische Informatik, Altenbergerstr. 69, 4040 Linz {moessenboeck,woess,loeberbauer}@ssw.uni-linz.ac.at Coco/R ist ein Compilergenerator, der aus einer attributierten Grammatik einen Scanner und einen Parser samt semantischen Aktionen erzeugt. Die erste Versi- on dieses Werkzeugs entstand in den frühen achtziger Jahren an der Universität Linz unter der Leitung von Peter Rechenberg. Inzwischen existiert Coco/R in mehreren Versionen für moderne Programmiersprachen und wird weltweit verwendet. In diesem Beitrag werden die Grundlagen, die Arbeitsweise und die Ge- schichte von Coco/R beschrieben. Ein Teil des Beitrags ist einem neuen Merk- mal von Coco/R gewidmet, nämlich der Fähigkeit, Grammatiken verarbeiten zu können, die nicht LL(1) sind. 1 Einleitung Die Übersetzung von Programmiersprachen – allgemein: die Verarbeitung struktu- rierter Datenströme – lässt sich mit attributierten Grammatiken spezifizieren. Die Syntax des Eingabestroms wird dabei durch eine kontextfreie Grammatik beschrieben und die nötigen Übersetzungsaktionen durch Attribute und semantische Aktionen, die an geeigneten Stellen in die Grammatik eingefügt werden. Attributierte Grammatiken dienen aber nicht nur der Beschreibung eines Überset- zungsprozesses, sondern können auch als seine Implementierung auf hoher Abstrak- tionsebene aufgefasst werden. Ein Werkzeug kann daraus die wesentlichen Teile eines Compilers erzeugen, nämlich einen lexikalischen Analysator (Scanner) und einen Syntaxanalysator (Parser) samt semantischen Übersetzungsaktionen. Solche Werkzeuge werden Compilergeneratoren genannt. Seit den frühen achtziger Jahren wird an der Universität Linz an solchen Werk- zeugen gearbeitet. Unter anderem ging daraus der Compilergenerator Coco/R hervor, der auch heute noch weiterentwickelt wird. Eine dieser Weiterentwicklungen erlaubt es, auch Sprachen verarbeiten zu können, deren Grammatik nicht LL(1) ist.
    [Show full text]
  • Codegenassem.Java) Lexical Analysis (Scanning)
    Undergraduate Compilers Review Structure of a Typical Interpreter Compiler Analysis Synthesis Announcements character stream – Send me email if you have set up a group. Include account names for group. lexical analysis IR code generation – Each project is 10% of grade tokens “words” IR Today syntactic analysis optimization – Outline of planned topics for course AST “sentences” IR – Overall structure of a compiler semantic analysis code generation – Lexical analysis (scanning) – Syntactic analysis (parsing) annotated AST target language interpreter CS553 Lecture Undergrad Compilers Review 2 CS553 Lecture Undergrad Compilers Review 3 Structure of the MiniJava Compiler (CodeGenAssem.java) Lexical Analysis (Scanning) Analysis Synthesis Break character stream into tokens (“words”) character stream – Tokens, lexemes, and patterns – Lexical analyzers are usually automatically generated from patterns Lexer lexical analysis IR code generation Translate (regular expressions) (e.g., lex) tokens “words” IRT Tree/ Examples Parser.parse() syntactic analysis instruction selection Mips/Codegen token lexeme(s) pattern AST “sentences” Assem const const const Project 4 if if if BuildSymTable semantic analysis optimization relation <,<=,=,!=,... < | <= | = | != | ... CheckTypes identifier foo,index [a-zA-Z_]+[a-zA-Z0-9_]* Assem CodeGenAssem number 3.14159,570 [0-9]+ | [0-9]*.[0-9]+ string “hi”, “mom” “.*” AST and symbol table code generation minijava.node/ MIPS const pi := 3.14159 ⇒ const, identifier(pi), assign,number(3.14159) SymTable/ CS553 Lecture Undergrad
    [Show full text]
  • Parsing, Lexical Analysis, and Tools
    CS 345 Parsing, Lexical Analysis, and Tools William Cook 1 2 Parsing techniques Top-Down • Begin with start symbol, derive parse tree • Match derived non-terminals with sentence • Use input to select from multiple options Bottom Up • Examine sentence, applying reductions that match • Keep reducing until start symbol is derived • Collects a set of tokens before deciding which production to use 3 Top-Down Parsing Recursive Descent • Interpret productions as functions, nonterminals as calls • Must predict which production will match – looks ahead at a few tokens to make choice • Handles EBNF naturally • Has trouble with left-recursive, ambiguous grammars – left recursion is production of form E ::= E … Also called LL(k) • scan input Left to right • use Left edge to select productions • use k symbols of look-ahead for prediction 4 Recursive Descent LL(1) Example Example E ::= E + E | E – E | T note: left recursion T ::= N | ( E ) N ::= { 0 | 1 | … | 9 } { … } means repeated Problems: • Can’t tell at beginning whether to use E + E or E - E – would require arbitrary look-ahead – But it doesn’t matter because they both begin with T • Left recursion in E will never terminate… 5 Recursive Descent LL(1) Example Example E ::= T [ + E | – E ] [ … ] means optional T ::= N | ( E ) N ::= { 0 | 1 | … | 9 } Solution • Combine equivalent forms in original production: E ::= E + E | E – E | T • There are algorithms for reorganizing grammars – cf. Greibach normal form (out of scope of this course) 6 LL Parsing Example E•23+7 E ::= T [ + E | – E ] T•23+7 T ::= N | ( E ) N ::= { 0 | 1 | … | 9 } N•23+7 23 +7 • • = Current location 23+•7 Preduction 23+E•7 indent = function call 23+T•7 23+N•7 23+7 • Intuition: Growing the parse tree from root down towards terminals.
    [Show full text]
  • 160428 Subscript
    Programming with Algebra André van Delft Anatoliy Kmetyuk Taras Shevchenko National University of Kiev Faculty of Information Technology 28 April 2016 1 Overview • Introduction – Programming is Still Hard – Some History – Algebra of Communicating Processes • SubScript – GUI application – DB query execution – Twitter Client • Conclusion 2 Programming is Still Hard Mainstream programming languages: imperative • good in batch processing • not good in parsing, concurrency, event handling • Callback Hell Neglected idioms • Non-imperative choice: BNF, YACC • Data flow: Unix pipes Math! 3 Algebra of Communicating Processes - 1 Bergstra & Klop, Amsterdam, 1982 - ... ACP ~ Boolean Algebra + choice · sequence 0 deadlock 1 empty process atomic actions a,b,… parallelism communication disruption, interruption time, space, probabilities money ... 4 Algebra of Communicating Processes - 2 Less known than CSP, CCS Specification & Verification • Communication Protocols • Production Plants • Railways • Coins and Coffee Machines • Money and Economy Strengths • Familiar syntax • Precise semantics • Reasoning by term rewriting • Events as actions 5 Algebra of Communicating Processes - 3 x+y = y+x (x+y)+z = x+(y+z) x+x = x (x+y)·z = x·z+y·z (x·y)·z = x·(y·z) 0+x = x 0·x = 0 1·x = x x·1 = x (x+1)·y = x·y + 1·y = x·y + y 6 Algebra of Communicating Processes - 4 x║y = x╙y + y╙x + x|y (x+y)╙z = ... a·x╙y = ... 1╙x = ... 0╙x = ... (x+y)|z = ... ... = ... 7 Programming Paradigms Smalltalk Parboiled Scala FastParse Lisp SubScript Haskell Java 8 Prolog λ Declarative JavaCC
    [Show full text]
  • Modern Compiler Design Associated Supplemental Materials C Implementation Details
    Modern Compiler Design Associated Supplemental Materials C Implementation Details David Galles Contents 1 Introduction & Explanation 3 1.1 WhatIsThisDocument? ............................. ......... 3 1.2 HowToUseThisDocument.. .. .. .. .. .. .. .. .. .. .. .. ......... 3 1.3 SoWhereAreChapters3and4? . .......... 3 1.4 Where Can I Find Necessary Files for Creating a Compiler inC?................ 3 2 Lexical Analysis 4 2.1 Lex–ALexicalAnalyzerGenerator . ............. 4 2.1.1 StructureofaLexfile............................ ........ 5 2.1.2 NamedRegularExpressions. .......... 6 2.1.3 TokensWithValues .............................. ....... 7 2.1.4 DealingwithWhiteSpace . ........ 8 2.1.5 KeepingTrackofTokenPositions . ........... 9 2.1.6 StatesinLex ................................... ...... 9 2.1.7 UsingLexwithOtherCPrograms. ......... 11 2.1.8 AdvancedLexFeatures . ........ 11 2.2 Creating a Lexical Analyzer for simpleJava in Lex . .................. 14 2.2.1 ProjectDefinition .............................. ........ 14 2.2.2 ProjectDifficulty ............................... ....... 18 2.2.3 Project“Gotcha”s.............................. ........ 18 2.2.4 ProvidedFiles ................................. ....... 19 5 Bottom-Up Parsing & Yacc 21 5.1 Yacc–YetAnotherCompilerCompiler . ............. 21 5.1.1 StructureofaYaccFile . ......... 21 5.1.2 DealingWithParsingErrors . .......... 22 5.1.3 TokensWithValues .............................. ....... 23 5.1.4 WhenYaccHasConflicts . .. .. .. .. .. .. .. .. .. .. .. ....... 23 5.1.5 OperatorPrecedence
    [Show full text]