The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+2 votes
493 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 (15k points) | 493 views
0
lexical 1, syntax 2 ?
0
x is not defined. It doesn't count?
0
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

0
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)

1 Answer

+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).

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

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



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

36,088 questions
43,532 answers
123,707 comments
42,762 users