For example, which part will detect these errors, Parser or Semantic Routine?

1) int Array[10.5] ( wrong size )

2) int x="some string"

3) A[10.5] + 3 (not in declaration, but in the middle of the code)

The book that I'm reading says the first one will be detected by parser but the third one will be detected by Semantic routine! I don't get it? why not both of them by parser? if the parser can detect first one, then it should be able to detect the third one

ALSO, i thought that lexical analyzers usually turn every number into a same token? for example:

a = 3.5 + 5 --> id = num + num

so how can parser or semantic analyzer differentiate between a real number and a integer number?

Lets assume our compiler is a modern day compiler for C for example.


1 Answer 1


It's impossible to say in general; it depends on the compiler resp. the exact definition of the syntax.

If your language contains special syntax for array declarations, the parser will note that it doesn't find an integer literal between the brackets.

For array accesses, the language probably allows arbitrary arithmetic expressions (including function calls) between the brackets. So the parser will create an expression tree, and only the type checker will determine that the expression is not of type integer.

  • $\begingroup$ Sorry I edited my question, lets say we assume the compiler is a modern day compiler for C, so in that case the first one is probably gonna be detected by parser(syntax error) and the second one is detected by parser again, and the third one is detected by semantic routine, right? $\endgroup$ Oct 9, 2017 at 6:11
  • $\begingroup$ @EdaiBossinShid I don't know. I'm not that familiar with the C compiler, and that would be offtopic here. I've explained to you why both make sense. You'll have to check your specific situation for yourself. Since you're reading a book, the relevant definitions/specifications are probably in there. $\endgroup$
    – Raphael
    Oct 9, 2017 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.