LANGUAGE TRANSLATORS: WEEK 3 LECTURE: Grammar Theory Introduction to Parsing Parser - Generators TUTORIAL: Questions on grammar theory WEEKLY WORK: Read.

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
FACS Week 15 Recursive Descent Parsers, Table Driven Recursive Descent Parsers TUTORIAL: Creating LL parsers using Tables.
May 2006CLINT-LN Parsing1 Computational Linguistics Introduction Approaches to Parsing.
GRAMMAR & PARSING (Syntactic Analysis) NLP- WEEK 4.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Top-Down Parsing.
FACS Week 14 Grammars and their properties; Introduction to Parsing Lee McCluskey, room 2/07
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
Context-Free Grammars Lecture 7
Formal Aspects Term 2, Week4 LECTURE: LR “Shift-Reduce” Parsers: The JavaCup Parser-Generator CREATES LR “Shift-Reduce” Parsers, they are very commonly.
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
Professor Yihjia Tsai Tamkang University
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
COP4020 Programming Languages
Lexical and syntax analysis
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
CPSC 388 – Compiler Design and Construction
Parsing. Goals of Parsing Check the input for syntactic accuracy Return appropriate error messages Recover if possible Produce, or at least traverse,
Top-Down Parsing - recursive descent - predictive parsing
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Language Translators - Lee McCluskey LANGUAGE TRANSLATORS: WEEK 21 LECTURE: Using JavaCup to create simple interpreters
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
PART I: overview material
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
LANGUAGE TRANSLATORS: WEEK 17 scom.hud.ac.uk/scomtlm/cis2380/ See Appel’s book chapter 3 for support reading Last Week: Top-down, Table driven parsers.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
LESSON 04.
csa3050: Parsing Algorithms 11 CSA350: NLP Algorithms Parsing Algorithms 1 Top Down Bottom-Up Left Corner.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data.
More Parsing CPSC 388 Ellen Walker Hiram College.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
Lecture 3: Parsing CS 540 George Mason University.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Top-Down Parsing.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
Parsing III (Top-down parsing: recursive descent & LL(1) )
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Programming Languages Translator
CS510 Compiler Lecture 4.
Parsing and Parser Parsing methods: top-down & bottom-up
Chapter 3 Context-Free Grammar and Parsing
Introduction to Parsing (adapted from CS 164 at Berkeley)
Syntax Analysis Chapter 4.
Lexical and Syntax Analysis
CPSC 388 – Compiler Design and Construction
Chapter 2: A Simple One Pass Compiler
Lecture 7: Introduction to Parsing (Syntax Analysis)
R.Rajkumar Asst.Professor CSE
Nonrecursive Predictive Parsing
Chapter 10: Compilers and Language Translation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 2, 09/04/2003 Prof. Roy Levow.
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

LANGUAGE TRANSLATORS: WEEK 3 LECTURE: Grammar Theory Introduction to Parsing Parser - Generators TUTORIAL: Questions on grammar theory WEEKLY WORK: Read Chapter 1 of Appel’s Book, read the JavaCup manual, read an introduction to parsing (all online) School of Computing and Engineering, University of Huddersfield

GRAMMARS Theoretical properties and results about the nature of GRAMMARS have been used for 40 years to influence the design of PARSERS. A CONTEXT FREE (BNF) GRAMMAR contains a vocabulary = {terminals, non-terminals} and “production rules”/”re-write rules” of the form: non-terminal ::= v1 v vn where the vi are members of the vocabulary. One non-terminal is called the SPECIAL SYMBOL School of Computing and Engineering, University of Huddersfield

PROPERTIES OF GRAMMARS -1 A syntax tree (parse tree) of a BNF Grammar G is a tree where - the root is the special symbol of G, - the leaves are terminals of G, - the nodes are non-terminals of G, - each node is the LHS of some rule P of G; the node’s children are connected to the node by arcs, and form the RHS of P - the leaves are all connected up to the root via arcs School of Computing and Engineering, University of Huddersfield

PROPERTIES OF GRAMMARS -2 A string of token is member of the language generated by a BNF grammar G IFF it forms the leaves (in the correct order) of some syntax tree generated by G G is AMBIGUOUS IFF at least one of the strings in its language has more than one distinct syntax tree School of Computing and Engineering, University of Huddersfield

PROPERTIES OF GRAMMARS -3 A BNF grammar G is LEFT-RECURSIVE if has at least one production of the form: X ::= X w where X is a non-terminal and w is a string of symbols in G’s vocabulary School of Computing and Engineering, University of Huddersfield

PROPERTIES OF GRAMMARS -4 If w is a list of symbols in the vocabulary of BNF grammar G then First(w) = set of TERMINAL symbols that may be at the front of ANY string derived from w using G’s productions School of Computing and Engineering, University of Huddersfield

PROPERTIES OF GRAMMARS -5 If X is a non-terminal of BNF grammar G then Follow(X) = set of TERMINAL symbols that can follow X in a derivation from G’s special symbol using G’s productions. Nullable(X) is true IFF the empty word can be derived from X School of Computing and Engineering, University of Huddersfield

PARSING A PARSER (or ‘PARSING ALGORITHM’) derives SYNTAX/PARSE TREES from a sequence of TOKENS Some well-known tools are little more than PARSERS e.g. Syntax Directed Editors - those that colour different syntax classes or point out syntax errors as you make them PARSING ALGORITHMS invariably are based on the CONTEXT FREE GRAMMAR that defines the language being parsed. School of Computing and Engineering, University of Huddersfield

Example Input String: b = 5; a = 5 * b; PRINT(b * a) Ouput PARSE TREE (as a Java data structure) new CompoundStm( new AssignStm("b",new NumExp(5)), new CompoundStm( new AssignStm("a", new OpExp(new NumExp(5), OpExp.Times, new IdExp("b"))), new PrintStm(new LastExpList(new OpExp(new IdExp("b"), OpExp.Times, new IdExp("a")))) ) ); School of Computing and Engineering, University of Huddersfield

PARSING ALGORITHMS Parsers that follow the language’s grammar can be TOP DOWN: - starting with the grammar’s special symbol, try to find a derivation of the string of tokens being parsed, consuming one token at a time BOTTOM UP: - start with the string viewed as a stack. Match the top ‘n’ tokens of the stack with the RHS of a grammar rule P and replace those tokens with the LHS of P School of Computing and Engineering, University of Huddersfield

PRACTICAL EXAMPLE OF THEORY An grammar G is LL(1) if and only if (i) G is NOT ambiguous (ii) G is NOT left recursive (iii) for EVERY two of G’s productions of the form X ::= W1, X ::= W2, it is the case that First(W1) and First(W2) have no common element LL(1) grammars are nice grammars, they can be translated into a very efficient LL(1) PARSING TABLE. School of Computing and Engineering, University of Huddersfield

Practical: JavaCup JavaCup is a tool that inputs a GRAMMAR in BNF form (+ other stuff..) and outputs a PARSE TREE (in the form of Java Constructors). Parsers created using JavaCup accept a sequence of TOKENS as input from a SCANNER such as last week’s. An easy way to show JavaCup’s use is to implement a simple interpreter with it: (i) JavaCup inputs a grammar and creates a corresponding parser P in java. (ii) P accepts tokens from the scanned input and generates and passes the parse tree to some Java code which then EVALUATES it. School of Computing and Engineering, University of Huddersfield

SUMMARY Parsers check strings are legal according to grammatical definitions, and build up a structure representing legal strings (parse trees) Parsers can sometimes be auto-generated from the defining grammar Theory of grammars helps us in the auto-generation of parsers School of Computing and Engineering, University of Huddersfield