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

asked in Compiler Design by Loyal (3.8k points)   | 339 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.....

2 Answers

+6 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 (285k 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 (66.2k 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


Top Users May 2017
  1. akash.dinkar12

    3302 Points

  2. pawan kumarln

    1776 Points

  3. Bikram

    1646 Points

  4. sh!va

    1640 Points

  5. Arjun

    1396 Points

  6. Devshree Dubey

    1272 Points

  7. Debashish Deka

    1142 Points

  8. Angkit

    1044 Points

  9. LeenSharma

    1000 Points

  10. Arunav Khare

    754 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 May 22 - 28
  1. Bikram

    732 Points

  2. Arnab Bhadra

    402 Points

  3. pawan kumarln

    402 Points

  4. bharti

    304 Points

  5. LeenSharma

    238 Points


22,823 questions
29,142 answers
65,209 comments
27,666 users