1
$\begingroup$

Will the below statement cause any lexical error ?

int a123c ;

According to me, int would be tokenized as a keyword and there would be a lexical error when "a123c" would be encountered as it doesn't fall into any token category.

I read this question https://stackoverflow.com/questions/31369524/clarification-regarding-lexical-errors-in-c

Here the accepted answer says that there is no lexical error.

Also will a statement like

int x = 192.24.43.13 ;

cause any lexical error ?

I am confused.

$\endgroup$
4
  • 1
    $\begingroup$ Whether or not it will produce an error entire depends on lexical structure of the source language. It seems you assume C/C++. Why not try a compiler and see if it produces an error? $\endgroup$
    – fade2black
    Oct 23, 2017 at 7:58
  • $\begingroup$ Of course compiler will generate an error but I want to categorize the error. Whether it will be lexical, syntactical or semantical. Also I can't see link in your post. $\endgroup$
    – Sagar P
    Oct 23, 2017 at 8:04
  • $\begingroup$ I am following dragon book but there are very less examples in that book for lexical analysis. $\endgroup$
    – Sagar P
    Oct 23, 2017 at 8:04
  • 1
    $\begingroup$ You should also improve the title of your post since it is too broad. Please take some time to improve it. $\endgroup$
    – fade2black
    Oct 23, 2017 at 9:04

1 Answer 1

1
$\begingroup$

Which error message is generated by which phase of a compiler depends on the underlying language structure and implementation of the compiler.

Nevertheless, lexical analyzer is responsible for generating tokens, so at this phase you could check if some lexeme/token is valid or not. For example, you could check if the symbol $ belongs to the source language. It is similar to reading an English text and checking if words belong to the English dictionary.

The syntax analyzer on the other hands checks if the sequence of tokens constitutes a legal program structure by analyzing the sequence of tokens (not characters). For example, it may check if a curled left bracket may follow the if keyword, or what token should follow what token in general. It is similar reading reading an English text and checking if "He/She" may be followed by a verb.

Assuming you are using a C/C++ compiler the first statement will not produce an error. You can read this related post.

As for the second line, this is what I got when tried to compile int x = 192.24.43.13 ;

error: invalid suffix '.43.13' on floating constant

This seems to be produced by a lexical analyzer. It complains about the structure of the floating point number.

I also tried to compile this line int x = 192.24 .43.13 ; which produced

error: expected ';' at end of declaration

which seems to be produced by a syntax analyzer.

Other possible lexical errors or warnings may be produced by the following lines

char ch = '12'; /* multi-character character constant */
char *a = "sdsd /* missing terminating '"' character */
/* this is unterminated comment
$\endgroup$
4
  • $\begingroup$ What about statement like " itn z = 5" (instead of int) ? This will not be a lexical error but a syntax error right ? $\endgroup$
    – Sagar P
    Oct 23, 2017 at 9:34
  • 1
    $\begingroup$ Yes. You are right. $\endgroup$
    – fade2black
    Oct 23, 2017 at 9:35
  • $\begingroup$ Can you give me an example of lexical error ? Also if possible please help me with this question cs.stackexchange.com/questions/82875/augmented-production $\endgroup$
    – Sagar P
    Oct 23, 2017 at 9:43
  • $\begingroup$ Please see updates. $\endgroup$
    – fade2black
    Oct 23, 2017 at 9:58

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