The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
0 votes

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

+11 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 semantic error for variable not defined.
answered by Veteran (327k 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 (96.7k 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
Identifier(function or variable)
Special symbols.
So 1xba not any things
So it produces Lexical error
answered by (153 points)

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

29,154 questions
36,976 answers
34,816 users