Question: This question asks you to investigate how the parser works. Q1: Different parse errors Consider the following two expressions that both produce parse errors: (2
This question asks you to investigate how the parser works.
Q1: Different parse errors
Consider the following two expressions that both produce parse errors:
(2
(2 +
Why does only the first one, but not the second one, complain about the unclosed parenthesis, even though this is an error that's present in both of them?
Q2: Different types of syntax errors
We have seen two kinds of syntax errors so far: input that doesnt scan/tokenize correctly, and input that scans/tokenizes correctly but does not produce a valid parse tree. Give two examples of each kind of error.
Q3: Draw an abstract syntax tree
Draw the abstract syntax tree (AST) that the parser would produce for the following expression:
2 + (5 / 7.2)
Annotate each node with which type it is. So far we have 4 types of nodes, all subclasses of Expr: Binary, Unary, Grouping, and Literal.
Hint: there should be 6 nodes.
Q4: Parsing other kinds of things
The parser right now only parses expressions. In Parser.java, parse() tries to parse the token stream as an expression, using the recursive-descent parser starting with the call expression(). It returns an Expr if it succeeds.
What would need to added and/or changed for statements to be parsed? For example, what if we wanted to parse variable declaration statements like var x = 5;? You don't have to implement this (we'll be doing that in a week or two), but describe what types of changes would be needed, and where.
Hint: One strategy is to look at what parts currently assume everything is an Expr.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
