The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+5 votes
704 views
int main()
{
    int 1a, b;
    Printf("\nGate 2018");
    Printf("%d",x);
}

How many types of error are there in this code?

asked in Compiler Design by Boss (15.1k points) | 704 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 :(
+2

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

2 Answers

+3 votes
int main()
{
    int 1a, b; //syntax error
    Printf("\nGate 2018");
    Printf("%d",x); //x is not defined hence semantic error
}
  1.  A syntax error at line 3 "1a" because in C a variable can either start from underscore or an alphabet.
  2.  variable x is not defined anywhere in the program so it will give a semantic error

Additional information :

C tokens are of six types. They are,

  1. Keywords               (eg: int, while),
  2. Identifiers               (eg: main, total),
  3. Constants              (eg: 10, 20),
  4. Strings                    (eg: “total”, “hello”),
  5. Special symbols  (eg: (), {}),
  6. Operators              (eg: +, /,-,*)

"1a" does not give any lexical error because source file is scanned left to right. First 1 is scanned and when the next scanned character does not matches the previous one (a) then lexical analyzer thinks they both are different tokens. 1 is constant and a is an identifier and does not generate any lexical error.

While scanning input by lexical analyzer the irrelevant information is removed(white spaces and comments).

 

answered by Boss (14k points)
reshown 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

0
@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?
+2 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)

answered by Active (1.3k 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.
0

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 

 



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

38,084 questions
45,574 answers
132,084 comments
49,061 users