retagged by
740 views
0 votes
0 votes
Which Phase(s) of compiler produce error(s) in the following program?

#include<stdio.h>

int main()

{   

      nit a b ;

      a = 10; b = 20;

      printf("%d%d",a,b);

      return 0;

}

a) Lexical Analysis                        b) Syntax Analysis

c) Semantic Analysis                     d) Both a and b
retagged by

2 Answers

2 votes
2 votes

The task of lexical analyser is just to identify tokens that is validated using the DFA used for lexical analyser.In addition it ignores white spaces , tabs , comments if any.So if a token is following the pattern which is indicated by the regular expression used for the dfa of the lexical analyser.

So the token "nit" will be considered as a valid token as far as the lexical analyser since it cannot check any attribute of a token for example address for the identifier , type of the identifier(int , float)etc.

Only errors like improper format of variable name like 1_abcd is a lexical error since we know a variable name cannot start with a digit according to the constraint mentioned in the regular expression of lexical analyser being used.

It is in the semantic analysis phase that it comes to know that 'a' , 'b' has been treated like variable but defined to be 'int'.So error message is displayed : 

a , b undeclared along with line number in which a and b are used.

So the error in the given code is a semantic error only.

Hence the correct option is C) 

1 votes
1 votes

A syntax error is an error in the source code of a program. Since computer programs must follow strict syntax to compile correctly, any aspects of the code that do not conform to the syntax of the programming language will produce a syntax error.

• Spelling mistakes

• Missing out quotes

• Missing out brackets

• Using upper case characters in key words e.g. IF instead of if

• Missing out a colon or semicolon at end of a statement

• Using tokens in the wrong order

i think  (B) should be the answer

Related questions

2 votes
2 votes
2 answers
1
2 votes
2 votes
3 answers
3
kman30 asked Jan 21, 2019
1,795 views
How many tokens in thisa>>=1;and a! , Will >>= and a! be treated as a single token ?
0 votes
0 votes
1 answer
4
Vipin Rai asked Dec 12, 2018
623 views
int main(){ int a,b; a = 10; b = 11; printf(“%d %d”, a++,b );}The number of tokens is