occs-compilers-2014-spring / discussion

A place for discussion
0 stars 0 forks source link

Parsing assignment #5

Open ikehz opened 10 years ago

ikehz commented 10 years ago

Hey folks,

In light of what Bob said in class today about his compiler's avoidance of deciding if something is a valid Var, I'm curious how others are dealing with the following grammar rule:

Expression ::= Var = Expression | CompExpression

If I'm not mistaken, we have to look arbitrarily far ahead to decide if we're in an assignment or not: Var ::= <id> | <id>[Expression] | *<id>, and Expression can be arbitrarily long.

I'm considering implementing a kind of recovery method where we look and look and look, and then go back and assemble things properly when we finally know whether or not we're in a Var or a CompExpression. What have others done?

dan-f commented 10 years ago

I talked to Bob about this last week, and came up with a solution that I've been really happy with.

First, note that E ->* Var. My notation may not be totally formal, but the idea is that on some number of productions, E can produce what Var produces. This allows us to generalize: Call E() initially, and then see if we're looking at an =. If we are, verify that the value returned by E is in fact a variable expression node. If it is, grab an Expression(), and return an op node. If we're looking at a comparison operator (such as <, ==, etc), grab another Expression() and return an op node. Otherwise, just return the first E() value. Hope that helps.