CMSC 330, Fall 2009, Practice Problem 3 Solutions


 Spencer Edwards
 3 years ago
 Views:
Transcription
1 CMC 330, Fall 2009, Practice Problem 3 olutions 1. Context Free Grammars a. List the 4 components of a context free grammar. Terminals, nonterminals, productions, start symbol b. Describe the relationship between terminals, nonterminals, productions. Productions are rules for replacing a single nonterminal with a string of terminals nonterminals c. Define ambiguity. Multiple leftmost (or rightmost) derivations for the same string d. Describe the difference between scanning & parsing. canning matches input to regular expressions to produce terminals, parsing matches terminals to grammars to create parse trees 2. Describing Grammars a. Describe the language accepted by the following grammar: ab a (ab)*a b. Describe the language accepted by the following grammar: ab a n b n, n 0 c. Describe the language accepted by the following grammar: bb A A aa b n a * b n, n 0 d. Describe the language accepted by the following grammar: A B A aac Aa B bbb trings of a & c with same or fewer c s than a s no prefix has more c s than a s, followed by an even number of b s e. Describe the language accepted by the following grammar: or () false Boolean expressions of & false separated by & or, with some expressions enclosed in parentheses f. Which of the previous grammars are left recursive? 2d, 2e g. Which of the previous grammars are right recursive? 2a, 2c, 2d, 2e h. Which of the previous grammars are ambiguous? Provide proof. Examples of multiple leftmost derivations for the same string 2d: => A => Aa => a => ab => a => A => => A => Aa => a => ab => a 2e: => => => => => => => => => =>
2 3. Creating Grammars a. Write a grammar for a x b y, where x = y ab b. Write a grammar for a x b y, where x > y al L al alb c. Write a grammar for a x b y, where x = 2y aab d. Write a grammar for a x b y a z, where z = x+y aa L L bla e. Write a grammar for a x b y a z, where z = xy aa L L alb f. Write a grammar for all strings of a b that are palindromes. aa bb L L a b g. Write a grammar for all strings of a b that include the substring baa. LbaaL L al bl // L = any h. Write a grammar for all strings of a b with an odd number of a s an odd number of b s. EaEbE EbEaE E EaEaE EbEbE // E = even #s i. Write a grammar for the if statement in OCaml if E then E else E if E then E E expr j. Write a grammar for all lists in OCaml [] [E] E:: E elem // Ignores types, allows lists of lists k. Which of your grammars are ambiguous? Can you come up with an unambiguous grammar that accepts the same language? Grammar for 3h is ambiguous. An unambiguous grammar must exist since the language can be recognized by a deterministic finite automaton, DFA > RE > Regular Grammar. Grammar for 3i is ambiguous. Multiple derivations for if expr then if expr then expr else expr. It is possible to write an unambiguous grammar by restricting some so that no unbalanced if statement can be produced. 4. Derivations, Parse Trees, Precedence Associativity For the following grammar: a. List 4 derivations for the string. i. => => => => => ii. => => => => => iii. => => => => => iv. => => => => => v. => => => => =>
3 vi. => => => => => vii. => => => => => viii. => => => => => ix. => => => => => x. => => => => => xi. => => => => => xii. => => => => => xiii. => => => => => xiv. => => => => => xv. => => => => => xvi. => => => => => b. Label each derivation as leftmost, rightmost, or neither. i ii are leftmost derivations, iii iv are rightmost derivations, remaining derivations are neither c. List the parse tree for each derivation Tree 1 = ii, iii, x, xi, Tree 2 = rest Tree 1 Tree 2 d. What is implied about the associativity of for each parse tree? Tree 1 => is rightassociative, Tree 2 => is leftassociative For the following grammar: or e. List all parse trees for the string or
4 Tree 1 Tree 2 or or f. What is implied about the precedence/associativity of or for each parse tree? Tree 1 => or has higher precedence than Tree 2 => has higher precedence than or g. Rewrite the grammar so that has higher precedence than or is right associative or L // op closer to tart = lower precedence op L L // right recursive = right associative 5. Left factoring & eliminating left recursion Rewrite the following grammars so they can be parsed by a predicative parser by eliminating left recursion applying left factoring where necessary a. + a b b L L + a L b. + a + b c c L L + a L + b L c L L + M M a L b L c. a b c a c a L L b c c d. a a a b a a L L a b e. a c a b b a L b L c b
5 6. Parsing For the problem, assume the term predictive parser refers to a topdown, recursive descent, nonbacktracking predictive parser. a. Consider the following grammar: or () false i. Compute First sets for each production nonterminal First() = { First(false) = { false First( () ) = { ( First( ) = First( or ) = First( ) = { (,, false ii. Explain why the grammar cannot be parsed by a predictive parser First sets of productions intersect, grammar is left recursive b. Consider the following grammar: ab ac c i. Compute First sets for each production nonterminal First(ab) = { a First(ac) = { a First(c) = { c First() = { a, c ii. how why the grammar cannot be parsed by a predictive parser. First sets of productions overlap First(ab) First(ac) = { a { a = { a iii. Rewrite the grammar so it can be parsed by a predictive parser. al c L b c iv. Write a predictive parser for the rewritten grammar. parse_( ) { if (lookahead == a ) { match( a ); // al else if (lookahead == c ) match( c ); // c else error( ); parse_l( ) { if (lookahead == b ) { match( b ); // L b parse_( ); else if (lookahead == c ) { match( c ); // L c parse_( ); else error( );
6 c. Consider the following grammar: a c c i. how why the grammar cannot be parsed by a predictive parser. First sets of productions intersect, grammar is left recursive ii. Rewrite the grammar so it can be parsed by a predictive parser. c L L al cl iii. Write a recursive descent parser for your new grammar parse_( ) { if (lookahead == c ) { match( c ); // cl else error( ); parse_l( ) { if (lookahead == a ) { match( a ); // L al else if (lookahead == c ) { match( c ); // L cl else ; // L d. Describe an abstract syntax tree (AT) Compact representations of parse trees with only essential parts 7. Automata a. Compare finite automata pushdown automata Pushdown automata are finite automata that can use 1 stack. Pushdown automata > finite automata in terms of computing power.
CMSC 330 Practice Problem 4 Solutions
CMC 330 Practice Problem 4 olutions 1. Context Free Grammars a. List the 4 components of a context free grammar. Terminals, nonterminals, productions, start symbol b. Describe the relationship between
More informationMidterm I (Solutions) CS164, Spring 2002
Midterm I (Solutions) CS164, Spring 2002 February 28, 2002 Please read all instructions (including these) carefully. There are 9 pages in this exam and 5 questions, each with multiple parts. Some questions
More informationAbout the Authors... iii Introduction... xvii. Chapter 1: System Software... 1
Table of Contents About the Authors... iii Introduction... xvii Chapter 1: System Software... 1 1.1 Concept of System Software... 2 Types of Software Programs... 2 Software Programs and the Computing Machine...
More informationCSE431 Translation of Computer Languages
CSE431 Translation of Computer Languages Top Down Parsers Doug Shook Top Down Parsers Two forms: Recursive Descent Table Also known as LL(k) parsers: Read tokens from Left to right Produces a Leftmost
More informationAutomata Theory TEST 1 Answers Max points: 156 Grade basis: 150 Median grade: 81%
Automata Theory TEST 1 Answers Max points: 156 Grade basis: 150 Median grade: 81% 1. (2 pts) See text. You can t be sloppy defining terms like this. You must show a bijection between the natural numbers
More informationCOP 3402 Systems Software Syntax Analysis (Parser)
COP 3402 Systems Software Syntax Analysis (Parser) Syntax Analysis 1 Outline 1. Definition of Parsing 2. Context Free Grammars 3. Ambiguous/Unambiguous Grammars Syntax Analysis 2 Lexical and Syntax Analysis
More informationCMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Parsing CMSC 330  Spring 2017 1 Recall: Front End Scanner and Parser Front End Token Source Scanner Parser Stream AST Scanner / lexer / tokenizer converts
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 informationAUBER (Models of Computation, Languages and Automata) EXERCISES
AUBER (Models of Computation, Languages and Automata) EXERCISES Xavier Vera, 2002 Languages and alphabets 1.1 Let be an alphabet, and λ the empty string over. (i) Is λ in? (ii) Is it true that λλλ=λ? Is
More informationSyntax Analysis Check syntax and construct abstract syntax tree
Syntax Analysis Check syntax and construct abstract syntax tree if == = ; b 0 a b Error reporting and recovery Model using context free grammars Recognize using Push down automata/table Driven Parsers
More informationSometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.
Eliminating Ambiguity Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity. Example: consider the following grammar stat if expr then stat if expr then stat else stat other One can
More informationCS375: Logic and Theory of Computing
CS375: Logic and Theory of Computing Fuhua (Frank) Cheng Department of Computer Science University of Kentucky 1 Table of Contents: Week 1: Preliminaries (set algebra, relations, functions) (read Chapters
More informationTorben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer
Torben./Egidius Mogensen Introduction to Compiler Design ^ Springer Contents 1 Lexical Analysis 1 1.1 Regular Expressions 2 1.1.1 Shorthands 4 1.1.2 Examples 5 1.2 Nondeterministic Finite Automata 6 1.3
More informationLL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012
Predictive Parsers LL(k) Parsing Can we avoid backtracking? es, if for a given input symbol and given nonterminal, we can choose the alternative appropriately. his is possible if the first terminal of
More informationArchitecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End
Architecture of Compilers, Interpreters : Organization of Programming Languages ource Analyzer Optimizer Code Generator Context Free Grammars Intermediate Representation Front End Back End Compiler / Interpreter
More informationWhere We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars
CMSC 330: Organization of Programming Languages Context Free Grammars Where We Are Programming languages Ruby OCaml Implementing programming languages Scanner Uses regular expressions Finite automata Parser
More informationPART 3  SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309
PART 3  SYNTAX ANALYSIS F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 64 / 309 Goals Definition of the syntax of a programming language using context free grammars Methods for parsing
More informationSyntax Analysis: Contextfree Grammars, Pushdown Automata and Parsing Part  4. Y.N. Srikant
Syntax Analysis: Contextfree Grammars, Pushdown Automata and Part  4 Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler
More informationR10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?
R1 SET  1 1. a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA? 2. a) Design a DFA that accepts the language over = {, 1} of all strings that
More informationContextFree Languages & Grammars (CFLs & CFGs) Reading: Chapter 5
ContextFree Languages & Grammars (CFLs & CFGs) Reading: Chapter 5 1 Not all languages are regular So what happens to the languages which are not regular? Can we still come up with a language recognizer?
More informationParsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones
Parsing III (Topdown parsing: recursive descent & LL(1) ) (Bottomup parsing) CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper,
More informationITEC2620 Introduction to Data Structures
ITEC2620 Introduction to Data Structures Lecture 9b Grammars I Overview How can a computer do Natural Language Processing? Grammar checking? Artificial Intelligence Represent knowledge so that brute force
More informationCOP4020 Programming Languages. Syntax Prof. Robert van Engelen
COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview n Tokens and regular expressions n Syntax and contextfree grammars n Grammar derivations n More about parse trees n Topdown and
More informationJNTUWORLD. Code No: R
Code No: R09220504 R09 SET1 B.Tech II Year  II Semester Examinations, AprilMay, 2012 FORMAL LANGUAGES AND AUTOMATA THEORY (Computer Science and Engineering) Time: 3 hours Max. Marks: 75 Answer any five
More informationITEC2620 Introduction to Data Structures
ITEC2620 Introduction to Data Structures Lecture 10a Grammars II Review Three main problems Derive a sentence from a grammar Develop a grammar for a language Given a grammar, determine if an input is valid
More informationCSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D1
CSE P 501 Compilers LR Parsing Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 D1 Agenda LR Parsing Tabledriven Parsers Parser States ShiftReduce and ReduceReduce conflicts UW CSE P 501 Spring 2018
More informationCS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution
CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution Exam Facts Format Wednesday, July 20 th from 11:00 a.m. 1:00 p.m. in Gates B01 The exam is designed to take roughly 90
More informationDerivations of a CFG. MACM 300 Formal Languages and Automata. Contextfree Grammars. Derivations and parse trees
Derivations of a CFG MACM 300 Formal Languages and Automata Anoop Sarkar http://www.cs.sfu.ca/~anoop strings grow on trees strings grow on Noun strings grow Object strings Verb Object Noun Verb Object
More informationBuilding Compilers with Phoenix
Building Compilers with Phoenix SyntaxDirected Translation Structure of a Compiler Character Stream Intermediate Representation Lexical Analyzer MachineIndependent Optimizer token stream Intermediate
More informationParsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.
Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008. Outline. Topdown versus Bottomup Parsing. Recursive Descent Parsing. Left Recursion Removal. Left Factoring. Predictive Parsing. Introduction.
More informationCompilation Lecture 3: Syntax Analysis: TopDown parsing. Noam Rinetzky
Compilation 03683133 Lecture 3: Syntax Analysis: TopDown parsing Noam Rinetzky 1 Recursive descent parsing Define a function for every nonterminal Every function work as follows Find applicable production
More information3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino
3. Syntax Analysis Andrea Polini Formal Languages and Compilers Master in Computer Science University of Camerino (Formal Languages and Compilers) 3. Syntax Analysis CS@UNICAM 1 / 54 Syntax Analysis: the
More informationCompiler Construction Using
Compiler Construction Using Java, JavaCC, and Yacc ANTHONY J. DOS REIS Stale University ofnew York at New Pallz IEEE computer society WILEY A JOHN WILEY & SONS, INC., PUBLICATION Preface xv Chapter 1 Strings,
More informationParsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1
Parsing #1 Leonidas Fegaras CSE 5317/4305 L3: Parsing #1 1 Parser source file get next character scanner get token parser AST token A parser recognizes sequences of tokens according to some grammar and
More informationRoll No. :... Invigilator's Signature :. CS/B.Tech(CSE)/SEM7/CS701/ LANGUAGE PROCESSOR. Time Allotted : 3 Hours Full Marks : 70
Name : Roll No. :... Invigilator's Signature :. CS/B.Tech(CSE)/SEM7/CS701/201112 2011 LANGUAGE PROCESSOR Time Allotted : 3 Hours Full Marks : 70 The figures in the margin indicate full marks. Candidates
More informationParsing II Topdown parsing. Comp 412
COMP 412 FALL 2018 Parsing II Topdown parsing Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled
More informationCompiler Construction
Compiler Construction Exercises 1 Review of some Topics in Formal Languages 1. (a) Prove that two words x, y commute (i.e., satisfy xy = yx) if and only if there exists a word w such that x = w m, y =
More informationFall Compiler Principles Lecture 3: Parsing part 2. Roman Manevich BenGurion University
Fall 20142015 Compiler Principles Lecture 3: Parsing part 2 Roman Manevich BenGurion University Tentative syllabus Front End Intermediate Representation Optimizations Code Generation Scanning Lowering
More informationParsing. source code. while (k<=n) {sum = sum+k; k=k+1;}
Compiler Construction Grammars Parsing source code scanner tokens regular expressions lexical analysis Lennart Andersson parser context free grammar Revision 2012 01 23 2012 parse tree AST builder (implicit)
More informationCMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Context Free Grammars and Parsing 1 Recall: Architecture of Compilers, Interpreters Source Parser Static Analyzer Intermediate Representation Front End Back
More informationPrinciples of Programming Languages COMP251: Syntax and Grammars
Principles of Programming Languages COMP251: Syntax and Grammars Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology Hong Kong, China Fall 2006
More informationPlan for Today. Regular Expressions: repetition and choice. Syntax and Semantics. Context Free Grammars
Plan for Today Context Free s models for specifying programming languages syntax semantics example grammars derivations Parse trees yntaxdirected translation Used syntaxdirected translation to interpret
More informationCS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)
CS1622 Lecture 9 Parsing (4) CS 1622 Lecture 9 1 Today Example of a recursive descent parser Predictive & LL(1) parsers Building parse tables CS 1622 Lecture 9 2 A Recursive Descent Parser. Preliminaries
More informationCompila(on (Semester A, 2013/14)
Compila(on 03683133 (Semester A, 2013/14) Lecture 4: Syntax Analysis (Top Down Parsing) Modern Compiler Design: Chapter 2.2 Noam Rinetzky Slides credit: Roman Manevich, Mooly Sagiv, Jeff Ullman, Eran
More informationContextFree Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation
Concepts Introduced in Chapter 2 A more detailed overview of the compilation process. Parsing Scanning Semantic Analysis SyntaxDirected Translation Intermediate Code Generation ContextFree Grammar A
More informationPSD3A Principles of Compiler Design Unit : IV. PSD3A Principles of Compiler Design
PSD3A Principles of Compiler Design Unit : IV 1 UNIT I  SYLLABUS Compiler Assembler Language Processing System Phases of Compiler Lexical Analyser Finite Automata NFA DFA Compiler Tools 2 Compiler 
More informationSyntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.
Syntax Syntax Syntax defines what is grammatically valid in a programming language Set of grammatical rules E.g. in English, a sentence cannot begin with a period Must be formal and exact or there will
More informationCOP4020 Programming Languages. Syntax Prof. Robert van Engelen
COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview Tokens and regular expressions Syntax and contextfree grammars Grammar derivations More about parse trees Topdown and bottomup
More informationCMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler
More informationCSE 3302 Programming Languages Lecture 2: Syntax
CSE 3302 Programming Languages Lecture 2: Syntax (based on slides by Chengkai Li) Leonidas Fegaras University of Texas at Arlington CSE 3302 L2 Spring 2011 1 How do we define a PL? Specifying a PL: Syntax:
More information1. [5 points each] True or False. If the question is currently open, write O or Open.
University of Nevada, Las Vegas Computer Science 456/656 Spring 2018 Practice for the Final on May 9, 2018 The entire examination is 775 points. The real final will be much shorter. Name: No books, notes,
More informationBuilding a Parser III. CS164 3:305:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1
Building a Parser III CS164 3:305:00 TT 10 Evans 1 Overview Finish recursive descent parser when it breaks down and how to fix it eliminating left recursion reordering productions Predictive parsers (aka
More informationCSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D1
CSE 401 Compilers LR Parsing Hal Perkins Autumn 2011 10/10/2011 200211 Hal Perkins & UW CSE D1 Agenda LR Parsing Tabledriven Parsers Parser States ShiftReduce and ReduceReduce conflicts 10/10/2011
More informationIntroduction to Syntax Analysis
Compiler Design 1 Introduction to Syntax Analysis Compiler Design 2 Syntax Analysis The syntactic or the structural correctness of a program is checked during the syntax analysis phase of compilation.
More informationUniversity of Nevada, Las Vegas Computer Science 456/656 Fall 2016
University of Nevada, Las Vegas Computer Science 456/656 Fall 2016 The entire examination is 925 points. The real final will be much shorter. Name: No books, notes, scratch paper, or calculators. Use pen
More informationQUESTION BANK. Formal Languages and Automata Theory(10CS56)
QUESTION BANK Formal Languages and Automata Theory(10CS56) Chapter 1 1. Define the following terms & explain with examples. i) Grammar ii) Language 2. Mention the difference between DFA, NFA and εnfa.
More informationContextFree Grammar (CFG)
ContextFree Grammar (CFG) contextfree grammar looks like this bunch of rules: ain idea: + 1 (),, are nonterminal symbols aka variables. When you see them, you apply rules to expand. One of them is designated
More informationPrinciples of Programming Languages COMP251: Syntax and Grammars
Principles of Programming Languages COMP251: Syntax and Grammars Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology Hong Kong, China Fall 2007
More informationQUESTION BANK. Unit 1. Introduction to Finite Automata
QUESTION BANK Unit 1 Introduction to Finite Automata 1. Obtain DFAs to accept strings of a s and b s having exactly one a.(5m )(JunJul 10) 2. Obtain a DFA to accept strings of a s and b s having even
More informationCMSC330 Spring 2014 Midterm 2 Solutions
CMSC330 Spring 2014 Midterm 2 Solutions 1. (16 pts) OCaml Types and Type Inference Give the type of the following OCaml expressions: a. (2 pts) fun b > b + 8 Type = int > int b. (3 pts) fun b > b 8
More informationParsing Part II (Topdown parsing, leftrecursion removal)
Parsing Part II (Topdown parsing, leftrecursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit
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 informationIntroduction to Parsing. Comp 412
COMP 412 FALL 2010 Introduction to Parsing Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make
More informationGrammars & Parsing. Lecture 12 CS 2112 Fall 2018
Grammars & Parsing Lecture 12 CS 2112 Fall 2018 Motivation The cat ate the rat. The cat ate the rat slowly. The small cat ate the big rat slowly. The small cat ate the big rat on the mat slowly. The small
More informationSyntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6Feb2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.
Syntax Analysis Prof. James L. Frankel Harvard University Version of 6:43 PM 6Feb2018 Copyright 2018, 2015 James L. Frankel. All rights reserved. ContextFree Grammar (CFG) terminals nonterminals start
More informationChapter 3. Parsing #1
Chapter 3 Parsing #1 Parser source file get next character scanner get token parser AST token A parser recognizes sequences of tokens according to some grammar and generates Abstract Syntax Trees (ASTs)
More informationGujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) JulyNov Compiler Design (170701)
Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) JulyNov 2014 Compiler Design (170701) Question Bank / Assignment Unit 1: INTRODUCTION TO COMPILING
More informationChapter 4: Syntax Analyzer
Chapter 4: Syntax Analyzer Chapter 4: Syntax Analysis 1 The role of the Parser The parser obtains a string of tokens from the lexical analyzer, and verifies that the string can be generated by the grammar
More informationBottomUp Parsing. Parser Generation. LR Parsing. Constructing LR Parser
Parser Generation Main Problem: given a grammar G, how to build a topdown parser or a bottomup parser for it? parser : a program that, given a sentence, reconstructs a derivation for that sentence 
More informationCS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University
CS415 Compilers Syntax Analysis These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Limits of Regular Languages Advantages of Regular Expressions
More informationParsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008
Parsing: continued David Notkin Autumn 2008 Parsing Algorithms Earley s algorithm (1970) works for all CFGs O(N 3 ) worst case performance O(N 2 ) for unambiguous grammars Based on dynamic programming,
More informationPart 3. Syntax analysis. Syntax analysis 96
Part 3 Syntax analysis Syntax analysis 96 Outline 1. Introduction 2. Contextfree grammar 3. Topdown parsing 4. Bottomup parsing 5. Conclusion and some practical considerations Syntax analysis 97 Structure
More informationSyntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38
Syntax Analysis Martin Sulzmann Martin Sulzmann Syntax Analysis 1 / 38 Syntax Analysis Objective Recognize individual tokens as sentences of a language (beyond regular languages). Example 1 (OK) Program
More informationOptimizing Finite Automata
Optimizing Finite Automata We can improve the DFA created by MakeDeterministic. Sometimes a DFA will have more states than necessary. For every DFA there is a unique smallest equivalent DFA (fewest states
More informationCS143 Midterm Fall 2008
CS143 Midterm Fall 2008 Please read all instructions (including these) carefully. There are 4 questions on the exam, some with multiple parts. You have 75 minutes to work on the exam. The exam is closed
More informationS Y N T A X A N A L Y S I S LR
LR parsing There are three commonly used algorithms to build tables for an LR parser: 1. SLR(1) = LR(0) plus use of FOLLOW set to select between actions smallest class of grammars smallest tables (number
More informationParsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.
Parsing Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students
More informationConcepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens
Concepts Introduced in Chapter 3 Lexical Analysis Regular Expressions (REs) Nondeterministic Finite Automata (NFA) Converting an RE to an NFA Deterministic Finite Automatic (DFA) Lexical Analysis Why separate
More informationCSE 401 Midterm Exam Sample Solution 2/11/15
Question 1. (10 points) Regular expression warmup. For regular expression questions, you must restrict yourself to the basic regular expression operations covered in class and on homework assignments:
More informationCMSC330 Spring 2017 Midterm 2
CMSC330 Spring 2017 Midterm 2 Name (PRINT YOUR NAME as it appears on gradescope ): Discussion Time (circle one) 10am 11am 12pm 1pm 2pm 3pm Discussion TA (circle one) Aaron Alex Austin Ayman Daniel Eric
More informationFormal Languages and Compilers Lecture VII Part 3: Syntactic A
Formal Languages and Compilers Lecture VII Part 3: Syntactic Analysis Free University of BozenBolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
More informationBottomup parsing. BottomUp Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form
Bottomup parsing Bottomup parsing Recall Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form If α V t,thenα is called a sentence in L(G) Otherwise it is just
More informationList of Figures. About the Authors. Acknowledgments
List of Figures Preface About the Authors Acknowledgments xiii xvii xxiii xxv 1 Compilation 1 1.1 Compilers..................................... 1 1.1.1 Programming Languages......................... 1
More informationIf you are going to form a group for A2, please do it before tomorrow (Friday) noon GRAMMARS & PARSING. Lecture 8 CS2110 Spring 2014
1 If you are going to form a group for A2, please do it before tomorrow (Friday) noon GRAMMARS & PARSING Lecture 8 CS2110 Spring 2014 Pointers. DO visit the java spec website 2 Parse trees: Text page 592
More informationSyntax Analysis Part I
Syntax Analysis Part I Chapter 4: ContextFree Grammars Slides adapted from : Robert van Engelen, Florida State University Position of a Parser in the Compiler Model Source Program Lexical Analyzer Token,
More informationRevisit the example. Transformed DFA 10/1/16 A B C D E. Start
Revisit the example ε 0 ε 1 Start ε a ε 2 3 ε b ε 4 5 ε a b b 6 7 8 9 10 εclosure(0)={0, 1, 2, 4, 7} = A Trans(A, a) = {1, 2, 3, 4, 6, 7, 8} = B Trans(A, b) = {1, 2, 4, 5, 6, 7} = C Trans(B, a) = {1,
More informationHomework. Context Free Languages. Before We Start. Announcements. Plan for today. Languages. Any questions? Recall. 1st half. 2nd half.
Homework Context Free Languages Homework #2 returned Homework #3 due today Homework #4 Pg 133  Exercise 1 (use structural induction) Pg 133  Exercise 3 Pg 134  Exercise 8b,c,d Pg 135  Exercise
More informationParsing III. (Topdown parsing: recursive descent & LL(1) )
Parsing III (Topdown parsing: recursive descent & LL(1) ) Roadmap (Where are we?) Previously We set out to study parsing Specifying syntax Contextfree grammars Ambiguity Topdown parsers Algorithm &
More informationLecture Notes on ShiftReduce Parsing
Lecture Notes on ShiftReduce Parsing 15411: Compiler Design Frank Pfenning, Rob Simmons, André Platzer Lecture 8 September 24, 2015 1 Introduction In this lecture we discuss shiftreduce parsing, which
More informationCS/B.Tech/CSE/IT/EVEN/SEM4/CS402/ ItIauIafIaAblll~AladUnrtel1ity
CS/B.Tech/CSE/IT/EVEN/SEM4/CS402/201516 ItIauIafIaAblll~AladUnrtel1ity ~ t; ~~ ) MAULANA ABUL KALAM AZAD UNIVERSITY OF TECHNOLOGY, WEST BENGAL Paper Code: CS402 FORMAL LANGUAGE AND AUTOMATA THEORY
More informationSyntax Analysis, V Bottomup Parsing & The Magic of Handles Comp 412
Midterm Exam: Thursday October 18, 7PM Herzstein Amphitheater Syntax Analysis, V Bottomup Parsing & The Magic of Handles Comp 412 COMP 412 FALL 2018 source code IR Front End Optimizer Back End IR target
More informationCMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler
More informationParsing Expression Grammars and Packrat Parsing. Aaron Moss
Parsing Expression Grammars and Packrat Parsing Aaron Moss References > B. Ford Packrat Parsing: Simple, Powerful, Lazy, Linear Time ICFP (2002) > Parsing Expression Grammars: A Recognition Based Syntactic
More informationAmbiguous Grammars and Compactification
Ambiguous Grammars and Compactification Mridul Aanjaneya Stanford University July 17, 2012 Mridul Aanjaneya Automata Theory 1/ 44 Midterm Review Mathematical Induction and Pigeonhole Principle Finite Automata
More informationChapter 3. Describing Syntax and Semantics ISBN
Chapter 3 Describing Syntax and Semantics ISBN 0321493621 Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Copyright 2009 AddisonWesley. All
More informationIntroduction to Syntax Analysis. The Second Phase of FrontEnd
Compiler Design IIIT Kalyani, WB 1 Introduction to Syntax Analysis The Second Phase of FrontEnd Compiler Design IIIT Kalyani, WB 2 Syntax Analysis The syntactic or the structural correctness of a program
More informationParser Generation. BottomUp Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottomup  from leaves to the root
Parser Generation Main Problem: given a grammar G, how to build a topdown parser or a bottomup parser for it? parser : a program that, given a sentence, reconstructs a derivation for that sentence 
More information8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing
8 Parsing Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces strings A parser constructs a parse tree for a string
More informationMultiple Choice Questions
Techno India Batanagar Computer Science and Engineering Model Questions Subject Name: Formal Language and Automata Theory Subject Code: CS 402 Multiple Choice Questions 1. The basic limitation of an FSM
More informationCMSC330 Fall 2014 Midterm 2 Solutions
CMSC330 Fall 2014 Midterm 2 Solutions 1. (5 pts) General. For the following multiple choice questions, circle the letter(s) on the right corresponding to the best answer(s) to each question. a. In the
More information