GATE CSE
First time here? Checkout the FAQ!
x
0 votes
508 views

asked in Compiler Design by Loyal (3.9k points)   | 508 views
Lexical error is detected by regular expression like identifier or keywords matching.

Here in line x = 1xab neither its a number nor an identifier so it results in lexical error.

lexical error in 1xab...identifiers shouldnt start with digits..

@sudso .......plz give any refernce.....

3 Answers

+7 votes
Best answer
1xab - is not a valid token in C (as variable names cannot start with a digit and numbers cannot have a letter other than 'x' (for hex) or 'l' (for long) and also not a valid keyword) and hence lexical error. It could have been x1ab, _x1ab etc. both would have given a semnatic error for variable not defined.
answered by Veteran (295k points)  
selected by
Sir I think that undeclared variable is a synatx error instead of sementic error
Why so? Can a PDA detect it?
@Arjun sir variable is not defined means it will nt be present in the symbol table....during conversion of tokens when lexical analyser checks the symbol table and didnt find it..it should eventually give an error there itself...like
int sum;
sno=15;
here because of sno there should be a lexical error...??
But how will the lexical analysis know that "sno" is a usage or a declaration?
sir it will check the symbol table for undefined symbols i think...if found then ok else should give error...like for sum it should know its datatype...m i wrong here?
and sir if not then what is the use of symbol table for lexical analyser?
Whatever lexical analyzer can do should be possible for a finite automata- or can be expressed as a regular expression. So, how this can be done?

Lexical analysis phase just populates the symbol table - not use for itself.

hmm ohk sir..one more thing if an identifier or keyword  is misspelled then also lexical analyser shouldnt create any problem as it just needs to create a token or there will be pattern matching as we would have already given regular expressions for identifiers and keywords etc?..i mean like itn a; it should be parsed by it na sir...because it dont know whether itn is a keyword or identifier....so it should create a token??

means except pattern matching of the identifiers lexical analyser cant give any error..rght sir?

yes, it won't give any error as long as the pattern satisfies any of the valid token.
ohkk thankyou sir :)
+2 votes

It is a lexical error at earliest as we can see that the token "1xab" will be treated as an identifier ..But we know as per standard naming convention in C language , the variable name cannot begin with "1" ..

So it will be an invalid token..Hence the error concerned will be lexical error at the earliest..

Hence A) is correct answer..

answered by Veteran (78.8k points)  
if digit in anywhere other than starting then its valid or not??
It is valid in all other cases except beginning
Undeclared variable is syntax or Symantic error
0 votes
Lexical analyser scan the program and generate token store into symbol table and return token id.
Token may be
Keyword
Identifier(function or variable)
Operators
Special symbols.
So 1xba not any things
So it produces Lexical error
answered by (35 points)  


Top Users Sep 2017
  1. Habibkhan

    8796 Points

  2. rishu_darkshadow

    3572 Points

  3. Warrior

    2914 Points

  4. Arjun

    2840 Points

  5. A_i_$_h

    2550 Points

  6. manu00x

    2268 Points

  7. nikunj

    1990 Points

  8. Bikram

    1874 Points

  9. makhdoom ghaya

    1820 Points

  10. SiddharthMahapatra

    1718 Points


26,346 questions
33,928 answers
80,524 comments
31,231 users