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

asked in Compiler Design by Loyal (3.4k points)   | 166 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

+5 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 (273k 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 :)
+1 vote

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 (59.6k points)  
if digit in anywhere other than starting then its valid or not??
It is valid in all other cases except beginning
Top Users Jan 2017
  1. Debashish Deka

    8126 Points

  2. sudsho

    5042 Points

  3. Habibkhan

    4706 Points

  4. Vijay Thakur

    4458 Points

  5. Bikram

    4348 Points

  6. saurabh rai

    4212 Points

  7. Arjun

    4010 Points

  8. santhoshdevulapally

    3722 Points

  9. GateSet

    3292 Points

  10. Sushant Gokhale

    3286 Points

Monthly Topper: Rs. 500 gift card

19,122 questions
24,033 answers
52,725 comments
20,276 users