views:

111

answers:

3

I'm writing code for the exercise 1-24, K&R2, which asks to write a basic syntactic debugger.

I made a parser with states normal, dquote, squote etc...

So I'm wondering if a code snippet like

/" text "

is allowed in the code? Should I report this as an error? (The problem is my parser goes into comment_entry state after / and ignores the ".)

+2  A: 

That is syntactically valid, but not semantically. It should parse as the division operator followed by a string literal. You can't divide stuff by a string literal, so it's not legal code, overall.

Comments start with a two-character token, /*, and end with */.

unwind
+4  A: 

Since a single / just means division it should not be interpreted as a comment. There is no division operator defined for strings, so something like "abc"/"def" doesn't make much sense, but it should not be a syntax error. Figuring out if this division is possible should not be done by the parser, but be left for later stages of the compilation to be decided there.

sth
A: 

As a standalone syntactical element this should be reported as an error.

Theoretically (as part of an expression) it would be possible to write

a= b /"text";   / a = b divided through address of string literal "text"

which is also wrong (you can't divide through a pointer).

But on the surface level would seem okay because it would syntactically decode as: variable operator variable operator constant-expression (address of string).

The real error would probably have to be caught in a deeper state of syntactical analysis (i.e. when checking if given types are suitable for the division operator).

Nicholaz