0

Recently I had to give examples for Lexical and Semantic Errors in C. I have provided the following examples.

I thought the following was lexical error,

int a#;

And for semantic error I gave the following example,

int a[10];
a=100;

But now I am a bit confused whether both are in fact syntax errors. Please provide me some idea about these errors?

Deepu
  • 7,592
  • 4
  • 25
  • 47
  • 1
    http://stackoverflow.com/questions/5535319/what-can-create-a-lexical-error-in-c this is about lexical errors and about semantic errors (and syntax) check out this one http://space.wccnet.edu/~pmillis/cps120/cps120_pgm_syntax.pdf – Blood Mar 22 '13 at 12:42

1 Answers1

4

First the classification of errors (as lexical, syntactic, semantic, pragmatic) is somehow arbitrary in the detail.

If you define a lexical error as an error detected by the lexer, then a malformed number could be one, eg 12q4z5. Or a name with a prohibited character like $

You could define a syntactic error as one detected at parsing time. However C is not stricto sensu a context-free language, and its parsers keep contextual information (e.g. in symbol tables).

Since all of a # and ; are valid lexemes your a#; is not a lexical error.

Actually # is mostly useful at preprocessing time, and what is parsed is actually the preprocessed form, not the user-given source code!

Many semantic errors are related to the notion of undefined behavior, like printf("%d") which lacks an integer argument. Or out of bound access, in your case printf("%d\n", a[1234]);

Some (but not all) semantic or pragmatic errors can be find thru static analysis tools. You can even say that modern compilers (when all warnings are enabled) do some. Your example of a=100; is a typing error (which could be arbitrarily called as syntactic, since the C compiler finds it at parsing time, and as semantic, since related to types which are not a context free property). And you have more specialized static analysis tools like Frama-C and you could extend or customize the GCC compiler (e.g. with MELT, a domain specific language) to add yours (like e.g. in TALPO).

A pragmatic error could be to declare a huge local variable, like here. Probably, when 1Tbyte RAM memory will be common, stacks would have many gigabytes, so that example would run, but not today.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I just did that in my answer. Can't you find them inside? – Basile Starynkevitch Mar 22 '13 at 12:48
  • @BasileStarynkevitch Can you please help me to clarify my doubt. What if I have code in C as `int a2.5b;`. It is not a valid identifier at all but what tokens are generated for this? Is it `int` as reserved keyword, `a` as valid identifier, `2` as valid constant, `.` as special symbol, `5` as valid constant and `;`as special symbol. Is my understanding correct? – Hardik Modha Jul 08 '15 at 17:25