in Compiler Design retagged by
703 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
in Compiler Design retagged by
703 views

1 comment

i think it is syntax error since lexical analyser will test only for tokens
1
1

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) 

4 Comments

Syntax analysis just checks the validity of tokens in a sentence if they are correct according to the grammar .Alternatively we can say it creates a parse tree with the help of the grammar.

But in semantic analysis phase , we get to know more about tokens as annotated parse tree is being created so more information is added to nodes of the parse tree .So the thing that "a" , "b" are undeclared , that the compiler comes to know about in the semantic phase only.

So variable "a" undeclared is a sort of semantic error.

But in nit a b ; b expects a comma , space , assignment etc which is related to sentential form of statement and hence mentioned in the grammar used in the parser.

So it is a syntax error.
2
2

I think, Here ans should be both syntax analysis and semantic analysis.

Syntax analysis phase or parsing - Because, the parser checks if the expression made by the tokens is syntactically correct. Here "nit" is syntactactically not correct token.

Semantic Analysis phase - Because, This phase checks whether identifiers are declared before use or not.

                                        Here a=10, b=20. assignment is done before correct declaration of a and b.

0
0
So it is both Syntax as well as Semantic error right?
0
0
1 vote
1 vote

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

3 Comments

But here a and b are not declared and they are initialized to 10 and 20 respectively. This is treated as declaration before use error and it can be caught only by using CSG and not CFG...So I think the answer should be C
1
1
Syntax analysis deals with the correctness of statements used which is checked using the grammar but it is not associated with meaning of tokens in statement which is done by semantic analyser. Hence C option is correct @Rajesh Raj
1
1
hmm.. u r right
0
0

Related questions