Reputation: 94
I am new to compiler construction and I was trying to make a CFG(Context Free Grammar) of Assignment Statement in programming for Syntax Analyzer in Compiler Construction and I want to know whether this illegal statement is a semantic error or a syntax error ?
5=a;
thanks!
Upvotes: 4
Views: 580
Reputation: 95306
The distinctions we use between "syntax" and "semantic" seem to me to be largely an accident of how we currently build compilers with "weak parsers".
The purpose of a compiler is to recognize that is has been given a valid program, to diagnose errors in that program where practical, and to compile that code into an executable form.
How it recognizes a valid program is usually accomplished by using a parser which knows something about the syntactic structure of the program (in many cases driven explicitly by a grammar), followed up by a set of "semantic" checks to validate that the provided structures don't violate the constraints as defined by the language reference manual.
As a practical issue, one cannot define a "parser" the checks all the "syntax" constraints: the parsing technology is often (always!) too weak. We settle for parsers that at best check the context-free structural properties of the program (e.g, "parentheses balance"). Everything else we push into "semantic checking" (by virtue of being the only other place according to the definition in the preceding paragraph).
So, one can define a really weak parser that simply reads characters and accepts whatever the character stream is (surely, your program source is made of characters :), and relegates everything else to "semantic checking". Any additional syntax checking that our chosen parser technology might do is just (admittedly very convenient) gravy.
So, yes, you can define a parser that accepts "5=a;" as matching (some of) the syntax constraints, and do a semantic check later that the left hand side is valid.
With most traditional parser generators (or even a hand-rolled recursive descent parser), you can, with some modest effort, usually define a grammar for your language which will reject "5=a;" as a "syntax error". Because this is so easy, we often assume that such checks are actually done by parsing, and so we would normally say this is a "syntax error" regardless of the parsing technology used, even though that is sloppy.
In contrast, "String S=3.7;" is probably accepted by our parser; the type inconsistency is probably done by a semantic check, so we would say that type checking is a semantic error. But this interpretation comes about because most compilers happen to be built in way that this is true.
If you have a sufficiently powerful (Turing-capable) specification system, (e.g, Van Wingaarden Grammars or Meta-S), you can in fact encode what you think of as all the "syntax" and "semantics" in the same formalism, and have that formalism "executed" to validate your source code. If that engine complains, is it a "syntax error" or a "semantic error"? In this case, we no longer have separate "parsing" and "semantic checking" so it gets difficult to say. At best you can say you have an error "parsing the source text as a valid program".
Upvotes: 4