The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+6 votes
int main()
    int 1a, b;
    Printf("\nGate 2018");

How many types of error are there in this code?

asked in Compiler Design by Boss (15.5k points) | 810 views

Shaik i was wrong before

Standard white-space characters are −

' '   (0x20)    space (SPC)
'\t'    (0x09)  horizontal tab (TAB)
'\n'    (0x0a)  newline (LF)
'\v'    (0x0b)  vertical tab (VT)
'\f'    (0x0c)  feed (FF)
'\r'    (0x0d)  carriage return (CR)


All white spaces acts as delimiters also a lookahead is used to check if a string is a valid token. So if there is no delimiter between 1 and a then it will give lexical error.

if the language was FORTRAN then it would not have been lexical error because in Fortran white-spaces are not considered as delimiters

 a lookahead is used to check if a string is a valid token.

any reference? 




that means

A) c is undecleared here

also semantic error

B) double initialization also give semantic error



@Mk Utkarsh

int a,b,c;

what about this? there is no white space btw a and comma and b....


int is a keyword then there is a whitespace which acts as a delimiter

while moving left to right from "a" look-ahead will check if is there any more characters which will be a valid token but next character is , special character so it is a separate token

so special characters are also works as delimiters. right?
what about

int a,b,a;

3 Answers

+4 votes
int main()
    int 1a, b; //syntax error
    Printf("\nGate 2018");
    Printf("%d",x); //x is not defined hence semantic error
  1.  A lexical error at line 3 "1a" because in C, the first character of a variable name should be either a letter or an underscore.
  2.  variable x is not defined anywhere in the program so it will give a semantic error


answered by Boss (19.7k points)
edited by

For example : int a1bc ; 
This line contains 5 tokens {int, a, 1, bc, ;}

there are no commas present (int a1bc)

how can we add commas (int, a, 1, bc, ;)


MIRIYALA JEEVAN KUMA that was a set representation. 
check now

@Utkarsh , Lexical Analysis phase uses "Lookahead" to decide where one token ends and next token begins. so 1 and a will not be considered as 2 tokens here.
can you provide a good source for this?

ankitgupta.1729 i watched the video thanks for the reference. updated the answer :)

+2 votes
int main()
    int 1a, b;
    Printf("\nGate 2018");


int 1a, b; $\Rightarrow$ error: invalid suffix "a" on integer constant

This gives a $lexical\space error$. This is because in C, the first character of a variable name should be either a letter or an underscore.


Printf("\nGate 2018"); $\Rightarrow$ warning: implicit declaration of function 'Printf'

$Semantic\space error$ at Printf. C is a case sensitive language and function used to print is printf. The program identifies 'Printf' as a new function and gives 'function not declared' error.


Printf("%d",x); $\Rightarrow$ error: 'x' undeclared (first use in this function)

This is a $semantic\space error$ again, as x is used here before declaring.


So, the $number\space of\space types$ of errors in this code is $2$. (Lexical error and Semantic error)

answered by Active (1.3k points)
edited by
@arun bro , I think by mistake you have written syntax error for int 1a; . it should be lexical error.
@ankitgupa.1729 Oh  yes, i also think it is lexical error. will correct it.

want to add some points to clear why 1a is a single invalid token and not 2 valid tokens as 1 and a ..

Lexical analyzer uses lookahead or some different strategy to decide whether one token ends and next token begins and to check whether it is a keyword and identifier...

In C , whitespace is not considered as a token class . it is a separator and whitespaces are removed after lexical analysis phase.

here, lexical analyzer scan 1a from left to right using lookahead till white space comes. when it reaches 1 then it is considered as a valid token  but when it reaches 'a' then it will throw lexical error because token class of identifier starts with a letter or underscore and not a digit. So, 1a will be considered as an invalid identifier. So, it is not a valid token.

 1111 , 1(whitespace)a  and while1 are  valid tokens. here while and 1 should not be considered as 2 different tokens of while and 1...

how lexical analyzer consider "else" as a  keyword not  4 tokens as e,l,s,e and how == is considered as a single token not  2 tokens as = and = is explained here 


0 votes
Considering the language to be C , there are 4 errors :

1. Identifier "1a" is not a valid declaration.

2. The 'P' in printf function should be in small letter.

3. Variable "x" should be declared first.

4. The function should return a value. So there must be a return statement.
answered by (11 points)

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

40,845 questions
47,507 answers
62,262 users