The Gateway to Computer Science Excellence
+7 votes
1.3k views
int main()
{
    int 1a, b;
    Printf("\nGate 2018");
    Printf("%d",x);
}

How many types of error are there in this code?

in Compiler Design by Boss (18.2k points) | 1.3k views
0
lexical 1, syntax 2 ?
0
x is not defined. It doesn't count?
+1
Answer is a Lexical and a semantic error.

Lexical error at "1ax"

And Semantic error at "x is not defined before its use."
0
int main()
{
    int 1a, b; // 1a will give lexical error
    Printf("\nGate 2018");
    Printf("%d",x); //x is not defined hence semantic error
}
0
ANy good resource for these Questions? I always go wrong with these questions :(
+3

https://web.stanford.edu/class/archive/cs/cs143/cs143.1128/

Go through the slides (under lecture sub-heading)
 

0

@Mk Utkarsh why 1a is not Syntax error?

0
Thanks man!! :)
0
@shubhanshu because L is not in the form of L(L+D)*

L= {a,..z,A,...Z,_} and D={1,2..0}

this is the rough idea
0
Yes, "1a" is a lexical error, but why not syntax, Is it because syntax errors only deal with the syntax of expression?
0

Shubhanshu if it passed lexical analysis then it is some kind of token and defined by the grammar and if not defined in the grammar then it may raise a syntax error

+1
once u dont get a token for the expression 1a from the lexical phase  to the parser hence there itself the program stops with a single lexical error the parser needs to simultaneously construct the parse tree when the lexical analyzer passes it the token but here the parser cannot generate the token for 1a
0

I think it is lexical error only but not syntax error, as it correctly following the rule of variable declaration in C.

0

Shubhanshu first character should be underscore or alphabet

0

Shubhanshu maybe i'm wrong 

int main()
{
    int 1a, b; // 1a will give syntax error
    Printf("\nGate 2018");
    Printf("%d",x); //x is not defined hence semantic error
}

"1" is a token and "a" is a token

but together "1a" is not allowed and it will be a syntax error.

0

"1" is a token and "a" is a token 

is there a delimiter b/w '1' and 'a' ?

i don't think so, then how could lexical analyzer delimit '1' and 'a' and marked them two different tokens ? 

0
lexical analyzer don't need any delimiter.

for example : "while()"

is there any delimiter between "while"and "(" ? still they both are different tokens right? but why?

because they belong to different types one is a keyword and other is a special symbol
0

is there any delimiter between "while"and "(" ? still they both are different tokens right? but why?

Here there is no need of a delimiter.

why because : while is a keyword(hence a token)

0

MIRIYALA JEEVAN KUMA , then whilem is generating two tokens ( while,m ) right?

lexical analyzers generates a lexeme when pattern getting end...

means while() ===> after scanning 'e' letter of while, scans '(' ===> contradict with previous patterns ===> generates while as lexeme..

int 1a, ===> int and 1 and a and  ====> Are lexemes hence it is not Lexical error

0

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.

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

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

any reference? 

+1

..

0

that means

https://gateoverflow.in/236759/syntax-error

A) c is undecleared here

also semantic error

B) double initialization also give semantic error

rt?

0

@Mk Utkarsh

int a,b,c;

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

0

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

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

int a,b,a;

3 Answers

+5 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

 

by Boss (35.8k points)
edited by
0

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, ;)

0

MIRIYALA JEEVAN KUMA that was a set representation. 
check now

+1
@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.
0
can you provide a good source for this?
0

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

0
:)
+3 votes
int main()
{
    int 1a, b;
    Printf("\nGate 2018");
    Printf("%d",x);
}

 

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)

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

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 =  ..it 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.
by (35 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
50,645 questions
56,615 answers
195,894 comments
102,332 users