If I declare the variable like
int a/*comment*/ ; //This does not give any error .
int a/*comment*/bc; This gives error
Now I am not getting the reason behind this , According to me when the character a is read for the first time after that symbol / is read so is it that it switches to some other state of DFA for recognizing some other pattern hence no error while in the second case after comment is read it finds some other sequence which couldn't belong to the formal pattern hence it gets halted in some non-final state of finite automaton due to which it gives an error .
Please clear this confusion .
A lexical error is any input that can be rejected by the lexer. This generally results from token recognition falling off the end of the rules you've defined. For example (in no particular syntax): [0-9]+ ===> NUMBER token [a-zA-Z] ===> LETTERS token anything else ===> 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.
Both declarations are invalid, so you are rightfully confused, but for different reasons: A lexical error occurs when the compiler does not recognize a sequence of characters as a proper lexical token. 2ab is not a valid C token.
The identifier is undeclared: In any programming language, all variables have to be declared before they are used. If you try to use the name of a such that hasn't been declared yet, an “undeclared identifier” compile-error will occur.
According to the C Standard (5.1.1.2 Translation phases)
3. ...Each comment is replaced by one space character.
Thus this line
int a/*comment*/bc;
after the translation phase is equivalent to
int a bc;
But you could write :)
int a\
bc;
provided that bc;
starts at the first position of the next line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With