First time here? Checkout the FAQ!
0 votes

asked in Compiler Design by Loyal (3.8k points)   | 308 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 (283k 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 should eventually give an error there
int sum;
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 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 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 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 (65.9k 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 Apr 2017
  1. akash.dinkar12

    3660 Points

  2. Divya Bharti

    2580 Points

  3. Deepthi_ts

    2040 Points

  4. rude

    1966 Points

  5. Tesla!

    1768 Points

  6. Debashish Deka

    1614 Points

  7. Shubham Sharma 2

    1610 Points

  8. Prashant.

    1492 Points

  9. Arjun

    1472 Points

  10. Arunav Khare

    1464 Points

Monthly Topper: Rs. 500 gift card

22,088 questions
28,063 answers
24,173 users