Grammars and Parsing for SSC1

Grammars and Parsing for SSC1

Grammars and Parsing for SSC1 Hayo Thielecke http://www.cs.bham.ac.uk/~hxt/parsing.html 1 Introduction Outline of the parsing part of the module Contents 1 Introduction 1 2 Grammars and derivations 2 3 From grammars to parsing methods 7 4 From grammars to parse trees as data structures 12 4.1 Parse trees in Java . 13 5 Parser generators 17 5.1 Yacc, ANTLR, JavaCC and SableCC . 18 6 Summary 20 Motivation: a challenge • Write a program that reads strings like this and evaluates them: (2+3*(2-3-4))*2 • In particular, brackets and precedence must be handled correctly (* binds more tightly than +). • If you attempt brute-force hacking, you may end up with something that is inefficient, incorrect, or both. |Try it! • This sort of problem was major challenge in the 1950s and 60s in compiling. • Parsing technology makes it straighforward. • Moreover, the techniques scale up to more realistic problems. 1 Why do you need to learn about grammars? • Grammars are widespread in programming. • XML is based on grammars and needs to be parsed. • Knowing grammars makes it much easier to learn the syntax of a new programming language. • Powerful tools exists for parsing (e.g., yacc, bison, ANTLR, SableCC, . ). But you have to understand grammars to use them. • Grammars give us examples of some more advanced object-oriented pro- gramming: Composite Pattern and polymorphic methods. • You may need parsing in your final-year project for reading complex input. Books I hope these slides are detailed enough, but if you want to dig deeper: There are lots of book on compilers. The ones which I know best are: • Appel, Modern Compiler Design in Java. • Aho, Sethi, and Ullman, nicknamed \The Dragon Book" Parsing is covered in both, but the Composite Pattern for trees and Visitors for tree walking only in Appel. See also the websites for ANTLR (http://antlr.org) and SableCC (http: //sablecc.org). Describing syntax • One often has to describe syntax precisely, particularly when dealing with computers. • \A block consists of a sequence of statements enclosed in curly brackets " • Informal English descriptions are too clumsy and not precise enough. Rather, we need precise rules, something like Block ! :::. • Such precise grammar rules exist for all programming language. See for example http://java.sun.com/docs/books/jls/second_edition/ html/syntax.doc.html. 2 Grammars and derivations What do we need in a grammar • Some symbols can occur in the actual syntax, like while or cat. • We also need other symbols that act like variables (for verbs in English or statements in Java, say). 2 • Rules then say how to replace these symbols • E.g. in English a verb can be sit. • E.g. in Java, modifier can be public, private, etc Example from programming language syntax Some rules for statements S in Java or C: S ! if ( E ) S else S S ! while ( E ) S S ! V = E; S ! f B g B ! SB B ! Here V is for variables and E for expressions. E ! E - 1 E ! ( E ) E ! 1 E ! E == 0 V ! foo Nesting in syntax Specifically, we need to be very careful about bracketing and nesting. Com- pare: while(i < n) a[i] = 0; i = i + 1; and while(i < n) { a[i] = 0; i = i + 1; } Theses snippets looks very similar. But their difference is clear when in- dented properly|which requires parsing. Grammars: formal definition A context-free grammar consists of • some terminal symbols a, b,..., +, ),. • some non-terminal symbols A, B, S,. • a distinguished non-terminal start symbol S 3 • some rules of the form A ! X1 :::Xn where n ≥ 0, A is a non-terminal, and the Xi are symbols. Notation: Greek letters Mathematicians and computer scientists are inordinately fond of Greek letters. α alpha β beta γ gamma " epsilon Notational conventions for grammars • We will use Greek letters α, β, . , to stand for strings of symbols that may contain both terminals and non-terminals. • In particular, " is used for the empty string (of length 0). • We will write A, B, . for non-terminals. • Terminal symbols are usually written in typewriter font, like for, while, [. • These conventions are handy once you get used to them and are found in most books. Some abbreviations in grammars (BNF) A rule with an alternative, written as a vertical bar j, A ! α j β is the same as having two rules for the same non-terminal: A ! α A ! β There is also some shorthand notation for repetitions: α∗ stands for zero or more occurences of α, and α+ stands for one or more occurrences of α. 4 Derivations • If A ! α is a rule, we can replace A by α for any strings β and γ on the left and right: βAγ ) βαγ This is one derivation step. • A string w consisting only of terminal symbols is generated by the gram- mar if there is a sequence of derivation steps leading to it from the start symbol S: S )···) w An example derivation Recall the rules S ! { B } B ! SB B ! Replacing always the leftmost non-terminal symbol, we have: S ) { B } ) { SB } ) {{ B } B } ) {{} B } ) {{} SB } ) {{}{ B } B} ) {{}{} B} ) {{}{}} The language of a grammar • In this context, a language is a set of strings (of terminal symbols). • For a given grammar, the language of that grammar consists of all strings of terminals that can be derived from the start symbol. • For useful grammars, there are usually infinitely many strings in its lan- guage (e.g., all Java programs). • Two different grammars can define the same language. Sometimes we may redesign the grammar as long as the language remains the same. 5 Definition of a parser Suppose a grammar is given. A parser is a program such that for any input string w: • If the string w is in the language of the grammar, the parser finds a derivation of the string • If string w is not in the language of the grammar, the parser must reject it, for example by raising a parsing exception • The parser always terminates. Grammars and brackets Grammars are good at expressing various forms of bracketing structure. In XML: an element begins with <tag> and ends with </tag>. The most typical context-free language is the Dyck language of balanced brackets: D ! D ! DD D ! ( D ) D ! [ D ] (This makes grammars more powerful than Regular Expressions.) Matching brackets Note that the brackets have to match in the rule D ! [ D ] This is different from \any number of [ and then any number of ]". For comparison, we could have a different grammar in which the brackets do not have to match: D ! ODC O ! [ O O ! " C ! C ] C ! " Another pitfall in reading rules Recall this rule: D ! DD Note that the repetition DD does not mean that the same string has to be repeated. It means any string derived from a D followed by any string derived 6 from a D. They may be the same, but need not. D ) DD ::: ) [] D ::: ) [][[]] Recursion in grammars A symbol may occur on the right hand-side of one of its rules: E ! (E) We often have mutual recursion in grammars: S ! { B } B ! SB Mutual recursion also exists in Java: for example, method f calls g and g calls f. Recursion in grammars and in Java Compare recursion in grammars, methods and classes: T ! :::T:::T::: int sumTree(Tree t) { ... ... return sumTree(t.left) + sumTree(t.right); } and classes public class Tree { ... public Tree left; public Tree right; } 3 From grammars to parsing methods Automata and grammars|what we are not going to do Pushdown automata (stack machines) are covered in Models of Computa- tion. The use of a stack for parsing will be covered in more detail in Compilers and Languages. Independently of formal automata models, we can use a programming per- spective: grammars give us classes or methods. For experts: we let Java manage the stack for us (as its call stack). 7 Parsing techniques • There are many different parsing technologies (LL(k), LR(1), LALR(1), . ). • Here we consider only predictive parsers, sometime also called recursive descent parsers. They correspond to translating grammar rules into code as described below. • The hard part is choosing the rules according to the lookahead. Predictive parser • A predictive parser can be constructed from grammar rules. • The parser is allowed to \look ahead" in the input; based on what it sees there, it then makes predictions. • Canonical example: matching brackets. If the parser sees a [ as the next input symbol, it \predicts" that the input contains something in brackets. • More technically: switch on the lookahead; [ labels one case. Overview: from grammars to Java code Methods for processing the language follow the structure of the grammar: Each non-terminal gives a method (with mutual recursion between such methods). Each grammar gives us a Java class hierarchy (with mutual recursion be- tween such classes). Each word in the language of the grammar gives us an object of the class corresponding to the start symbol (a parse tree). Recursive methods From grammars to mutually recursive methods: • For each non-terminal A there is a method A. The method body is a switch statement that chooses a rule for A. • For each rule A ! X1 :::Xn, there is a branch in the switch statement. There are method calls for all the non-terminals among X1,..., Xn. Each grammar gives us some recursive methods. For each derivation in the language, we have a sequence of method calls. Reminder: the switch statement in Java Assume test() returns an integer. We can use switch to branch on which integer is returned: switch(test()) { case 1: // test() is 1 break; // we are done with this case 8 case 2: case 3: // test() is 2 or 3 break; default: // test() had some other value break; } Switch and if We could write the same more clumsily with if and else: temp = test(); if (temp == 1) { // test() returned 1 } else if (temp == 2 && temp == 3) { // test() returned 2 or 3 } else { // test() returned some other value } Grammar of switch We extend the grammar for statements S with switch statements: S ! switch (E){ B } B ! CB j " C ! LT L ! K j default: K ! case V : j case V : K T ! break; j return E; j ST Here E can be any expression and V any constant value.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    20 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us