Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will the below declaration of the variable lead to lexical error or syntactical error?

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 .

like image 855
Radha Gogia Avatar asked Dec 06 '15 15:12

Radha Gogia


People also ask

What are lexical errors examples?

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!

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 lexical and 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.

What type of error is not declaring a variable?

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.


1 Answers

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.

like image 67
Vlad from Moscow Avatar answered Oct 30 '22 05:10

Vlad from Moscow