Lecture 6: Syntax Analysis (Cont.)

Total Page:16

File Type:pdf, Size:1020Kb

Lecture 6: Syntax Analysis (Cont.) Lecture 6: Syntax Analysis (cont.) Xiaoyuan Xie 谢晓园 [email protected] 计算机学院E301 Syntax Analysis 2018/10/20 2 Where are we ? Source Lexical Analysis Code Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Machine Code 3 Derivations Revisited • A derivation encodes two pieces of information: What productions were applied to produce the resulting string from the start symbol? In what order were they applied? • Multiple derivations might use the same productions, but apply them in a different order. 4 Derivation exercise 1 Productions: assign_stmt id := expr ; expr expr op term expr term term id Let’s derive: term real id := id + real – integer ; term integer Please use left-most derivation op + op - 5 id := id + real – integer ; Left-most derivation: Using production: assign_stmt assign_stmt id := expr ; id := expr ; expr expr op term id := expr op term ; expr expr op term id := expr op term op term ; expr term id := term op term op term ; term id id := id op term op term; op + id := id + term op term ; term real id := id + real op term ; op - id := id + real - term ; term integer id := id + real - integer; 6 Parse Trees • A parse tree is a tree encoding the steps in a derivation. • Internal nodes represent nonterminal symbols used in the production. • Inorder walk of the leaves contains the generated string. • Encodes what productions are used, not the order in which those productions are applied. 7 Parse Tree • Inner nodes of a parse tree are non-terminal symbols. • The leaves of a parse tree are terminal symbols. • A parse tree can be seen as a graphical representation of a derivation. EX. E -E -(E) -(E+E) -(id+E) -(id+id) E E -E -(E) E -(E+E) E - E - E - E (E ) (E ) E E E + E - - E E -(id+E) -(id+id) (E ) (E ) E + E E + E id id id 8 E E op E | ( E ) | -E | id op + | - | * | / E E op E E id op E E op E id + E id + E op E id + E op E id * id id + id op E id + id * E id + id * id 9 Parse Trees and Derivations Consider the expression grammar: E E+E | E*E | (E) | -E | id Leftmost derivations of id + id * id E E E E + E E + E id + E E + E E + E id E id + E id + E * E E + E id E * E 10 Parse Trees and Derivations (cont.) E id + E * E id + id * E E + E id E * E id E id + id * E id + id * id E + E id E * E id id 11 Alternative Parse Tree & Derivation E E * E E E + E * E E * E id + E * E E+ E id id + id * E id id id + id * id WHAT’S THE ISSUE HERE ? Two distinct leftmost derivations! 12 Challenges in Parsing 13 Ambiguity • A grammar produces more than one parse tree for a sentence is called as an ambiguous grammar. E E E+E id+E id+E*E E + E id+id*E id+id*id id E * E id id E E*E E+E*E id+E*E E id+id*E id+id*id E * E E + E id two parse trees for id+id*id. id id 14 Is Ambiguity a Problem? Depends on semantics. E E E E + E E * * id id E E E + E id id id id 15 Resolving Ambiguity • If a grammar can be made unambiguous at all, it is usually made unambiguous through layering. • Have exactly one way to build each piece of the string? • Have exactly one way of combining those pieces back together? 16 Resolving Ambiguity • For the most parsers, the grammar must be unambiguous. • unambiguous grammar • unique selection of the parse tree for a sentence • We should eliminate the ambiguity in the grammar during the design phase of the compiler. 17 Ambiguity – Operator Precedence • Ambiguous grammars (because of ambiguous operators) can be disambiguated according to the precedence and associativity rules. E E+E | E*E | E^E | id | (E) E E+T | T disambiguate the grammar T T*F | F F G^F | G precedence: ^ (right to left) G id | (E) * (left to right) + (left to right) Rewrite to eliminate the ambiguity Or, simply tell which parse tree should be selected 18 A Parser Context free grammar, G Yes, if s in L(G) Parser No, otherwise Token stream, s (from lexer) Error messages • Syntax analyzers (parsers) = CFG acceptors which also output the corresponding derivation when the token stream is accepted • Various kinds: LL(k), LR(k), SLR, LALR 19 Types • Top-Down Parsing • Recursive descent parsing • Predictive parsing • LL(1) • Bottom-Up Parsing • Shift-Reduce Parsing • LR parser 20 Homework Page 206: Exercise 4.2.1 Page 207: Exercise 4.2.2 (d) (f) (g) 21 Top-Down Parsing 2018/10/20 22 Two Key Points expression => term => term*factor => term/factor*factor – Q1: Which non-terminal to be replaced? Leftmost derivation – Q2: Which production to be used? Top-Down Parsing The parse tree is created top to bottom (from root to leaves). By always replacing the leftmost non-terminal symbol via a production rule, we are guaranteed of developing a parse tree in a left-to-right fashion that is consistent with scanning the input. 24 Pushdown Automaton An illustration with PDA Reading Analysis Head Stack Derivation Match? P: (1) Z aBeA abec Z Z production starting aBeA a with a?- (1) (2) A Bc (3) B d bec BeA B production starting bBeA b (4) B bB with b? -(4) (5) B ec BeA B production starting eA e with e?-(5) a b e c c A A production starting with c? -(2)(5) An illustration with PDA Reading Stack Analysis Derivation Match? P: Head (1) Z aBeA c A A production Bc (2) A Bc starting with c?-(2) (3) B d (4) B bB Bc c c (5) B c A production starting with c? -(5) a b e c Problem - Backtraking • General category of Top-Down Parsing • Choose production rule based on input symbol • May require backtracking to correct a wrong choice. •Example: S c A d A ab | a S input: cad cad S cad cA d cA d a b Problem: backtrack S S S cad cad cad c d cA d A cA d a b a a 28 Problem – Left recursion • A grammar is Left Recursion if it has a nonterminal A such that there is a derivation A + A for some string . Left Recursion + top-down parsing = infinite loop Eg. Term Term*Num Term Term Term Num Term * Term Num * Term * Num …… Elimination of Left recursion • Eliminating Direct Left Recursion βiαi* Elimination of Left recursion • A A | elimination of left recursion PP PP| 1 • P P1| P2|…| P m | 1 | 2|… | n • elimination of left recursion P1 P| 2 P|…| nP P 1 P| 2 P|…| mP| 31 Elimination of Left recursion (eg.) • G[E]: E E+T|T T T*F|F F (E)| I Elimination of Left Recursion E TE E +TE| T FT T *FT| F (E)| i 32 Elimination of Left recursion (eg.) P PaPb|BaP • We have α = aPb, β=BaP • So, P βP’ Multiple P? Consider the most-left one. P’ αP’|ε • 改写后:P BaPP’ P’ aPbP’|ε Elimination of Indirect Left recursion Direct: Indirect: , , then we have e.g:S → Aa | b, A → Sd |ε S => Aa => Sda 1 34 Elimination of Left recursion algorithm 35 Elimination of Left recursion (eg.) S A b 1:S A S a | b 2:A A Aba | b A bA’ A’ baA’ | Elimination of Left recursion (eg.) 1:S 2:A S → Aa | b, S → Aa | b, ε A → Ac | Sd | A → Ac | Aad | bd |ε S → Aa | b, A →bdA’ | A’ A’ → cA’ | adA’ |ε Elimination of Left recursion (eg.) 1:S S Qc | c 2:Q Q Rb | b 3:R R S a | a S Qc | c Q Rb | b R Sa | a (Qc|c)a | a Qca | ca |a (Rb|b)ca | ca | a S Qc | c Q Rb | b R (bca | ca | a)R’ R’ bcaR’ | Elimination of Left recursion (eg.) 1:R S Qc | c 2:Q Q Rb | b 3:S R S a | a R Sa | a Q Rb | b Sab | ab | b S Qc | c Sabc | abc | bc | c S (abc | bc | c)S’ S’ abcS’ | Problem - Left Factoring • A 1 | 2 A A A 1 | 2 • A 1 | 2|… | n | A A| A 1 | 2|… | n 40 Problem - Left Factoring 41 Problem - Left Factoring • E.g – S iEtS | iEtSeS | a Eb – For, S, the longest pre-fix is iEtS, Thus, S iEtSS’ | a S’ eS| Eb Problem - Left Factoring • E.g. G: (1) S→aSb For (1)、(2), extract the left factor: (2) S→aS S→ aS(b|ε) (3) S→ε S→ε We have G′: S→aSA A→b A→ε S→ε Homework Page 216: Exercise 4.3.1 44 Two Parsing Methods 2018/10/20 45 A Naïve Method – Recursive-Descent Parsing • Backtracking is needed (If a choice of a production rule does not work, we backtrack to try other alternatives.) • It is a general parsing technique, but not widely used. • Not efficient 46 Recursive-Descent Parsing A typical procedure for a nonterminal in a top-down parse 47 Recursive-Descent Parsing • Example Z ( ) B ( ) { { P: if (token == a) case token of (1) Z aBd {a} { match(a); d: match(d);break; (2) B d {d} B( ); c: match(c); break; (3) B c {c} match(d); b:{ match(b); (4) B bB {b} } B( ); break;} else error(); other: error(); } } a b c d void main() {read(); Z(); } A Non-Recursive Method – Predictive Parsing • no backtracking, efficient • needs a special form of grammars (LL(1) grammars).
Recommended publications
  • Derivatives of Parsing Expression Grammars
    Derivatives of Parsing Expression Grammars Aaron Moss Cheriton School of Computer Science University of Waterloo Waterloo, Ontario, Canada [email protected] This paper introduces a new derivative parsing algorithm for recognition of parsing expression gram- mars. Derivative parsing is shown to have a polynomial worst-case time bound, an improvement on the exponential bound of the recursive descent algorithm. This work also introduces asymptotic analysis based on inputs with a constant bound on both grammar nesting depth and number of back- tracking choices; derivative and recursive descent parsing are shown to run in linear time and constant space on this useful class of inputs, with both the theoretical bounds and the reasonability of the in- put class validated empirically. This common-case constant memory usage of derivative parsing is an improvement on the linear space required by the packrat algorithm. 1 Introduction Parsing expression grammars (PEGs) are a parsing formalism introduced by Ford [6]. Any LR(k) lan- guage can be represented as a PEG [7], but there are some non-context-free languages that may also be represented as PEGs (e.g. anbncn [7]). Unlike context-free grammars (CFGs), PEGs are unambiguous, admitting no more than one parse tree for any grammar and input. PEGs are a formalization of recursive descent parsers allowing limited backtracking and infinite lookahead; a string in the language of a PEG can be recognized in exponential time and linear space using a recursive descent algorithm, or linear time and space using the memoized packrat algorithm [6]. PEGs are formally defined and these algo- rithms outlined in Section 3.
    [Show full text]
  • A Grammar-Based Approach to Class Diagram Validation Faizan Javed Marjan Mernik Barrett R
    A Grammar-Based Approach to Class Diagram Validation Faizan Javed Marjan Mernik Barrett R. Bryant, Jeff Gray Department of Computer and Faculty of Electrical Engineering and Department of Computer and Information Sciences Computer Science Information Sciences University of Alabama at Birmingham University of Maribor University of Alabama at Birmingham 1300 University Boulevard Smetanova 17 1300 University Boulevard Birmingham, AL 35294-1170, USA 2000 Maribor, Slovenia Birmingham, AL 35294-1170, USA [email protected] [email protected] {bryant, gray}@cis.uab.edu ABSTRACT between classes to perform the use cases can be modeled by UML The UML has grown in popularity as the standard modeling dynamic diagrams, such as sequence diagrams or activity language for describing software applications. However, UML diagrams. lacks the formalism of a rigid semantics, which can lead to Static validation can be used to check whether a model conforms ambiguities in understanding the specifications. We propose a to a valid syntax. Techniques supporting static validation can also grammar-based approach to validating class diagrams and check whether a model includes some related snapshots (i.e., illustrate this technique using a simple case-study. Our technique system states consisting of objects possessing attribute values and involves converting UML representations into an equivalent links) desired by the end-user, but perhaps missing from the grammar form, and then using existing language transformation current model. We believe that the latter problem can occur as a and development tools to assist in the validation process. A string system becomes more intricate; in this situation, it can become comparison metric is also used which provides feedback, allowing hard for a developer to detect whether a state envisaged by the the user to modify the original class diagram according to the user is included in the model.
    [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]
  • Section 12.3 Context-Free Parsing We Know (Via a Theorem) That the Context-Free Languages Are Exactly Those Languages That Are Accepted by Pdas
    Section 12.3 Context-Free Parsing We know (via a theorem) that the context-free languages are exactly those languages that are accepted by PDAs. When a context-free language can be recognized by a deterministic final-state PDA, it is called a deterministic context-free language. An LL(k) grammar has the property that a parser can be constructed to scan an input string from left to right and build a leftmost derivation by examining next k input symbols to determine the unique production for each derivation step. If a language has an LL(k) grammar, it is called an LL(k) language. LL(k) languages are deterministic context-free languages, but there are deterministic context-free languages that are not LL(k). (See text for an example on page 789.) Example. Consider the language {anb | n ∈ N}. (1) It has the LL(1) grammar S → aS | b. A parser can examine one input letter to decide whether to use S → aS or S → b for the next derivation step. (2) It has the LL(2) grammar S → aaS | ab | b. A parser can examine two input letters to determine whether to use S → aaS or S → ab for the next derivation step. Notice that the grammar is not LL(1). (3) Quiz. Find an LL(3) grammar that is not LL(2). Solution. S → aaaS | aab | ab | b. 1 Example/Quiz. Why is the following grammar S → AB n n + k for {a b | n, k ∈ N} an-LL(1) grammar? A → aAb | Λ B → bB | Λ. Answer: Any derivation starts with S ⇒ AB.
    [Show full text]
  • Formal Grammar Specifications of User Interface Processes
    FORMAL GRAMMAR SPECIFICATIONS OF USER INTERFACE PROCESSES by MICHAEL WAYNE BATES ~ Bachelor of Science in Arts and Sciences Oklahoma State University Stillwater, Oklahoma 1982 Submitted to the Faculty of the Graduate College of the Oklahoma State University iri partial fulfillment of the requirements for the Degree of MASTER OF SCIENCE July, 1984 I TheSIS \<-)~~I R 32c-lf CO'f· FORMAL GRAMMAR SPECIFICATIONS USER INTER,FACE PROCESSES Thesis Approved: 'Dean of the Gra uate College ii tta9zJ1 1' PREFACE The benefits and drawbacks of using a formal grammar model to specify a user interface has been the primary focus of this study. In particular, the regular grammar and context-free grammar models have been examined for their relative strengths and weaknesses. The earliest motivation for this study was provided by Dr. James R. VanDoren at TMS Inc. This thesis grew out of a discussion about the difficulties of designing an interface that TMS was working on. I would like to express my gratitude to my major ad­ visor, Dr. Mike Folk for his guidance and invaluable help during this study. I would also like to thank Dr. G. E. Hedrick and Dr. J. P. Chandler for serving on my graduate committee. A special thanks goes to my wife, Susan, for her pa­ tience and understanding throughout my graduate studies. iii TABLE OF CONTENTS Chapter Page I. INTRODUCTION . II. AN OVERVIEW OF FORMAL LANGUAGE THEORY 6 Introduction 6 Grammars . • . • • r • • 7 Recognizers . 1 1 Summary . • • . 1 6 III. USING FOR~AL GRAMMARS TO SPECIFY USER INTER- FACES . • . • • . 18 Introduction . 18 Definition of a User Interface 1 9 Benefits of a Formal Model 21 Drawbacks of a Formal Model .
    [Show full text]
  • Lecture 3: Recursive Descent Limitations, Precedence Climbing
    Lecture 3: Recursive descent limitations, precedence climbing David Hovemeyer September 9, 2020 601.428/628 Compilers and Interpreters Today I Limitations of recursive descent I Precedence climbing I Abstract syntax trees I Supporting parenthesized expressions Before we begin... Assume a context-free struct Node *Parser::parse_A() { grammar has the struct Node *next_tok = lexer_peek(m_lexer); following productions on if (!next_tok) { the nonterminal A: error("Unexpected end of input"); } A → b C A → d E struct Node *a = node_build0(NODE_A); int tag = node_get_tag(next_tok); (A, C, E are if (tag == TOK_b) { nonterminals; b, d are node_add_kid(a, expect(TOK_b)); node_add_kid(a, parse_C()); terminals) } else if (tag == TOK_d) { What is the problem node_add_kid(a, expect(TOK_d)); node_add_kid(a, parse_E()); with the parse function } shown on the right? return a; } Limitations of recursive descent Recall: a better infix expression grammar Grammar (start symbol is A): A → i = A T → T*F A → E T → T/F E → E + T T → F E → E-T F → i E → T F → n Precedence levels: Nonterminal Precedence Meaning Operators Associativity A lowest Assignment = right E Expression + - left T Term * / left F highest Factor No Parsing infix expressions Can we write a recursive descent parser for infix expressions using this grammar? Parsing infix expressions Can we write a recursive descent parser for infix expressions using this grammar? No Left recursion Left-associative operators want to have left-recursive productions, but recursive descent parsers can’t handle left recursion
    [Show full text]
  • Topics in Context-Free Grammar CFG's
    Topics in Context-Free Grammar CFG’s HUSSEIN S. AL-SHEAKH 1 Outline Context-Free Grammar Ambiguous Grammars LL(1) Grammars Eliminating Useless Variables Removing Epsilon Nullable Symbols 2 Context-Free Grammar (CFG) Context-free grammars are powerful enough to describe the syntax of most programming languages; in fact, the syntax of most programming languages is specified using context-free grammars. In linguistics and computer science, a context-free grammar (CFG) is a formal grammar in which every production rule is of the form V → w Where V is a “non-terminal symbol” and w is a “string” consisting of terminals and/or non-terminals. The term "context-free" expresses the fact that the non-terminal V can always be replaced by w, regardless of the context in which it occurs. 3 Definition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.) A context-free grammar is a quadruple (V, Z, P, S) where: V is a finite set of variables. E (the alphabet) is a finite set of terminal symbols. P is a finite set of rules (Ax). Where x is string of variables and terminals S is a distinguished element of V called the start symbol. The sets V and E are assumed to be disjoint. 4 Definition: Context-Free Languages A language L is context-free IF AND ONLY IF there is a grammar G with L=L(G) . 5 Example A context-free grammar G : S aSb S A derivation: S aSb aaSbb aabb L(G) {anbn : n 0} (((( )))) 6 Derivation Order 1.
    [Show full text]
  • 15–212: Principles of Programming Some Notes on Grammars and Parsing
    15–212: Principles of Programming Some Notes on Grammars and Parsing Michael Erdmann∗ Spring 2011 1 Introduction These notes are intended as a “rough and ready” guide to grammars and parsing. The theoretical foundations required for a thorough treatment of the subject are developed in the Formal Languages, Automata, and Computability course. The construction of parsers for programming languages using more advanced techniques than are discussed here is considered in detail in the Compiler Construction course. Parsing is the determination of the structure of a sentence according to the rules of grammar. In elementary school we learn the parts of speech and learn to analyze sentences into constituent parts such as subject, predicate, direct object, and so forth. Of course it is difficult to say precisely what are the rules of grammar for English (or other human languages), but we nevertheless find this kind of grammatical analysis useful. In an effort to give substance to the informal idea of grammar and, more importantly, to give a plausible explanation of how people learn languages, Noam Chomsky introduced the notion of a formal grammar. Chomsky considered several different forms of grammars with different expressive power. Roughly speaking, a grammar consists of a series of rules for forming sentence fragments that, when used in combination, determine the set of well-formed (grammatical) sentences. We will be concerned here only with one, particularly useful form of grammar, called a context-free grammar. The idea of a context-free grammar is that the rules are specified to hold independently of the context in which they are applied.
    [Show full text]
  • Packrat Parsers Can Support Left Recursion
    Packrat Parsers Can Support Left Recursion Alessandro Warth, James R. Douglass, Todd Millstein To be published as part of ACM SIGPLAN 2008 Workshop on Partial VPRI Technical Report TR-2007-002 Evaluation and Program Manipulation (PEPM ’08) January 2008. Viewpoints Research Institute, 1209 Grand Central Avenue, Glendale, CA 91201 t: (818) 332-3001 f: (818) 244-9761 Packrat Parsers Can Support Left Recursion ∗ Alessandro Warth James R. Douglass Todd Millstein University of California, Los Angeles The Boeing Company University of California, Los Angeles and Viewpoints Research Institute [email protected] [email protected] [email protected] Abstract ing [10], eliminates the need for moded lexers [9] when combin- Packrat parsing offers several advantages over other parsing tech- ing grammars (e.g., in Domain-Specific Embedded Language niques, such as the guarantee of linear parse times while supporting (DSEL) implementations). backtracking and unlimited look-ahead. Unfortunately, the limited Unfortunately, “like other recursive descent parsers, packrat support for left recursion in packrat parser implementations makes parsers cannot support left-recursion” [6], which is typically used them difficult to use for a large class of grammars (Java’s, for exam- to express the syntax of left-associative operators. To better under- ple). This paper presents a modification to the memoization mech- stand this limitation, consider the following rule for parsing expres- anism used by packrat parser implementations that makes it possi- sions: ble for them to support (even indirectly or mutually) left-recursive rules. While it is possible for a packrat parser with our modification expr ::= <expr> "-" <num> / <num> to yield super-linear parse times for some left-recursive grammars, Note that the first alternative in expr begins with expr itself.
    [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]
  • COMP 181 Z What Is the Tufts Mascot? “Jumbo” the Elephant
    Prelude COMP 181 z What is the Tufts mascot? “Jumbo” the elephant Lecture 6 z Why? Top-down Parsing z P. T. Barnum was an original trustee of Tufts z 1884: donated $50,000 for a natural museum on campus Barnum Museum, later Barnum Hall September 21, 2006 z “Jumbo”: famous circus elephant z 1885: Jumbo died, was stuffed, donated to Tufts z 1975: Fire destroyed Barnum Hall, Jumbo Tufts University Computer Science 2 Last time Grammar issues z Finished scanning z Often: more than one way to derive a string z Produces a stream of tokens z Why is this a problem? z Removes things we don’t care about, like white z Parsing: is string a member of L(G)? space and comments z We want more than a yes or no answer z Context-free grammars z Key: z Formal description of language syntax z Represent the derivation as a parse tree z Deriving strings using CFG z We want the structure of the parse tree to capture the meaning of the sentence z Depicting derivation as a parse tree Tufts University Computer Science 3 Tufts University Computer Science 4 Grammar issues Parse tree: x – 2 * y z Often: more than one way to derive a string Right-most derivation Parse tree z Why is this a problem? Rule Sentential form expr z Parsing: is string a member of L(G)? - expr z We want more than a yes or no answer 1 expr op expr # Production rule 3 expr op <id,y> expr op expr 1 expr → expr op expr 6 expr * <id,y> z Key: 2 | number 1 expr op expr * <id,y> expr op expr * y z Represent the derivation as a parse3 tree | identifier 2 expr op <num,2> * <id,y> z We want the structure
    [Show full text]
  • QUESTION BANK SOLUTION Unit 1 Introduction to Finite Automata
    FLAT 10CS56 QUESTION BANK SOLUTION Unit 1 Introduction to Finite Automata 1. Obtain DFAs to accept strings of a’s and b’s having exactly one a.(5m )(Jun-Jul 10) 2. Obtain a DFA to accept strings of a’s and b’s having even number of a’s and b’s.( 5m )(Jun-Jul 10) L = {Œ,aabb,abab,baba,baab,bbaa,aabbaa,---------} 3. Give Applications of Finite Automata. (5m )(Jun-Jul 10) String Processing Consider finding all occurrences of a short string (pattern string) within a long string (text string). This can be done by processing the text through a DFA: the DFA for all strings that end with the pattern string. Each time the accept state is reached, the current position in the text is output. Finite-State Machines A finite-state machine is an FA together with actions on the arcs. Statecharts Statecharts model tasks as a set of states and actions. They extend FA diagrams. Lexical Analysis Dept of CSE, SJBIT 1 FLAT 10CS56 In compiling a program, the first step is lexical analysis. This isolates keywords, identifiers etc., while eliminating irrelevant symbols. A token is a category, for example “identifier”, “relation operator” or specific keyword. 4. Define DFA, NFA & Language? (5m)( Jun-Jul 10) Deterministic finite automaton (DFA)—also known as deterministic finite state machine—is a finite state machine that accepts/rejects finite strings of symbols and only produces a unique computation (or run) of the automaton for each input string. 'Deterministic' refers to the uniqueness of the computation. Nondeterministic finite automaton (NFA) or nondeterministic finite state machine is a finite state machine where from each state and a given input symbol the automaton may jump into several possible next states.
    [Show full text]