1. Home
  2. Explore
  3. Computer Science
  4. Syntax Tree Project Overview

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Syntax Tree Project Overview

  • 18Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Parsing Fundamentals

What is the primary role of a parser in programming language processing?

18 questions

  1. 1

    What is the primary role of a parser in programming language processing?

    1. A

      To execute the source code directly

    2. B

      To transform plain text content into a structured data representation

    3. C

      To generate machine code from source code

    4. D

      To check for syntax errors only

    Correct answer
    B

    To transform plain text content into a structured data representation

    Explanation

    A parser takes plain text and converts it into a data structure (like an AST) that a program can work with. The other options describe interpreters, syntax checkers, or compilers.

  2. 2

    During lexical analysis, what happens to the input text?

    1. A

      It is broken into tokens like nouns, verbs, and adjectives

    2. B

      It is checked for semantic correctness

    3. C

      It is directly converted into a syntax tree

    4. D

      It is grouped into phrases based on grammar rules

    Correct answer
    A

    It is broken into tokens like nouns, verbs, and adjectives

    Explanation

    Lexical analysis (tokenization) splits the input into meaningful tokens. The other steps occur later in parsing.

  3. 3

    In a syntax tree, what distinguishes leaf nodes from branch nodes?

    1. A

      Leaf nodes are always strings, while branch nodes are numbers

    2. B

      Leaf nodes are mutable, while branch nodes are immutable

    3. C

      Leaf nodes wrap values from the source code, while branch nodes have children

    4. D

      Leaf nodes contain operators, while branch nodes contain operands

    Correct answer
    C

    Leaf nodes wrap values from the source code, while branch nodes have children

    Explanation

    Leaf nodes (tokens) represent atomic values, while branch nodes contain other nodes. The talk explicitly states leaf nodes wrap a value from source code.

  4. 4

    Which method in the syntax tree node interface enables the visitor pattern?

    1. A

      child_nodes

    2. B

      copy

    3. C

      deconstruct

    4. D

      accept

    Correct answer
    D

    accept

    Explanation

    The accept method calls back into the visitor, implementing double dispatch. Other methods serve different purposes.

  5. 5

    Are syntax tree nodes mutable by default?

    1. A

      No, they are immutable; mutation is done via a dedicated interface

    2. B

      Yes, they are mutable and can be changed with dot assignment

    3. C

      It depends on the programming language being parsed

    4. D

      Only leaf nodes are mutable; branch nodes are immutable

    Correct answer
    A

    No, they are immutable; mutation is done via a dedicated interface

    Explanation

    The talk states nodes are immutable by default and mutation should go through a better interface (like the mutation visitor).

  6. 6

    In the visitor pattern, what is meant by 'double dispatch'?

    1. A

      The node dispatches to two different visitors

    2. B

      The visitor calls itself recursively twice for each node

    3. C

      The visitor method calls the accept method on two different nodes

    4. D

      Two methods are called: the node's accept method and the visitor's specific visit method

    Correct answer
    D

    Two methods are called: the node's accept method and the visitor's specific visit method

    Explanation

    The accept method calls the visitor's visit method with self, which then returns to the node. This two-method interaction is double dispatch.

  7. 7

    How does the generic visit method in the visitor pattern work for nodes that only need to traverse children?

    1. A

      It uses a case statement to handle every node type

    2. B

      It directly inspects the child_nodes method and calls accept recursively

    3. C

      It delegates to a separate traversal method defined in each node

    4. D

      It calls visit_child_nodes, which calls accept on each child node

    Correct answer
    D

    It calls visit_child_nodes, which calls accept on each child node

    Explanation

    The talk explains that visit_child_nodes calls accept on each child, and specific visit methods can be aliased to it for nodes that only need traversal.

  8. 8

    What is Ripper in the context of Ruby parsing?

    1. A

      A tool that converts Ruby code into executable bytecode

    2. B

      A standard library that provides an event-based parser generated from Ruby's grammar file

    3. C

      A third-party gem for parsing Ruby into JSON

    4. D

      A linter that checks for style violations

    Correct answer
    B

    A standard library that provides an event-based parser generated from Ruby's grammar file

    Explanation

    Ripper is a standard library parser that fires events (like on_const) as it parses. It is generated from the C Ruby grammar file (parse.y).

  9. 9

    Why does syntax tree need to handle Lambda locals separately from Ripper?

    1. A

      Lambda locals require a special sub-parser because Ripper does not expose them

    2. B

      Ripper crashes when encountering Lambda locals

    3. C

      Lambda locals are not part of the Ruby language

    4. D

      Ripper does not provide location information for Lambda local variables

    Correct answer
    A

    Lambda locals require a special sub-parser because Ripper does not expose them

    Explanation

    The talk mentions that Ripper does not support Lambda local variables, so syntax tree parses them itself using a state machine.

  10. 10

    How does syntax tree attach comments to nodes after parsing?

    1. A

      By using Ripper's built-in comment attachment mechanism

    2. B

      By performing a binary search over the child nodes to find the most appropriate node for each comment

    3. C

      By storing all comments in a separate array and ignoring them

    4. D

      By associating each comment with the nearest token by line number

    Correct answer
    B

    By performing a binary search over the child nodes to find the most appropriate node for each comment

    Explanation

    The talk describes building an array of comments and then binary-searching through the tree to attach them to the node that immediately follows or is on the same line.

  11. 11

    What is the core idea behind the prettyprint algorithm used for formatting?

    1. A

      Replacing all spaces with newlines

    2. B

      Reindenting every line to a fixed column width

    3. C

      Converting the AST back into a string via a depth-first traversal

    4. D

      Building groups of nodes with breakables and breaking the outermost group when the line width is exceeded

    Correct answer
    D

    Building groups of nodes with breakables and breaking the outermost group when the line width is exceeded

    Explanation

    The talk explains the prettyprint algorithm: create groups with breakables, and when line width is exceeded, break the outermost group first.

  12. 12

    What is the prettier print gem, and why was it created?

    1. A

      A gem that only supports Ruby 2.7 pattern matching

    2. B

      A gem that formats JavaScript code using the Prettier algorithm

    3. C

      A drop-in replacement for prettyprint with additional features, created because the maintainer of prettyprint did not accept syntax tree's changes

    4. D

      A wrapper around syntax tree to make formatting faster

    Correct answer
    C

    A drop-in replacement for prettyprint with additional features, created because the maintainer of prettyprint did not accept syntax tree's changes

    Explanation

    The talk states that the maintainer of prettyprint rejected the additions, so syntax tree published a new gem that is a drop-in replacement with more functionality.

  13. 13

    What does the syntax_tree match CLI command output?

    1. A

      The formatted version of the source code

    2. B

      The AST in JSON format

    3. C

      A Ruby expression that can be used in a pattern match to exactly match the given source node

    4. D

      The location of every match in the source file

    Correct answer
    C

    A Ruby expression that can be used in a pattern match to exactly match the given source node

    Explanation

    The talk describes match as outputting a pattern-matching expression that can be used in an in clause to match the node represented by the source.

  14. 14

    How does the syntax_tree search command work internally?

    1. A

      It compares the AST of the pattern with the AST of the file

    2. B

      It uses syntax tree to parse the pattern and builds a proc that matches against nodes

    3. C

      It performs a regex search on the source text

    4. D

      It uses the language server protocol to find nodes

    Correct answer
    B

    It uses syntax tree to parse the pattern and builds a proc that matches against nodes

    Explanation

    The talk explains that search uses syntax tree to parse the pattern matching expression and generates a proc that is used to match nodes.

  15. 15

    Which language server protocol procedures does syntax tree's language server initially support?

    1. A

      textDocument/codeAction, textDocument/rename, and textDocument/documentLink

    2. B

      didChangeWatchedFiles, executeCommand, and workspace/symbol

    3. C

      didOpen, didChange, didClose, and textDocument/formatting

    4. D

      completion, hover, goToDefinition, and findReferences

    Correct answer
    C

    didOpen, didChange, didClose, and textDocument/formatting

    Explanation

    The talk lists didChange, didOpen, didClose, and textDocument/formatting as the main procedures provided.

  16. 16

    What is the benefit of syntax tree's YARV visitor?

    1. A

      It converts YARV bytecode into syntax tree nodes

    2. B

      It optimizes Ruby code for faster execution

    3. C

      It compiles syntax tree nodes into Ruby VM instruction sequences for possible execution

    4. D

      It allows debugging of Ruby bytecode

    Correct answer
    C

    It compiles syntax tree nodes into Ruby VM instruction sequences for possible execution

    Explanation

    The talk mentions that the YARV visitor compiles syntax tree nodes into instruction sequence objects representing bytecode, potentially enabling execution after mutation.

  17. 17

    How does syntax tree enable auto-correction or linting rules?

    1. A

      By directly modifying the source text based on line numbers

    2. B

      By calling RuboCop's internal API

    3. C

      By using a mutation visitor that queries the tree with pattern matching and applies copy methods to produce a modified tree

    4. D

      By replacing Ripper with a custom parser that includes fix functionality

    Correct answer
    C

    By using a mutation visitor that queries the tree with pattern matching and applies copy methods to produce a modified tree

    Explanation

    The talk describes a mutation visitor that uses a query (pattern match) and then calls copy on nodes to return a new tree with changes, enabling auto-correction.

  18. 18

    What is the planned future replacement for Ripper in syntax tree?

    1. A

      The whitequark/parser gem

    2. B

      A new, air-tolerant, portable parser being developed at Shopify

    3. C

      A JavaScript-based parser for compatibility

    4. D

      A parser that uses machine learning

    Correct answer
    B

    A new, air-tolerant, portable parser being developed at Shopify

    Explanation

    The talk mentions that a new Ruby parser is being written at Shopify, which will be more efficient, air tolerant, and portable, and will replace Ripper transparently.

Question 1 of 18
Question 1 of 18

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent