Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler Design : Is "variable not declared" a syntactic error or semantic error?

Is such type of an error produced during type checking or when input is being parsed? Under what type should the error be addressed?

like image 498
arg21 Avatar asked Nov 02 '12 20:11

arg21


People also ask

Is not declaring a variable a syntax error?

In the case of languages -- and there are many -- which require identifiers to be declared, a program with undeclared identifiers is ill-formed and thus a missing declaration is clearly a syntax error.

What is syntactic error in compiler design?

In computer science, a syntactic error is an error in the syntax of a sequence of characters or tokens intended to be written in a specific programming language. This type of error appears during the syntax analysis phase. Syntax or syntactic error is also found during the execution of the program.

What is syntactic or semantic error?

The syntax error is an incorrect construction of the source code, whereas a semantic error is erroneous logic that produces the wrong result when executed.

What is semantic error in compiler design?

A semantic error is text which is grammatically correct but doesn't make any sense. An example in the context of the C# language will be “int x = 12.3;” - 12.3 is not an integer literal and there is no implicit conversion from 12.3 to int, so this statement does not make sense. But it is grammatically correct.


2 Answers

The way I see it it is a semantic error, because your language parses just fine even though your are using an identifier which you haven't previously bound--i.e. syntactic analysis only checks the program for well-formed-ness. Semantic analysis actually checks that your program has a valid meaning--e.g. bindings, scoping or typing. As @pst said you can do scope checking during parsing, but this is an implementation detail. AFAIK old compilers used to do this to save some time and space, but I think today such an approach is questionable if you don't have some hard performance/memory constraints.

like image 100
Alex Avatar answered Sep 30 '22 08:09

Alex


The program conforms to the language grammar, so it is syntactically correct. A language grammar doesn't contain any statements like 'the identifier must be declared', and indeed doesn't have any way of doing so. An attempt to build a two-level grammar along these lines failed spectacularly in the Algol-68 project, and it has not been attempted since to my knowledge.

The meaning, if any, of each is a semantic issue. Frank deRemer called issues like this 'static semantics'.

like image 40
user207421 Avatar answered Sep 30 '22 09:09

user207421